mem-pool: only search head block for available space
authorJameson Miller <jamill@microsoft.com>
Mon, 2 Jul 2018 19:49:33 +0000 (19:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Jul 2018 17:58:27 +0000 (10:58 -0700)
Instead of searching all memory blocks for available space to fulfill
a memory request, only search the head block. If the head block does
not have space, assume that previous block would most likely not be
able to fulfill request either. This could potentially lead to more
memory fragmentation, but also avoids searching memory blocks that
probably will not be able to fulfill request.

This pattern will benefit consumers that are able to generate a good
estimate for how much memory will be needed, or if they are performing
fixed sized allocations, so that once a block is exhausted it will
never be able to fulfill a future request.

Signed-off-by: Jameson Miller <jamill@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
mem-pool.c
index 389d7af447036ef58e64d2041a8c6743a862817a..c80124f1fe12a1f8fb9478048b12d1fa5fcda944 100644 (file)
@@ -21,16 +21,16 @@ static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t b
 
 void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
 {
-       struct mp_block *p;
+       struct mp_block *p = NULL;
        void *r;
 
        /* round up to a 'uintmax_t' alignment */
        if (len & (sizeof(uintmax_t) - 1))
                len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
 
-       for (p = mem_pool->mp_block; p; p = p->next_block)
-               if (p->end - p->next_free >= len)
-                       break;
+       if (mem_pool->mp_block &&
+           mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len)
+               p = mem_pool->mp_block;
 
        if (!p) {
                if (len >= (mem_pool->block_alloc / 2)) {