1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-16 15:32:43 +01:00

PosixSourceAccessor: Use concurrent_flat_map

This commit is contained in:
Eelco Dolstra 2025-07-15 19:49:07 +02:00
parent e8314e69ab
commit 8e98f62a6e

View file

@ -3,7 +3,7 @@
#include "nix/util/signals.hh"
#include "nix/util/sync.hh"
#include <unordered_map>
#include <boost/unordered/concurrent_flat_map.hpp>
namespace nix {
@ -90,23 +90,21 @@ bool PosixSourceAccessor::pathExists(const CanonPath & path)
std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & path)
{
static SharedSync<std::unordered_map<Path, std::optional<struct stat>>> _cache;
using Cache = boost::concurrent_flat_map<Path, std::optional<struct stat>>;
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<Cache::mapped_type> 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;
}