From 9872469c75619fd857e63f31aab165e63a6dbec5 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Wed, 21 Aug 2024 11:55:35 +0200 Subject: [PATCH] Optimization for the ADDR case: stop loop if we found 2 (A and AAAA) --- pdns/recursordist/recursor_cache.cc | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pdns/recursordist/recursor_cache.cc b/pdns/recursordist/recursor_cache.cc index b487ffc337..7071b800ac 100644 --- a/pdns/recursordist/recursor_cache.cc +++ b/pdns/recursordist/recursor_cache.cc @@ -419,7 +419,7 @@ time_t MemRecursorCache::get(time_t now, const DNSName& qname, const QType qtype if (routingTag) { auto entries = getEntries(*lockedShard, qname, qtype, routingTag); - bool found = false; + unsigned int found = 0; time_t ttd{}; if (entries.first != entries.second) { @@ -436,17 +436,20 @@ time_t MemRecursorCache::get(time_t now, const DNSName& qname, const QType qtype if (!entryMatches(firstIndexIterator, qtype, requireAuth, who)) { continue; } - found = true; + ++found; handleServeStaleBookkeeping(now, serveStale, firstIndexIterator); ttd = handleHit(now, *lockedShard, firstIndexIterator, qname, origTTL, res, signatures, authorityRecs, variable, cachedState, wasAuth, fromAuthZone, fromAuthIP); - if (qtype != QType::ANY && qtype != QType::ADDR) { // normally if we have a hit, we are done + if (qtype == QType::ADDR && found == 2) { + break; + } + if (qtype != QType::ANY) { // normally if we have a hit, we are done break; } } - if (found) { + if (found > 0) { if (cachedState && ttd > now) { ptrAssign(state, *cachedState); } @@ -460,7 +463,7 @@ time_t MemRecursorCache::get(time_t now, const DNSName& qname, const QType qtype if (entries.first != entries.second) { OrderedTagIterator_t firstIndexIterator; - bool found = false; + unsigned int found = 0; time_t ttd{}; for (auto i = entries.first; i != entries.second; ++i) { @@ -475,17 +478,20 @@ time_t MemRecursorCache::get(time_t now, const DNSName& qname, const QType qtype if (!entryMatches(firstIndexIterator, qtype, requireAuth, who)) { continue; } - found = true; + ++found; handleServeStaleBookkeeping(now, serveStale, firstIndexIterator); ttd = handleHit(now, *lockedShard, firstIndexIterator, qname, origTTL, res, signatures, authorityRecs, variable, cachedState, wasAuth, fromAuthZone, fromAuthIP); - if (qtype != QType::ANY && qtype != QType::ADDR) { // normally if we have a hit, we are done + if (qtype == QType::ADDR && found == 2) { + break; + } + if (qtype != QType::ANY) { // normally if we have a hit, we are done break; } } - if (found) { + if (found > 0) { if (cachedState && ttd > now) { ptrAssign(state, *cachedState); } @@ -636,6 +642,7 @@ void MemRecursorCache::replace(time_t now, const DNSName& qname, const QType qty break; } } + if (!isNew) { moveCacheItemToBack(lockedShard->d_map, stored); } -- 2.47.2