Reuse eval caches and related values when possible

This commit is contained in:
Guillaume Maudoux 2024-04-19 16:21:51 +02:00
parent aa165301d1
commit a60a1f09b2
2 changed files with 23 additions and 7 deletions

View file

@ -444,12 +444,10 @@ ref<eval_cache::EvalCache> openEvalCache(
std::shared_ptr<flake::LockedFlake> lockedFlake) std::shared_ptr<flake::LockedFlake> lockedFlake)
{ {
auto fingerprint = lockedFlake->getFingerprint(state.store); auto fingerprint = lockedFlake->getFingerprint(state.store);
return make_ref<nix::eval_cache::EvalCache>( auto hash = evalSettings.useEvalCache && evalSettings.pureEval
evalSettings.useEvalCache && evalSettings.pureEval ? fingerprint
? fingerprint : std::nullopt;
: std::nullopt, auto rootLoader = [&state, lockedFlake]()
state,
[&state, lockedFlake]()
{ {
/* For testing whether the evaluation cache is /* For testing whether the evaluation cache is
complete. */ complete. */
@ -465,7 +463,17 @@ ref<eval_cache::EvalCache> openEvalCache(
assert(aOutputs); assert(aOutputs);
return aOutputs->value; return aOutputs->value;
}); };
if (hash) {
auto search = state.evalCaches.find(hash.value());
if (search == state.evalCaches.end()) {
search = state.evalCaches.emplace(hash.value(), make_ref<nix::eval_cache::EvalCache>(hash, state, rootLoader)).first;
}
return search->second;
} else {
return make_ref<nix::eval_cache::EvalCache>(hash, state, rootLoader);
}
} }
Installables SourceExprCommand::parseInstallables( Installables SourceExprCommand::parseInstallables(

View file

@ -34,6 +34,9 @@ class StorePath;
struct SingleDerivedPath; struct SingleDerivedPath;
enum RepairFlag : bool; enum RepairFlag : bool;
struct MemoryInputAccessor; struct MemoryInputAccessor;
namespace eval_cache {
class EvalCache;
}
/** /**
@ -282,6 +285,11 @@ public:
return *new EvalErrorBuilder<T>(*this, args...); return *new EvalErrorBuilder<T>(*this, args...);
} }
/**
* A cache for evaluation caches, so as to reuse the same root value if possible
*/
std::map<const Hash, ref<eval_cache::EvalCache>> evalCaches;
private: private:
/* Cache for calls to addToStore(); maps source paths to the store /* Cache for calls to addToStore(); maps source paths to the store