diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 73a08116dd5..b932f6ab5e5 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -3,7 +3,7 @@ #include "nix/util/signals.hh" #include "nix/util/sync.hh" -#include +#include namespace nix { @@ -88,25 +88,23 @@ bool PosixSourceAccessor::pathExists(const CanonPath & path) std::optional PosixSourceAccessor::cachedLstat(const CanonPath & path) { - static SharedSync>> _cache; + using Cache = boost::concurrent_flat_map>; + static Cache cache; // Note: we convert std::filesystem::path to Path because the // former is not hashable on libc++. Path absPath = makeAbsPath(path).string(); - { - auto cache(_cache.readLock()); - auto i = cache->find(absPath); - if (i != cache->end()) - return i->second; - } + std::optional res; + cache.cvisit(absPath, [&](auto & x) { res.emplace(x.second); }); + if (res) + return *res; auto st = nix::maybeLstat(absPath.c_str()); - auto cache(_cache.lock()); - if (cache->size() >= 16384) - cache->clear(); - cache->emplace(absPath, st); + if (cache.size() >= 16384) + cache.clear(); + cache.emplace(absPath, st); return st; }