From 528c1ac6e2e51cd6d87193da8666fc9a437858c5 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Tue, 8 Sep 2026 15:50:16 -0700 Subject: [PATCH] Memory pool: cached slot index in a page Previously, freeing a chunk searched the pool slots linearly to map the chunk size back to its slot. The fix is to keep the slot index in the page descriptor, in a byte that was unused. --- src/njs_mp.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/njs_mp.c b/src/njs_mp.c index f39fb711..7382c796 100644 --- a/src/njs_mp.c +++ b/src/njs_mp.c @@ -44,7 +44,8 @@ typedef struct { /* Number of free chunks of a chunked page. */ uint8_t chunks; - uint8_t _unused; + /* Index of the pool slot the chunks of the page belong to. */ + uint8_t slot; /* Chunk bitmap. There can be no more than 32 chunks in a page. */ uint8_t map[4]; @@ -464,6 +465,7 @@ njs_mp_alloc_small(njs_mp_t *mp, size_t size) /* slot->chunks are already one less. */ page->chunks = slot->chunks; page->size = size >> mp->chunk_size_shift; + page->slot = slot - mp->slots; p = njs_mp_page_addr(mp, page); } @@ -812,8 +814,9 @@ njs_mp_chunk_free(njs_mp_t *mp, njs_mp_block_t *cluster, njs_mp_chunk_set_free(page->map, chunk); - /* Find a slot with appropriate chunk size. */ - for (slot = mp->slots; slot->size < size; slot++) { /* void */ } + slot = &mp->slots[page->slot]; + + njs_assert(slot->size == size); if (page->chunks != slot->chunks) { page->chunks++; -- 2.47.3