Skip to content

Commit dfcbe70

Browse files
authored
Merge pull request #13750 from obsidiansystems/simplify-derivation-goal-0
Simplify `DerivationGoal` in two ways
2 parents be3a508 + ed55937 commit dfcbe70

2 files changed

Lines changed: 46 additions & 60 deletions

File tree

src/libstore/build/derivation-goal.cc

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,17 @@ Goal::Co DerivationGoal::repairClosure()
241241
that produced those outputs. */
242242

243243
/* Get the output closure. */
244-
auto outputs = queryDerivationOutputMap();
244+
auto outputs = [&] {
245+
for (auto * drvStore : {&worker.evalStore, &worker.store})
246+
if (drvStore->isValidPath(drvPath))
247+
return worker.store.queryDerivationOutputMap(drvPath, drvStore);
248+
249+
OutputPathMap res;
250+
for (auto & [name, output] : drv->outputsAndOptPaths(worker.store))
251+
res.insert_or_assign(name, *output.second);
252+
return res;
253+
}();
254+
245255
StorePathSet outputClosure;
246256
if (auto mPath = get(outputs, wantedOutput)) {
247257
worker.store.computeFSClosure(*mPath, outputClosure);
@@ -304,37 +314,6 @@ Goal::Co DerivationGoal::repairClosure()
304314
co_return done(BuildResult::AlreadyValid, assertPathValidity());
305315
}
306316

307-
std::map<std::string, std::optional<StorePath>> DerivationGoal::queryPartialDerivationOutputMap()
308-
{
309-
assert(!drv->type().isImpure());
310-
311-
for (auto * drvStore : {&worker.evalStore, &worker.store})
312-
if (drvStore->isValidPath(drvPath))
313-
return worker.store.queryPartialDerivationOutputMap(drvPath, drvStore);
314-
315-
/* In-memory derivation will naturally fall back on this case, where
316-
we do best-effort with static information. */
317-
std::map<std::string, std::optional<StorePath>> res;
318-
for (auto & [name, output] : drv->outputs)
319-
res.insert_or_assign(name, output.path(worker.store, drv->name, name));
320-
return res;
321-
}
322-
323-
OutputPathMap DerivationGoal::queryDerivationOutputMap()
324-
{
325-
assert(!drv->type().isImpure());
326-
327-
for (auto * drvStore : {&worker.evalStore, &worker.store})
328-
if (drvStore->isValidPath(drvPath))
329-
return worker.store.queryDerivationOutputMap(drvPath, drvStore);
330-
331-
// See comment in `DerivationGoal::queryPartialDerivationOutputMap`.
332-
OutputPathMap res;
333-
for (auto & [name, output] : drv->outputsAndOptPaths(worker.store))
334-
res.insert_or_assign(name, *output.second);
335-
return res;
336-
}
337-
338317
std::pair<bool, SingleDrvOutputs> DerivationGoal::checkPathValidity()
339318
{
340319
if (drv->type().isImpure())
@@ -344,7 +323,20 @@ std::pair<bool, SingleDrvOutputs> DerivationGoal::checkPathValidity()
344323
StringSet wantedOutputsLeft{wantedOutput};
345324
SingleDrvOutputs validOutputs;
346325

347-
for (auto & i : queryPartialDerivationOutputMap()) {
326+
auto partialDerivationOutputMap = [&] {
327+
for (auto * drvStore : {&worker.evalStore, &worker.store})
328+
if (drvStore->isValidPath(drvPath))
329+
return worker.store.queryPartialDerivationOutputMap(drvPath, drvStore);
330+
331+
/* In-memory derivation will naturally fall back on this case, where
332+
we do best-effort with static information. */
333+
std::map<std::string, std::optional<StorePath>> res;
334+
for (auto & [name, output] : drv->outputs)
335+
res.insert_or_assign(name, output.path(worker.store, drv->name, name));
336+
return res;
337+
}();
338+
339+
for (auto & i : partialDerivationOutputMap) {
348340
auto initialOutput = get(initialOutputs, i.first);
349341
if (!initialOutput)
350342
// this is an invalid output, gets caught with (!wantedOutputsLeft.empty())

src/libstore/include/nix/store/build/derivation-goal.hh

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,6 @@ struct DerivationGoal : public Goal
4343
*/
4444
OutputName wantedOutput;
4545

46-
/**
47-
* The derivation stored at drvPath.
48-
*/
49-
std::unique_ptr<Derivation> drv;
50-
51-
/**
52-
* The remainder is state held during the build.
53-
*/
54-
55-
std::map<std::string, InitialOutput> initialOutputs;
56-
57-
BuildMode buildMode;
58-
59-
std::unique_ptr<MaintainCount<uint64_t>> mcExpectedBuilds;
60-
6146
DerivationGoal(
6247
const StorePath & drvPath,
6348
const Derivation & drv,
@@ -73,18 +58,32 @@ struct DerivationGoal : public Goal
7358

7459
std::string key() override;
7560

61+
JobCategory jobCategory() const override
62+
{
63+
return JobCategory::Administration;
64+
};
65+
66+
private:
67+
7668
/**
77-
* The states.
69+
* The derivation stored at drvPath.
7870
*/
79-
Co haveDerivation();
71+
std::unique_ptr<Derivation> drv;
8072

8173
/**
82-
* Wrappers around the corresponding Store methods that first consult the
83-
* derivation. This is currently needed because when there is no drv file
84-
* there also is no DB entry.
74+
* The remainder is state held during the build.
8575
*/
86-
std::map<std::string, std::optional<StorePath>> queryPartialDerivationOutputMap();
87-
OutputPathMap queryDerivationOutputMap();
76+
77+
std::map<std::string, InitialOutput> initialOutputs;
78+
79+
BuildMode buildMode;
80+
81+
std::unique_ptr<MaintainCount<uint64_t>> mcExpectedBuilds;
82+
83+
/**
84+
* The states.
85+
*/
86+
Co haveDerivation();
8887

8988
/**
9089
* Update 'initialOutputs' to determine the current status of the
@@ -103,11 +102,6 @@ struct DerivationGoal : public Goal
103102
Co repairClosure();
104103

105104
Done done(BuildResult::Status status, SingleDrvOutputs builtOutputs = {}, std::optional<Error> ex = {});
106-
107-
JobCategory jobCategory() const override
108-
{
109-
return JobCategory::Administration;
110-
};
111105
};
112106

113107
} // namespace nix

0 commit comments

Comments
 (0)