1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 14:51:16 +02:00

Cache result of Input::getFingerprint()

The fingerprint calculation can be expensive (especially for dirty Git
trees) so we need to cache it.
This commit is contained in:
Eelco Dolstra 2024-12-04 13:17:31 +01:00
parent d044a05197
commit f469bc2ae4
2 changed files with 16 additions and 3 deletions

View file

@ -113,7 +113,15 @@ Input Input::fromAttrs(const Settings & settings, Attrs && attrs)
std::optional<std::string> Input::getFingerprint(ref<Store> store) const std::optional<std::string> Input::getFingerprint(ref<Store> store) const
{ {
return scheme ? scheme->getFingerprint(store, *this) : std::nullopt; if (!scheme) return std::nullopt;
if (cachedFingerprint) return *cachedFingerprint;
auto fingerprint = scheme->getFingerprint(store, *this);
cachedFingerprint = fingerprint;
return fingerprint;
} }
ParsedURL Input::toURL() const ParsedURL Input::toURL() const
@ -307,7 +315,7 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
auto accessor = makeStorePathAccessor(store, storePath); auto accessor = makeStorePathAccessor(store, storePath);
accessor->fingerprint = scheme->getFingerprint(store, *this); accessor->fingerprint = getFingerprint(store);
return {accessor, *this}; return {accessor, *this};
} catch (Error & e) { } catch (Error & e) {
@ -318,7 +326,7 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
auto [accessor, result] = scheme->getAccessor(store, *this); auto [accessor, result] = scheme->getAccessor(store, *this);
assert(!accessor->fingerprint); assert(!accessor->fingerprint);
accessor->fingerprint = scheme->getFingerprint(store, result); accessor->fingerprint = result.getFingerprint(store);
return {accessor, std::move(result)}; return {accessor, std::move(result)};
} }

View file

@ -46,6 +46,11 @@ struct Input
*/ */
std::optional<Path> parent; std::optional<Path> parent;
/**
* Cached result of getFingerprint().
*/
mutable std::optional<std::optional<std::string>> cachedFingerprint;
public: public:
/** /**
* Create an `Input` from a URL. * Create an `Input` from a URL.