From: Remi Gacogne Date: Tue, 2 Mar 2021 17:08:17 +0000 (+0100) Subject: dnsdist: Remove the useless separate entries counter from the cache X-Git-Tag: dnsdist-1.6.0-alpha2~1^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5106174f8ad118ddfc06f0f2dfc4d185ffee711a;p=thirdparty%2Fpdns.git dnsdist: Remove the useless separate entries counter from the cache Getting the size of the map is a constant-time operation and should be as accurate so let's just use that. --- diff --git a/pdns/dnsdist-cache.cc b/pdns/dnsdist-cache.cc index d27636d7b6..88128e2465 100644 --- a/pdns/dnsdist-cache.cc +++ b/pdns/dnsdist-cache.cc @@ -105,7 +105,6 @@ void DNSDistPacketCache::insertLocked(CacheShard& shard, uint32_t key, CacheValu tie(it, result) = map.insert({key, newValue}); if (result) { - shard.d_entriesCount++; return; } @@ -165,7 +164,7 @@ void DNSDistPacketCache::insert(uint32_t key, const boost::optional& su uint32_t shardIndex = getShardIndex(key); - if (d_shards.at(shardIndex).d_entriesCount >= (d_maxEntries / d_shardCount)) { + if (d_shards.at(shardIndex).d_map.size() >= (d_maxEntries / d_shardCount)) { return; } @@ -308,12 +307,12 @@ size_t DNSDistPacketCache::purgeExpired(size_t upTo, const time_t now) uint32_t shardIndex = (d_expungeIndex++ % d_shardCount); WriteLock w(&d_shards.at(shardIndex).d_lock); - if (d_shards.at(shardIndex).d_entriesCount <= maxPerShard) { + auto& map = d_shards.at(shardIndex).d_map; + if (map.size() <= maxPerShard) { continue; } - size_t toRemove = d_shards.at(shardIndex).d_entriesCount - maxPerShard; - auto& map = d_shards.at(shardIndex).d_map; + size_t toRemove = map.size() - maxPerShard; for (auto it = map.begin(); toRemove > 0 && it != map.end(); ) { const CacheValue& value = it->second; @@ -321,7 +320,6 @@ size_t DNSDistPacketCache::purgeExpired(size_t upTo, const time_t now) if (value.validity <= now) { it = map.erase(it); --toRemove; - d_shards[shardIndex].d_entriesCount--; ++removed; } else { ++it; @@ -362,13 +360,11 @@ size_t DNSDistPacketCache::expunge(size_t upTo) if (map.size() >= toRemove) { std::advance(endIt, toRemove); map.erase(beginIt, endIt); - shard.d_entriesCount -= toRemove; removed += toRemove; } else { removed += map.size(); map.clear(); - shard.d_entriesCount = 0; } } @@ -388,7 +384,6 @@ size_t DNSDistPacketCache::expungeByName(const DNSName& name, uint16_t qtype, bo if ((value.qname == name || (suffixMatch && value.qname.isPartOf(name))) && (qtype == QType::ANY || qtype == value.qtype)) { it = map.erase(it); - d_shards[shardIndex].d_entriesCount--; ++removed; } else { ++it; @@ -409,7 +404,7 @@ uint64_t DNSDistPacketCache::getSize() uint64_t count = 0; for (uint32_t shardIndex = 0; shardIndex < d_shardCount; shardIndex++) { - count += d_shards.at(shardIndex).d_entriesCount; + count += d_shards.at(shardIndex).d_map.size(); } return count; diff --git a/pdns/dnsdist-cache.hh b/pdns/dnsdist-cache.hh index c2441a631d..464217e3ed 100644 --- a/pdns/dnsdist-cache.hh +++ b/pdns/dnsdist-cache.hh @@ -104,10 +104,10 @@ private: class CacheShard { public: - CacheShard(): d_entriesCount(0) + CacheShard() { } - CacheShard(const CacheShard& old): d_entriesCount(0) + CacheShard(const CacheShard& old) { } @@ -118,7 +118,6 @@ private: std::unordered_map d_map; ReadWriteLock d_lock; - std::atomic d_entriesCount; }; bool cachedValueMatches(const CacheValue& cachedValue, uint16_t queryFlags, const DNSName& qname, uint16_t qtype, uint16_t qclass, bool tcp, bool dnssecOK, const boost::optional& subnet) const;