From 7f9b5226af81607fac499807140632b4a59e598e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 7 Sep 2025 16:54:39 +0200 Subject: [PATCH] Add getConcurrent helper function --- src/libutil/include/nix/util/util.hh | 11 +++++++++++ src/libutil/posix-source-accessor.cc | 4 +--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/libutil/include/nix/util/util.hh b/src/libutil/include/nix/util/util.hh index 561550c41..a20305a6f 100644 --- a/src/libutil/include/nix/util/util.hh +++ b/src/libutil/include/nix/util/util.hh @@ -218,6 +218,17 @@ typename T::mapped_type * get(T & map, K & key) template typename T::mapped_type * get(T && map, const typename T::key_type & key) = delete; +/** + * Look up a value in a `boost::concurrent_flat_map`. + */ +template +std::optional getConcurrent(const T & map, const typename T::key_type & key) +{ + std::optional res; + map.cvisit(key, [&](auto & x) { res = x.second; }); + return res; +} + /** * Get a value for the specified key from an associate container, or a default value if the key isn't present. */ diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 877c63331..c524f3e4f 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -95,9 +95,7 @@ std::optional PosixSourceAccessor::cachedLstat(const CanonPath & pa // former is not hashable on libc++. Path absPath = makeAbsPath(path).string(); - std::optional res; - cache.cvisit(absPath, [&](auto & x) { res.emplace(x.second); }); - if (res) + if (auto res = getConcurrent(cache, absPath)) return *res; auto st = nix::maybeLstat(absPath.c_str());