mirror of
https://github.com/NixOS/nix
synced 2025-06-24 22:11:15 +02:00
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.
This commit is contained in:
parent
90d70aa4c9
commit
cd61e922ff
2 changed files with 13 additions and 10 deletions
|
@ -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<std::shared_ptr<const ValidPathInfo>> 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<std::shared_ptr<const ValidPathInfo>> 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)) {
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace nix {
|
|||
/**
|
||||
* A simple least-recently used cache. Not thread-safe.
|
||||
*/
|
||||
template<typename Key, typename Value>
|
||||
template<typename Key, typename Value, typename Compare = std::less<>>
|
||||
class LRUCache
|
||||
{
|
||||
private:
|
||||
|
@ -22,7 +22,7 @@ private:
|
|||
// and LRU.
|
||||
struct LRUIterator;
|
||||
|
||||
using Data = std::map<Key, std::pair<LRUIterator, Value>>;
|
||||
using Data = std::map<Key, std::pair<LRUIterator, Value>, Compare>;
|
||||
using LRU = std::list<typename Data::iterator>;
|
||||
|
||||
struct LRUIterator
|
||||
|
@ -43,7 +43,8 @@ public:
|
|||
/**
|
||||
* Insert or upsert an item in the cache.
|
||||
*/
|
||||
void upsert(const Key & key, const Value & value)
|
||||
template<typename K>
|
||||
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<typename K>
|
||||
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<Value> get(const Key & key)
|
||||
template<typename K>
|
||||
std::optional<Value> get(const K & key)
|
||||
{
|
||||
auto i = data.find(key);
|
||||
if (i == data.end())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue