From 8e98f62a6eb4aa868615504ff5294999d5d6108e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 15 Jul 2025 19:49:07 +0200 Subject: [PATCH] PosixSourceAccessor: Use concurrent_flat_map --- src/libutil/posix-source-accessor.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 2ce7c88e4f8..7c2d1c2967c 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 { @@ -90,23 +90,21 @@ 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; }