[PATCH 08/12] vinyl: use mempool for vy_history_node allocations

Vladimir Davydov vdavydov.dev at gmail.com
Sun Apr 15 22:55:21 MSK 2018


To reuse vy_history in read iterator, we need to persist history nodes
between calls to vy_read_iterator_next so we can't allocate them on the
region. So let's introduce a memory pool for them.
---
 src/box/vy_history.c      | 15 ++++++++-------
 src/box/vy_history.h      |  9 +++++++--
 src/box/vy_lsm.c          |  5 +++++
 src/box/vy_lsm.h          |  4 +++-
 src/box/vy_point_lookup.c |  5 +----
 5 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/box/vy_history.c b/src/box/vy_history.c
index a11705a6..c1fe6c6a 100644
--- a/src/box/vy_history.c
+++ b/src/box/vy_history.c
@@ -30,13 +30,13 @@
  */
 #include "vy_history.h"
 
+#include <assert.h>
 #include <stdbool.h>
 #include <stddef.h>
-#include <small/region.h>
+#include <small/mempool.h>
 #include <small/rlist.h>
 
 #include "diag.h"
-#include "fiber.h"
 #include "tuple.h"
 #include "iproto_constants.h"
 #include "vy_stmt.h"
@@ -45,10 +45,10 @@
 int
 vy_history_append_stmt(struct vy_history *history, struct tuple *stmt)
 {
-	struct region *region = &fiber()->gc;
-	struct vy_history_node *node = region_alloc(region, sizeof(*node));
+	assert(history->pool->objsize == sizeof(struct vy_history_node));
+	struct vy_history_node *node = mempool_alloc(history->pool);
 	if (node == NULL) {
-		diag_set(OutOfMemory, sizeof(*node), "region",
+		diag_set(OutOfMemory, sizeof(*node), "mempool",
 			 "struct vy_history_node");
 		return -1;
 	}
@@ -63,10 +63,11 @@ vy_history_append_stmt(struct vy_history *history, struct tuple *stmt)
 void
 vy_history_cleanup(struct vy_history *history)
 {
-	struct vy_history_node *node;
-	rlist_foreach_entry(node, &history->stmts, link) {
+	struct vy_history_node *node, *tmp;
+	rlist_foreach_entry_safe(node, &history->stmts, link, tmp) {
 		if (node->is_refable)
 			tuple_unref(node->stmt);
+		mempool_free(history->pool, node);
 	}
 	rlist_create(&history->stmts);
 }
diff --git a/src/box/vy_history.h b/src/box/vy_history.h
index 01f5364c..bb4ed28e 100644
--- a/src/box/vy_history.h
+++ b/src/box/vy_history.h
@@ -42,6 +42,7 @@
 extern "C" {
 #endif /* defined(__cplusplus) */
 
+struct mempool;
 struct key_def;
 struct tuple;
 struct tuple_format;
@@ -53,6 +54,8 @@ struct vy_history {
 	 * Linked by vy_history_node::link.
 	 */
 	struct rlist stmts;
+	/** Memory pool for vy_history_node allocations. */
+	struct mempool *pool;
 };
 
 /** Key history node. */
@@ -78,11 +81,13 @@ struct vy_history_node {
 };
 
 /**
- * Initialize a history list.
+ * Initialize a history list. The 'pool' argument specifies
+ * the memory pool to use for node allocations.
  */
 static inline void
-vy_history_create(struct vy_history *history)
+vy_history_create(struct vy_history *history, struct mempool *pool)
 {
+	history->pool = pool;
 	rlist_create(&history->stmts);
 }
 
diff --git a/src/box/vy_lsm.c b/src/box/vy_lsm.c
index 73196b10..4dfc0a25 100644
--- a/src/box/vy_lsm.c
+++ b/src/box/vy_lsm.c
@@ -35,6 +35,7 @@
 #include <stddef.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <small/mempool.h>
 
 #include "diag.h"
 #include "errcode.h"
@@ -50,6 +51,7 @@
 #include "vy_stat.h"
 #include "vy_stmt.h"
 #include "vy_upsert.h"
+#include "vy_history.h"
 #include "vy_read_set.h"
 
 int
@@ -74,6 +76,8 @@ vy_lsm_env_create(struct vy_lsm_env *env, const char *path,
 	env->upsert_thresh_arg = upsert_thresh_arg;
 	env->too_long_threshold = TIMEOUT_INFINITY;
 	env->lsm_count = 0;
+	mempool_create(&env->history_node_pool, cord_slab_cache(),
+		       sizeof(struct vy_history_node));
 	return 0;
 }
 
@@ -82,6 +86,7 @@ vy_lsm_env_destroy(struct vy_lsm_env *env)
 {
 	tuple_unref(env->empty_key);
 	tuple_format_unref(env->key_format);
+	mempool_destroy(&env->history_node_pool);
 }
 
 const char *
diff --git a/src/box/vy_lsm.h b/src/box/vy_lsm.h
index 2ef190ba..c7ccb7d6 100644
--- a/src/box/vy_lsm.h
+++ b/src/box/vy_lsm.h
@@ -34,7 +34,7 @@
 #include <assert.h>
 #include <stdbool.h>
 #include <stdint.h>
-
+#include <small/mempool.h>
 #include <small/rlist.h>
 
 #include "index_def.h"
@@ -90,6 +90,8 @@ struct vy_lsm_env {
 	size_t bloom_size;
 	/** Size of memory used for page index. */
 	size_t page_index_size;
+	/** Memory pool for vy_history_node allocations. */
+	struct mempool history_node_pool;
 };
 
 /** Create a common LSM tree environment. */
diff --git a/src/box/vy_point_lookup.c b/src/box/vy_point_lookup.c
index 6994ebfd..b48a2332 100644
--- a/src/box/vy_point_lookup.c
+++ b/src/box/vy_point_lookup.c
@@ -241,14 +241,13 @@ vy_point_lookup(struct vy_lsm *lsm, struct vy_tx *tx,
 	assert(tuple_field_count(key) >= lsm->cmp_def->part_count);
 
 	*ret = NULL;
-	size_t region_svp = region_used(&fiber()->gc);
 	double start_time = ev_monotonic_now(loop());
 	int rc = 0;
 
 	lsm->stat.lookup++;
 	/* History list */
 	struct vy_history history;
-	vy_history_create(&history);
+	vy_history_create(&history, &lsm->env->history_node_pool);
 restart:
 	rc = vy_point_lookup_scan_txw(lsm, tx, key, &history);
 	if (rc != 0 || vy_history_is_terminal(&history))
@@ -285,7 +284,6 @@ restart:
 		 * cannot distinguish these two cases we always restart.
 		 */
 		vy_history_cleanup(&history);
-		region_truncate(&fiber()->gc, region_svp);
 		goto restart;
 	}
 
@@ -297,7 +295,6 @@ done:
 		lsm->stat.upsert.applied += upserts_applied;
 	}
 	vy_history_cleanup(&history);
-	region_truncate(&fiber()->gc, region_svp);
 
 	if (rc != 0)
 		return -1;
-- 
2.11.0




More information about the Tarantool-patches mailing list