From 646d1f5ff7b6ee7e707f496b0d3c1cc0064bf6b7 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 16 Oct 2025 16:54:47 -0400 Subject: [PATCH 1/2] `registerOutputs`: Swap check and non-check cases in the code This is the exact same control flow, but with one less branch because it becomes redundant. --- src/libstore/unix/build/derivation-builder.cc | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/libstore/unix/build/derivation-builder.cc b/src/libstore/unix/build/derivation-builder.cc index 0efdc14b2..ae161ae8c 100644 --- a/src/libstore/unix/build/derivation-builder.cc +++ b/src/libstore/unix/build/derivation-builder.cc @@ -1737,27 +1737,6 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() dynamicOutputLock.lockPaths({store.toRealPath(finalDestPath)}); } - /* Move files, if needed */ - if (store.toRealPath(finalDestPath) != actualPath) { - if (buildMode == bmRepair) { - /* Path already exists, need to replace it */ - replaceValidPath(store.toRealPath(finalDestPath), actualPath); - } else if (buildMode == bmCheck) { - /* Path already exists, and we want to compare, so we leave out - new path in place. */ - } else if (store.isValidPath(newInfo.path)) { - /* Path already exists because CA path produced by something - else. No moving needed. */ - assert(newInfo.ca); - /* Can delete our scratch copy now. */ - deletePath(actualPath); - } else { - auto destPath = store.toRealPath(finalDestPath); - deletePath(destPath); - movePath(actualPath, destPath); - } - } - if (buildMode == bmCheck) { /* Check against already registered outputs */ @@ -1799,6 +1778,24 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() } else { /* do tasks relating to registering these outputs */ + /* Move files, if needed */ + if (store.toRealPath(finalDestPath) != actualPath) { + if (buildMode == bmRepair) { + /* Path already exists, need to replace it */ + replaceValidPath(store.toRealPath(finalDestPath), actualPath); + } else if (store.isValidPath(newInfo.path)) { + /* Path already exists because CA path produced by something + else. No moving needed. */ + assert(newInfo.ca); + /* Can delete our scratch copy now. */ + deletePath(actualPath); + } else { + auto destPath = store.toRealPath(finalDestPath); + deletePath(destPath); + movePath(actualPath, destPath); + } + } + /* For debugging, print out the referenced and unreferenced paths. */ for (auto & i : inputPaths) { if (references.count(i)) From 35b08b71a4f04d6173401c8ba26426e84e54f827 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 16 Oct 2025 16:59:19 -0400 Subject: [PATCH 2/2] `registerOutputs`: Hoist up `optimizePath` call and comment rationale --- src/libstore/unix/build/derivation-builder.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libstore/unix/build/derivation-builder.cc b/src/libstore/unix/build/derivation-builder.cc index ae161ae8c..9685f2054 100644 --- a/src/libstore/unix/build/derivation-builder.cc +++ b/src/libstore/unix/build/derivation-builder.cc @@ -1783,16 +1783,26 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() if (buildMode == bmRepair) { /* Path already exists, need to replace it */ replaceValidPath(store.toRealPath(finalDestPath), actualPath); + /* Optimize store object we just replaced with new + (not-yet-optimized) data. */ + store.optimisePath( + store.toRealPath(finalDestPath), NoRepair); // FIXME: combine with scanForReferences() } else if (store.isValidPath(newInfo.path)) { /* Path already exists because CA path produced by something else. No moving needed. */ assert(newInfo.ca); /* Can delete our scratch copy now. */ deletePath(actualPath); + /* Presume already-existing store object is already + optimized. */ } else { auto destPath = store.toRealPath(finalDestPath); deletePath(destPath); movePath(actualPath, destPath); + /* Optimize store object we just installed from new + (not-yet-optimized) data. */ + store.optimisePath( + store.toRealPath(finalDestPath), NoRepair); // FIXME: combine with scanForReferences() } } @@ -1804,10 +1814,6 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() debug("unreferenced input: '%1%'", store.printStorePath(i)); } - if (!store.isValidPath(newInfo.path)) - store.optimisePath( - store.toRealPath(finalDestPath), NoRepair); // FIXME: combine with scanForReferences() - newInfo.deriver = drvPath; newInfo.ultimate = true; store.signPathInfo(newInfo);