[Dnsmasq-discuss] [PATCH 2/2] Skip the full cache scan while no entry can have expired.
Dominik Derigs
git at dl6er.de
Fri Oct 30 06:34:45 UTC 2026
From: DL6ER <dl6er at dl6er.de>
When the cache is full and its least-recently-used entry is still
live, really_insert() walks every hash chain for expired entries
before it evicts that entry. In a full cache of unexpired entries, the
normal state with use-stale-cache, the walk frees nothing and is
repeated for the next insert. Every walk visits every cached entry, so
the cost of a miss grows with cache-size.
Keep a lower bound on the time at which the next entry can expire,
lowered whenever an entry is hashed and recomputed by every full scan,
and skip the scan until that time has come. The scan only frees entries
for which is_expired() is true, so a skipped scan could not have freed
anything.
Measured with the dnsmasq embedded in Pi-hole FTL on x86, a full cache
of unexpired entries and one new name at a time, instructions spent in
really_insert() per miss:
cache-size=10000: 365k -> 5.3k
cache-size=100000: 3.65M -> 3.8k
With one new name every 700 ms, a miss took 1.2-1.5 ms before and
0.3-0.5 ms after with 10000 entries, and 7-8.6 ms before and 0.45-0.6 ms
after with 100000 entries.
Signed-off-by: DL6ER <dl6er at dl6er.de>
---
src/cache.c | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/src/cache.c b/src/cache.c
index ad4936b..3b90573 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -22,6 +22,7 @@ static struct crec *new_chain = NULL;
static int insert_error;
static union bigname *big_free = NULL;
static int bignames_left, hash_size;
+static time_t next_expiry; /* no entry can expire before this, 0 if none can */
struct nameblock {
struct nameblock *next;
@@ -331,6 +332,25 @@ static struct crec **hash_bucket(char *name)
return hash_table + ((val ^ (val >> 16)) & (hash_size - 1));
}
+/* Lower next_expiry to the time at which is_expired() can become true. */
+static void note_expiry(struct crec *crecp)
+{
+ time_t expiry = crecp->ttd;
+
+ if (crecp->flags & F_IMMORTAL)
+ return;
+
+ if (daemon->cache_max_expiry != 0 && !(crecp->flags & (F_DS | F_DNSKEY)))
+ {
+ if (daemon->cache_max_expiry == -1)
+ return;
+ expiry += daemon->cache_max_expiry;
+ }
+
+ if (next_expiry == 0 || difftime(expiry, next_expiry) < 0)
+ next_expiry = expiry;
+}
+
static void cache_hash(struct crec *crecp)
{
/* maintain an invariant that all entries with F_REVERSE set
@@ -361,6 +381,7 @@ static void cache_hash(struct crec *crecp)
crecp->hash_next = *up;
*up = crecp;
+ note_expiry(crecp);
}
static void cache_blockdata_free(struct crec *crecp)
@@ -632,6 +653,7 @@ static struct crec *cache_scan_free(char *name, union all_addr *addr, unsigned s
int i;
int addrlen = (flags & F_IPV6) ? IN6ADDRSZ : INADDRSZ;
+ next_expiry = 0;
for (i = 0; i < hash_size; i++)
for (crecp = hash_table[i], up = &hash_table[i];
crecp && ((crecp->flags & F_REVERSE) || !(crecp->flags & F_IMMORTAL));
@@ -655,7 +677,10 @@ static struct crec *cache_scan_free(char *name, union all_addr *addr, unsigned s
cache_free(crecp);
}
else
- up = &crecp->hash_next;
+ {
+ note_expiry(crecp);
+ up = &crecp->hash_next;
+ }
}
return NULL;
@@ -799,7 +824,9 @@ static struct crec *really_insert(char *name, union all_addr *addr, unsigned sho
}
else
{
- cache_scan_free(NULL, NULL, class, now, 0, NULL, NULL);
+ /* The scan cannot free anything before next_expiry. */
+ if (next_expiry != 0 && difftime(now, next_expiry) >= 0)
+ cache_scan_free(NULL, NULL, class, now, 0, NULL, NULL);
freed_all = 1;
}
}
--
2.53.0
More information about the Dnsmasq-discuss
mailing list