Skip to content

PosixSourceAccessor: Use concurrent_flat_map #13478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/libutil/posix-source-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
Copy link
Contributor

@xokdvium xokdvium Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why this cache is a global? I was really confused by this when reading through the code some time prior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is suspicious, when there can be different ones of these with different roots

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason it shouldn't be global? There can be any number of instances of PosixSourceAccessor, but they all represent the same filesystem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they don't, because of the root field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but the root field is included in the cache keys.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. OK that does work, but I would still prefer less global state. It is just bad luck for things like forking, etc.


// 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;
}
Expand Down
Loading