From 10d41e48b8abd6f60f3dc379a8a0b5b5eb4889b4 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 2 Mar 2019 20:23:51 +1100 Subject: Fix BLI_mempool incorrect slop-space calculation Also ensure elements fit evenly into the chunk size causing allocations to be slightly smaller in some cases. In own tests reduces overall memory use by about ~4.5% for high poly meshes in edit-mode. --- source/blender/blenlib/intern/BLI_mempool.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'source/blender/blenlib/intern/BLI_mempool.c') diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index a96d4c8b5dd..292cafecc98 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -145,7 +145,7 @@ struct BLI_mempool { #define NODE_STEP_PREV(node) ((void *)((char *)(node) - esize)) /** Extra bytes implicitly used for every chunk alloc. */ -#define CHUNK_OVERHEAD (uint)(MEM_SIZE_OVERHEAD) +#define CHUNK_OVERHEAD (uint)(MEM_SIZE_OVERHEAD + sizeof(BLI_mempool_chunk)) #ifdef USE_CHUNK_POW2 static uint power_of_2_max_u(uint x) @@ -290,18 +290,24 @@ BLI_mempool *BLI_mempool_create( pool->chunks = NULL; pool->chunk_tail = NULL; pool->esize = esize; - pool->csize = esize * pchunk; - /* Optimize chunk size to powers of 2, accounting for slop-space. */ #ifdef USE_CHUNK_POW2 { - BLI_assert(pool->csize > CHUNK_OVERHEAD); - pool->csize = power_of_2_max_u(pool->csize) - CHUNK_OVERHEAD; - pchunk = pool->csize / esize; + BLI_assert(power_of_2_max_u(pchunk * esize) > CHUNK_OVERHEAD); + pchunk = (power_of_2_max_u(pchunk * esize) - CHUNK_OVERHEAD) / esize; } #endif + pool->csize = esize * pchunk; + + /* Ensure this is a power of 2, minus the rounding by element size. */ +#ifdef USE_CHUNK_POW2 + { + uint final_size = (uint)MEM_SIZE_OVERHEAD + (uint)sizeof(BLI_mempool_chunk) + pool->csize; + BLI_assert(((uint)power_of_2_max_u(final_size) - final_size) < pool->esize); + } +#endif pool->pchunk = pchunk; pool->flag = flag; -- cgit v1.2.3