From cd61e922ff312eefe0be55b7b468b83117f94181 Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Wed, 14 May 2025 21:42:35 +0000 Subject: [PATCH] libutil: Use heterogeneous lookup for LRUCache This gets rid of some ugly std::string_view -> std::string conversions, which are an eye-sore and lead to extra copying. --- src/libstore/store-api.cc | 10 +++++----- src/libutil/include/nix/util/lru-cache.hh | 13 ++++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 08c542041..f20d2d329 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -570,7 +570,7 @@ bool Store::isValidPath(const StorePath & storePath) { { auto state_(state.lock()); - auto res = state_->pathInfoCache.get(std::string(storePath.to_string())); + auto res = state_->pathInfoCache.get(storePath.to_string()); if (res && res->isKnownNow()) { stats.narInfoReadAverted++; return res->didExist(); @@ -582,7 +582,7 @@ bool Store::isValidPath(const StorePath & storePath) if (res.first != NarInfoDiskCache::oUnknown) { stats.narInfoReadAverted++; auto state_(state.lock()); - state_->pathInfoCache.upsert(std::string(storePath.to_string()), + state_->pathInfoCache.upsert(storePath.to_string(), res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue { .value = res.second }); return res.first == NarInfoDiskCache::oValid; } @@ -641,7 +641,7 @@ std::optional> Store::queryPathInfoFromClie auto hashPart = std::string(storePath.hashPart()); { - auto res = state.lock()->pathInfoCache.get(std::string(storePath.to_string())); + auto res = state.lock()->pathInfoCache.get(storePath.to_string()); if (res && res->isKnownNow()) { stats.narInfoReadAverted++; if (res->didExist()) @@ -657,7 +657,7 @@ std::optional> Store::queryPathInfoFromClie stats.narInfoReadAverted++; { auto state_(state.lock()); - state_->pathInfoCache.upsert(std::string(storePath.to_string()), + state_->pathInfoCache.upsert(storePath.to_string(), res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second }); if (res.first == NarInfoDiskCache::oInvalid || !goodStorePath(storePath, res.second->path)) @@ -701,7 +701,7 @@ void Store::queryPathInfo(const StorePath & storePath, { auto state_(state.lock()); - state_->pathInfoCache.upsert(std::string(storePath.to_string()), PathInfoCacheValue { .value = info }); + state_->pathInfoCache.upsert(storePath.to_string(), PathInfoCacheValue { .value = info }); } if (!info || !goodStorePath(storePath, info->path)) { diff --git a/src/libutil/include/nix/util/lru-cache.hh b/src/libutil/include/nix/util/lru-cache.hh index 5fa3b34a6..20891a086 100644 --- a/src/libutil/include/nix/util/lru-cache.hh +++ b/src/libutil/include/nix/util/lru-cache.hh @@ -11,7 +11,7 @@ namespace nix { /** * A simple least-recently used cache. Not thread-safe. */ -template +template> class LRUCache { private: @@ -22,7 +22,7 @@ private: // and LRU. struct LRUIterator; - using Data = std::map>; + using Data = std::map, Compare>; using LRU = std::list; struct LRUIterator @@ -43,7 +43,8 @@ public: /** * Insert or upsert an item in the cache. */ - void upsert(const Key & key, const Value & value) + template + void upsert(const K & key, const Value & value) { if (capacity == 0) return; @@ -68,7 +69,8 @@ public: i->second.first.it = j; } - bool erase(const Key & key) + template + bool erase(const K & key) { auto i = data.find(key); if (i == data.end()) @@ -82,7 +84,8 @@ public: * Look up an item in the cache. If it exists, it becomes the most * recently used item. * */ - std::optional get(const Key & key) + template + std::optional get(const K & key) { auto i = data.find(key); if (i == data.end())