From 313796a4f6d642dfdc75a99d2f3934e656938330 Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Fri, 25 Oct 2019 09:11:06 -0700 Subject: [PATCH] eina: introduce a simple malloc near implementation for Chained Mempool. This actually seems to significantly and reliably improve speed result with expedite in a higher way than I expected (~10%) which is neat, I guess. Reviewed-by: Mike Blumenkrantz Differential Revision: https://phab.enlightenment.org/D10537 --- .../mp/chained_pool/eina_chained_mempool.c | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/modules/eina/mp/chained_pool/eina_chained_mempool.c b/src/modules/eina/mp/chained_pool/eina_chained_mempool.c index 1917e07921..261b02c200 100644 --- a/src/modules/eina/mp/chained_pool/eina_chained_mempool.c +++ b/src/modules/eina/mp/chained_pool/eina_chained_mempool.c @@ -371,6 +371,56 @@ eina_chained_mempool_free(void *data, void *ptr) return; } +static void * +eina_chained_mempool_malloc_near(void *data, + void *after, void *before, + unsigned int size EINA_UNUSED) +{ + Chained_Mempool *pool = data; + Chained_Pool *p = NULL; + void *mem = NULL; + + if (!eina_spinlock_take(&pool->mutex)) + { +#ifdef EINA_HAVE_DEBUG_THREADS + assert(eina_thread_equal(pool->self, eina_thread_self())); +#endif + } + + if (after) + { + Eina_Rbtree *r = eina_rbtree_inline_lookup(pool->root, after, + 0, _eina_chained_mp_pool_key_cmp, NULL); + + if (r) + { + p = EINA_RBTREE_CONTAINER_GET(r, Chained_Pool); + + if (!p->base && !p->last) + p = NULL; + } + } + + if (before && p == NULL) + { + Eina_Rbtree *r = eina_rbtree_inline_lookup(pool->root, before, + 0, _eina_chained_mp_pool_key_cmp, NULL); + if (r) + { + p = EINA_RBTREE_CONTAINER_GET(r, Chained_Pool); + if (!p->base && !p->last) + p = NULL; + } + } + + if (p) mem = _eina_chained_mempool_alloc_in(pool, p); + + eina_spinlock_release(&pool->mutex); + + if (!mem) return eina_chained_mempool_malloc(pool, size); + return mem; +} + static Eina_Bool eina_chained_mempool_from(void *data, void *ptr) { @@ -727,7 +777,7 @@ static Eina_Mempool_Backend _eina_chained_mp_backend = { &eina_chained_mempool_repack, &eina_chained_mempool_from, &eina_chained_mempool_iterator_new, - NULL + &eina_chained_mempool_malloc_near }; Eina_Bool chained_init(void)