Merge pull request #11092 from DeterminateSystems/hash-SourcePath

Use std::unordered_map for the EvalState caches
This commit is contained in:
Eelco Dolstra 2024-07-16 11:06:43 +02:00 committed by GitHub
commit 9c6678da0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 8 deletions

View file

@ -1061,7 +1061,7 @@ void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial)
if (!e) if (!e)
e = parseExprFromFile(resolvedPath); e = parseExprFromFile(resolvedPath);
fileParseCache[resolvedPath] = e; fileParseCache.emplace(resolvedPath, e);
try { try {
auto dts = debugRepl auto dts = debugRepl
@ -1084,8 +1084,8 @@ void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial)
throw; throw;
} }
fileEvalCache[resolvedPath] = v; fileEvalCache.emplace(resolvedPath, v);
if (path != resolvedPath) fileEvalCache[path] = v; if (path != resolvedPath) fileEvalCache.emplace(path, v);
} }

View file

@ -309,15 +309,15 @@ private:
/* Cache for calls to addToStore(); maps source paths to the store /* Cache for calls to addToStore(); maps source paths to the store
paths. */ paths. */
Sync<std::map<SourcePath, StorePath>> srcToStore; Sync<std::unordered_map<SourcePath, StorePath>> srcToStore;
/** /**
* A cache from path names to parse trees. * A cache from path names to parse trees.
*/ */
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::map<SourcePath, Expr *, std::less<SourcePath>, traceable_allocator<std::pair<const SourcePath, Expr *>>> FileParseCache; typedef std::unordered_map<SourcePath, Expr *, std::hash<SourcePath>, std::equal_to<SourcePath>, traceable_allocator<std::pair<const SourcePath, Expr *>>> FileParseCache;
#else #else
typedef std::map<SourcePath, Expr *> FileParseCache; typedef std::unordered_map<SourcePath, Expr *> FileParseCache;
#endif #endif
FileParseCache fileParseCache; FileParseCache fileParseCache;
@ -325,9 +325,9 @@ private:
* A cache from path names to values. * A cache from path names to values.
*/ */
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::map<SourcePath, Value, std::less<SourcePath>, traceable_allocator<std::pair<const SourcePath, Value>>> FileEvalCache; typedef std::unordered_map<SourcePath, Value, std::hash<SourcePath>, std::equal_to<SourcePath>, traceable_allocator<std::pair<const SourcePath, Value>>> FileEvalCache;
#else #else
typedef std::map<SourcePath, Value> FileEvalCache; typedef std::unordered_map<SourcePath, Value> FileEvalCache;
#endif #endif
FileEvalCache fileEvalCache; FileEvalCache fileEvalCache;

View file

@ -9,6 +9,8 @@
#include "canon-path.hh" #include "canon-path.hh"
#include "source-accessor.hh" #include "source-accessor.hh"
#include <boost/functional/hash.hpp> // for boost::hash_combine
namespace nix { namespace nix {
/** /**
@ -114,8 +116,21 @@ struct SourcePath
{ {
return {accessor, accessor->resolveSymlinks(path, mode)}; return {accessor, accessor->resolveSymlinks(path, mode)};
} }
friend class std::hash<nix::SourcePath>;
}; };
std::ostream & operator << (std::ostream & str, const SourcePath & path); std::ostream & operator << (std::ostream & str, const SourcePath & path);
} }
template<>
struct std::hash<nix::SourcePath>
{
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;
}
};

View file

@ -375,4 +375,18 @@ inline std::string operator + (std::string_view s1, const char * s2)
return s; return s;
} }
/**
* hash_combine() from Boost. Hash several hashable values together
* into a single hash.
*/
inline void hash_combine(std::size_t & seed) { }
template <typename T, typename... Rest>
inline void hash_combine(std::size_t & seed, const T & v, Rest... rest)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
hash_combine(seed, rest...);
}
} }