Skip to content

Commit 3c8b629

Browse files
committed
Store the output hashes in the initialOutputs of the drv goal
That way we 1. Don't have to recompute them several times 2. Can compute them in a place where we know the type of the parent derivation, meaning that we don't need the casting dance we had before
1 parent c9b7e0f commit 3c8b629

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/libstore/build/derivation-goal.cc

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ DerivationGoal::DerivationGoal(const StorePath & drvPath, const BasicDerivation
124124
, buildMode(buildMode)
125125
{
126126
this->drv = std::make_unique<BasicDerivation>(BasicDerivation(drv));
127+
128+
auto outputHashes = staticOutputHashes(worker.store, drv);
129+
for (auto &[outputName, outputHash] : outputHashes)
130+
initialOutputs.insert({
131+
outputName,
132+
InitialOutput{
133+
.wanted = true, // Will be refined later
134+
.outputHash = outputHash
135+
}
136+
});
137+
127138
state = &DerivationGoal::haveDerivation;
128139
name = fmt(
129140
"building of '%s' from in-memory derivation",
@@ -258,8 +269,20 @@ void DerivationGoal::loadDerivation()
258269

259270
assert(worker.store.isValidPath(drvPath));
260271

272+
auto fullDrv = new Derivation(worker.store.derivationFromPath(drvPath));
273+
274+
auto outputHashes = staticOutputHashes(worker.store, *fullDrv);
275+
for (auto &[outputName, outputHash] : outputHashes)
276+
initialOutputs.insert({
277+
outputName,
278+
InitialOutput{
279+
.wanted = true, // Will be refined later
280+
.outputHash = outputHash
281+
}
282+
});
283+
261284
/* Get the derivation. */
262-
drv = std::unique_ptr<BasicDerivation>(new Derivation(worker.store.derivationFromPath(drvPath)));
285+
drv = std::unique_ptr<BasicDerivation>(fullDrv);
263286

264287
haveDerivation();
265288
}
@@ -1022,14 +1045,6 @@ void DerivationGoal::buildDone()
10221045
void DerivationGoal::resolvedFinished() {
10231046
assert(resolvedDrv);
10241047

1025-
// If the derivation was originally a full `Derivation` (and not just
1026-
// a `BasicDerivation`, we must retrieve it because the `staticOutputHashes`
1027-
// will be wrong otherwise
1028-
Derivation fullDrv = *drv;
1029-
if (auto upcasted = dynamic_cast<Derivation *>(drv.get()))
1030-
fullDrv = *upcasted;
1031-
1032-
auto originalHashes = staticOutputHashes(worker.store, fullDrv);
10331048
auto resolvedHashes = staticOutputHashes(worker.store, *resolvedDrv);
10341049

10351050
// `wantedOutputs` might be empty, which means “all the outputs”
@@ -1038,7 +1053,7 @@ void DerivationGoal::resolvedFinished() {
10381053
realWantedOutputs = resolvedDrv->outputNames();
10391054

10401055
for (auto & wantedOutput : realWantedOutputs) {
1041-
assert(originalHashes.count(wantedOutput) != 0);
1056+
assert(initialOutputs.count(wantedOutput) != 0);
10421057
assert(resolvedHashes.count(wantedOutput) != 0);
10431058
auto realisation = worker.store.queryRealisation(
10441059
DrvOutput{resolvedHashes.at(wantedOutput), wantedOutput}
@@ -1047,7 +1062,7 @@ void DerivationGoal::resolvedFinished() {
10471062
// realisation won't be there
10481063
if (realisation) {
10491064
auto newRealisation = *realisation;
1050-
newRealisation.id = DrvOutput{originalHashes.at(wantedOutput), wantedOutput};
1065+
newRealisation.id = DrvOutput{initialOutputs.at(wantedOutput).outputHash, wantedOutput};
10511066
worker.store.registerDrvOutput(newRealisation);
10521067
} else {
10531068
// If we don't have a realisation, then it must mean that something
@@ -3807,9 +3822,8 @@ void DerivationGoal::checkPathValidity()
38073822
{
38083823
bool checkHash = buildMode == bmRepair;
38093824
for (auto & i : queryPartialDerivationOutputMap()) {
3810-
InitialOutput info {
3811-
.wanted = wantOutput(i.first, wantedOutputs),
3812-
};
3825+
InitialOutput & info = initialOutputs.at(i.first);
3826+
info.wanted = wantOutput(i.first, wantedOutputs);
38133827
if (i.second) {
38143828
auto outputPath = *i.second;
38153829
info.known = {
@@ -3822,19 +3836,14 @@ void DerivationGoal::checkPathValidity()
38223836
};
38233837
}
38243838
if (settings.isExperimentalFeatureEnabled("ca-derivations")) {
3825-
Derivation fullDrv = *drv;
3826-
if (auto upcasted = dynamic_cast<Derivation *>(drv.get()))
3827-
fullDrv = *upcasted;
3828-
auto outputHashes = staticOutputHashes(worker.store, fullDrv);
38293839
if (auto real = worker.store.queryRealisation(
3830-
DrvOutput{outputHashes.at(i.first), i.first})) {
3840+
DrvOutput{initialOutputs.at(i.first).outputHash, i.first})) {
38313841
info.known = {
38323842
.path = real->outPath,
38333843
.status = PathStatus::Valid,
38343844
};
38353845
}
38363846
}
3837-
initialOutputs.insert_or_assign(i.first, info);
38383847
}
38393848
}
38403849

src/libstore/build/derivation-goal.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct InitialOutputStatus {
3737

3838
struct InitialOutput {
3939
bool wanted;
40+
Hash outputHash;
4041
std::optional<InitialOutputStatus> known;
4142
};
4243

0 commit comments

Comments
 (0)