Skip to content

Commit ab49546

Browse files
committed
Cache result of Input::getFingerprint()
The fingerprint calculation can be expensive (especially for dirty Git trees) so we need to cache it.
1 parent a799ea4 commit ab49546

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/libfetchers/fetchers.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ Input Input::fromAttrs(const Settings & settings, Attrs && attrs)
113113

114114
std::optional<std::string> Input::getFingerprint(ref<Store> store) const
115115
{
116-
return scheme ? scheme->getFingerprint(store, *this) : std::nullopt;
116+
if (!scheme) return std::nullopt;
117+
118+
if (cachedFingerprint) return *cachedFingerprint;
119+
120+
auto fingerprint = scheme->getFingerprint(store, *this);
121+
122+
cachedFingerprint = fingerprint;
123+
124+
return fingerprint;
117125
}
118126

119127
ParsedURL Input::toURL() const
@@ -307,7 +315,7 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
307315

308316
auto accessor = makeStorePathAccessor(store, storePath);
309317

310-
accessor->fingerprint = scheme->getFingerprint(store, *this);
318+
accessor->fingerprint = getFingerprint(store);
311319

312320
return {accessor, *this};
313321
} catch (Error & e) {
@@ -318,7 +326,7 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
318326
auto [accessor, result] = scheme->getAccessor(store, *this);
319327

320328
assert(!accessor->fingerprint);
321-
accessor->fingerprint = scheme->getFingerprint(store, result);
329+
accessor->fingerprint = result.getFingerprint(store);
322330

323331
return {accessor, std::move(result)};
324332
}

src/libfetchers/fetchers.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ struct Input
4646
*/
4747
std::optional<Path> parent;
4848

49+
/**
50+
* Cached result of getFingerprint().
51+
*/
52+
mutable std::optional<std::optional<std::string>> cachedFingerprint;
53+
4954
public:
5055
/**
5156
* Create an `Input` from a URL.

0 commit comments

Comments
 (0)