Skip to content

Commit cbdb497

Browse files
authored
Merge pull request #13478 from NixOS/posix-source-accessor-concurrent-map
PosixSourceAccessor: Use concurrent_flat_map
2 parents 3543a73 + 3a67caf commit cbdb497

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/libutil/posix-source-accessor.cc

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "nix/util/signals.hh"
44
#include "nix/util/sync.hh"
55

6-
#include <unordered_map>
6+
#include <boost/unordered/concurrent_flat_map.hpp>
77

88
namespace nix {
99

@@ -88,25 +88,23 @@ bool PosixSourceAccessor::pathExists(const CanonPath & path)
8888

8989
std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & path)
9090
{
91-
static SharedSync<std::unordered_map<Path, std::optional<struct stat>>> _cache;
91+
using Cache = boost::concurrent_flat_map<Path, std::optional<struct stat>>;
92+
static Cache cache;
9293

9394
// Note: we convert std::filesystem::path to Path because the
9495
// former is not hashable on libc++.
9596
Path absPath = makeAbsPath(path).string();
9697

97-
{
98-
auto cache(_cache.readLock());
99-
auto i = cache->find(absPath);
100-
if (i != cache->end())
101-
return i->second;
102-
}
98+
std::optional<Cache::mapped_type> res;
99+
cache.cvisit(absPath, [&](auto & x) { res.emplace(x.second); });
100+
if (res)
101+
return *res;
103102

104103
auto st = nix::maybeLstat(absPath.c_str());
105104

106-
auto cache(_cache.lock());
107-
if (cache->size() >= 16384)
108-
cache->clear();
109-
cache->emplace(absPath, st);
105+
if (cache.size() >= 16384)
106+
cache.clear();
107+
cache.emplace(absPath, st);
110108

111109
return st;
112110
}

0 commit comments

Comments
 (0)