mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 03:56:01 +01:00
Merge pull request #13478 from NixOS/posix-source-accessor-concurrent-map
PosixSourceAccessor: Use concurrent_flat_map
This commit is contained in:
commit
cbdb497c79
1 changed files with 10 additions and 12 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
#include "nix/util/signals.hh"
|
#include "nix/util/signals.hh"
|
||||||
#include "nix/util/sync.hh"
|
#include "nix/util/sync.hh"
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <boost/unordered/concurrent_flat_map.hpp>
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
|
@ -88,25 +88,23 @@ bool PosixSourceAccessor::pathExists(const CanonPath & path)
|
||||||
|
|
||||||
std::optional<struct stat> PosixSourceAccessor::cachedLstat(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
|
// Note: we convert std::filesystem::path to Path because the
|
||||||
// former is not hashable on libc++.
|
// former is not hashable on libc++.
|
||||||
Path absPath = makeAbsPath(path).string();
|
Path absPath = makeAbsPath(path).string();
|
||||||
|
|
||||||
{
|
std::optional<Cache::mapped_type> res;
|
||||||
auto cache(_cache.readLock());
|
cache.cvisit(absPath, [&](auto & x) { res.emplace(x.second); });
|
||||||
auto i = cache->find(absPath);
|
if (res)
|
||||||
if (i != cache->end())
|
return *res;
|
||||||
return i->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto st = nix::maybeLstat(absPath.c_str());
|
auto st = nix::maybeLstat(absPath.c_str());
|
||||||
|
|
||||||
auto cache(_cache.lock());
|
if (cache.size() >= 16384)
|
||||||
if (cache->size() >= 16384)
|
cache.clear();
|
||||||
cache->clear();
|
cache.emplace(absPath, st);
|
||||||
cache->emplace(absPath, st);
|
|
||||||
|
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue