From baf85a8f38aaa601eb8f05aee01adb5547d40888 Mon Sep 17 00:00:00 2001 From: Simon Kelley Date: Wed, 2 Sep 2026 21:43:49 +0100 Subject: [PATCH] Tune store_name() fix in a9cb3971c695e0132f52a225b80c604ed47864f3 By only checking the last-allocated block for space, the amount off unused memory on the system is increased. This patch alters things so that the 10 last-allocated blocks are checked. This stil avoids the O(n^2) explosion but should improve memory efficiency. --- src/cache.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cache.c b/src/cache.c index e16c351..c20b738 100644 --- a/src/cache.c +++ b/src/cache.c @@ -223,14 +223,18 @@ static char *store_name(unsigned int namelen, unsigned int index) { struct nameblock *block; char *ret = NULL; + int i; if (namelen > NAMEBLOCK_CHARS) return NULL; - - if (hostblocks && hostblocks->index == index && NAMEBLOCK_CHARS - hostblocks->last >= namelen) + + /* Check the first few blocks for possible space. This allows efficient memory usage + without blowing up O(n^2) CPU usage which the linked list gets long. */ + for (i = 10, block = hostblocks; i != 0 && block; block = block->next, i--) + if (block->index == index && NAMEBLOCK_CHARS - block->last >= namelen) { - ret = &hostblocks->data[hostblocks->last]; - hostblocks->last += namelen; + ret = &block->data[block->last]; + block->last += namelen; return ret; }