From e099a5bc678a7bba0b2c99fbe667c08d4a7cc0f7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 14 Apr 2025 14:29:14 +0200 Subject: [PATCH] Move the InputCache to EvalState --- src/libcmd/repl.cc | 3 --- src/libexpr/eval.cc | 3 +++ src/libexpr/include/nix/expr/eval.hh | 7 ++++++- src/libexpr/primops/fetchTree.cc | 2 +- src/libfetchers/include/nix/fetchers/input-cache.hh | 2 +- src/libfetchers/input-cache.cc | 5 ++--- src/libflake/flake/flake.cc | 6 +++--- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 3805942ce..c5a95268b 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -28,7 +28,6 @@ #include "nix/expr/print.hh" #include "nix/util/ref.hh" #include "nix/expr/value.hh" -#include "nix/fetchers/input-cache.hh" #include "nix/util/strings.hh" @@ -459,7 +458,6 @@ ProcessLineResult NixRepl::processLine(std::string line) else if (command == ":l" || command == ":load") { state->resetFileCache(); - fetchers::InputCache::getCache()->clear(); loadFile(arg); } @@ -469,7 +467,6 @@ ProcessLineResult NixRepl::processLine(std::string line) else if (command == ":r" || command == ":reload") { state->resetFileCache(); - fetchers::InputCache::getCache()->clear(); reloadFiles(); } diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index d6e01c028..0212162dd 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -20,6 +20,7 @@ #include "nix/util/url.hh" #include "nix/fetchers/fetch-to-store.hh" #include "nix/fetchers/tarball.hh" +#include "nix/fetchers/input-cache.hh" #include "parser-tab.hh" @@ -290,6 +291,7 @@ EvalState::EvalState( )} , store(store) , buildStore(buildStore ? buildStore : store) + , inputCache(fetchers::InputCache::create()) , debugRepl(nullptr) , debugStop(false) , trylevel(0) @@ -1132,6 +1134,7 @@ void EvalState::resetFileCache() { fileEvalCache.clear(); fileParseCache.clear(); + inputCache->clear(); } diff --git a/src/libexpr/include/nix/expr/eval.hh b/src/libexpr/include/nix/expr/eval.hh index 056fd98d3..505a7d1e7 100644 --- a/src/libexpr/include/nix/expr/eval.hh +++ b/src/libexpr/include/nix/expr/eval.hh @@ -33,7 +33,10 @@ namespace nix { constexpr size_t maxPrimOpArity = 8; class Store; -namespace fetchers { struct Settings; } +namespace fetchers { +struct Settings; +struct InputCache; +} struct EvalSettings; class EvalState; class StorePath; @@ -301,6 +304,8 @@ public: RootValue vImportedDrvToDerivation = nullptr; + ref inputCache; + /** * Debugger */ diff --git a/src/libexpr/primops/fetchTree.cc b/src/libexpr/primops/fetchTree.cc index c5cb70b44..5d41d65c1 100644 --- a/src/libexpr/primops/fetchTree.cc +++ b/src/libexpr/primops/fetchTree.cc @@ -202,7 +202,7 @@ static void fetchTree( throw Error("input '%s' is not allowed to use the '__final' attribute", input.to_string()); } - auto cachedInput = fetchers::InputCache::getCache()->getAccessor(state.store, input, false); + auto cachedInput = state.inputCache->getAccessor(state.store, input, false); auto storePath = StorePath::random(input.getName()); diff --git a/src/libfetchers/include/nix/fetchers/input-cache.hh b/src/libfetchers/include/nix/fetchers/input-cache.hh index 6a7194741..a7ca34487 100644 --- a/src/libfetchers/include/nix/fetchers/input-cache.hh +++ b/src/libfetchers/include/nix/fetchers/input-cache.hh @@ -25,7 +25,7 @@ struct InputCache virtual void clear() = 0; - static ref getCache(); + static ref create(); }; } diff --git a/src/libfetchers/input-cache.cc b/src/libfetchers/input-cache.cc index 6772d67c7..716143899 100644 --- a/src/libfetchers/input-cache.cc +++ b/src/libfetchers/input-cache.cc @@ -72,10 +72,9 @@ struct InputCacheImpl : InputCache } }; -ref InputCache::getCache() +ref InputCache::create() { - static auto cache = make_ref(); - return cache; + return make_ref(); } } diff --git a/src/libflake/flake/flake.cc b/src/libflake/flake/flake.cc index 34eab755a..299a74640 100644 --- a/src/libflake/flake/flake.cc +++ b/src/libflake/flake/flake.cc @@ -353,7 +353,7 @@ static Flake getFlake( CopyMode copyMode) { // Fetch a lazy tree first. - auto cachedInput = fetchers::InputCache::getCache()->getAccessor(state.store, originalRef.input, useRegistries); + auto cachedInput = state.inputCache->getAccessor(state.store, originalRef.input, useRegistries); auto resolvedRef = FlakeRef(std::move(cachedInput.resolvedInput), originalRef.subdir); auto lockedRef = FlakeRef(std::move(cachedInput.lockedInput), originalRef.subdir); @@ -368,7 +368,7 @@ static Flake getFlake( debug("refetching input '%s' due to self attribute", newLockedRef); // FIXME: need to remove attrs that are invalidated by the changed input attrs, such as 'narHash'. newLockedRef.input.attrs.erase("narHash"); - auto cachedInput2 = fetchers::InputCache::getCache()->getAccessor(state.store, newLockedRef.input, useRegistries); + auto cachedInput2 = state.inputCache->getAccessor(state.store, newLockedRef.input, useRegistries); cachedInput.accessor = cachedInput2.accessor; lockedRef = FlakeRef(std::move(cachedInput2.lockedInput), newLockedRef.subdir); } @@ -734,7 +734,7 @@ LockedFlake lockFlake( if (auto resolvedPath = resolveRelativePath()) { return {*resolvedPath, *input.ref}; } else { - auto cachedInput = fetchers::InputCache::getCache()->getAccessor(state.store, input.ref->input, useRegistries); + auto cachedInput = state.inputCache->getAccessor(state.store, input.ref->input, useRegistries); auto lockedRef = FlakeRef(std::move(cachedInput.lockedInput), input.ref->subdir);