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

Reapply "Merge pull request #13938 from NixOS/import-thunk"

This reverts commit fd034814dc.
This commit is contained in:
Eelco Dolstra 2025-09-22 11:48:58 +02:00
parent ab7feb3898
commit 5f60602875
9 changed files with 135 additions and 88 deletions

View file

@ -8,6 +8,8 @@
#include <set>
#include <vector>
#include <boost/container_hash/hash.hpp>
namespace nix {
/**
@ -258,11 +260,17 @@ public:
*/
std::string makeRelative(const CanonPath & path) const;
friend struct std::hash<CanonPath>;
friend std::size_t hash_value(const CanonPath &);
};
std::ostream & operator<<(std::ostream & stream, const CanonPath & path);
inline std::size_t hash_value(const CanonPath & path)
{
boost::hash<std::string_view> hasher;
return hasher(path.path);
}
} // namespace nix
template<>
@ -270,8 +278,8 @@ struct std::hash<nix::CanonPath>
{
using is_avalanching = std::true_type;
std::size_t operator()(const nix::CanonPath & s) const noexcept
std::size_t operator()(const nix::CanonPath & path) const noexcept
{
return std::hash<std::string>{}(s.path);
return nix::hash_value(path);
}
};

View file

@ -119,15 +119,23 @@ struct SourcePath
std::ostream & operator<<(std::ostream & str, const SourcePath & path);
inline std::size_t hash_value(const SourcePath & path)
{
std::size_t hash = 0;
boost::hash_combine(hash, path.accessor->number);
boost::hash_combine(hash, path.path);
return hash;
}
} // namespace nix
template<>
struct std::hash<nix::SourcePath>
{
using is_avalanching = std::true_type;
std::size_t operator()(const nix::SourcePath & s) const noexcept
{
std::size_t hash = 0;
hash_combine(hash, s.accessor->number, s.path);
return hash;
return nix::hash_value(s);
}
};

View file

@ -220,6 +220,17 @@ typename T::mapped_type * get(T & map, const K & key)
template<class T, typename K>
typename T::mapped_type * get(T && map, const K & key) = delete;
/**
* Look up a value in a `boost::concurrent_flat_map`.
*/
template<class T>
std::optional<typename T::mapped_type> getConcurrent(const T & map, const typename T::key_type & key)
{
std::optional<typename T::mapped_type> 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.
*/

View file

@ -95,9 +95,7 @@ std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & pa
// former is not hashable on libc++.
Path absPath = makeAbsPath(path).string();
std::optional<Cache::mapped_type> 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());