Reduce substitution memory consumption

copyStorePath() now pipes the output of srcStore->narFromPath()
directly into dstStore->addToStore(). The sink used by the former is
converted into a source usable by the latter using
boost::coroutine2. This is based on [1].

This reduces the maximum resident size of

  $ nix build --store ~/my-nix/ /nix/store/b0zlxla7dmy1iwc3g459rjznx59797xy-binutils-2.28.1 --substituters file:///tmp/binary-cache-xz/ --no-require-sigs

from 418592 KiB to 53416 KiB. (The previous commit also reduced the
runtime from ~4.2s to ~3.4s, not sure why.) A further improvement will
be to download files into a Sink.

[1] https://github.com/NixOS/nix/compare/master...Mathnerd314:dump-fix-coroutine#diff-dcbcac55a634031f9cc73707da6e4b18

Issue #1969.
This commit is contained in:
Eelco Dolstra 2018-03-16 20:22:34 +01:00
parent 3e6b194d78
commit 48662d151b
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
8 changed files with 145 additions and 37 deletions

View file

@ -964,20 +964,11 @@ void LocalStore::invalidatePath(State & state, const Path & path)
}
void LocalStore::addToStore(const ValidPathInfo & info, const ref<std::string> & nar,
void LocalStore::addToStore(const ValidPathInfo & info, Source & source,
RepairFlag repair, CheckSigsFlag checkSigs, std::shared_ptr<FSAccessor> accessor)
{
assert(info.narHash);
Hash h = hashString(htSHA256, *nar);
if (h != info.narHash)
throw Error("hash mismatch importing path '%s'; expected hash '%s', got '%s'",
info.path, info.narHash.to_string(), h.to_string());
if (nar->size() != info.narSize)
throw Error("size mismatch importing path '%s'; expected %s, got %s",
info.path, info.narSize, nar->size());
if (requireSigs && checkSigs && !info.checkSignatures(*this, publicKeys))
throw Error("cannot add path '%s' because it lacks a valid signature", info.path);
@ -999,8 +990,27 @@ void LocalStore::addToStore(const ValidPathInfo & info, const ref<std::string> &
deletePath(realPath);
StringSource source(*nar);
restorePath(realPath, source);
/* While restoring the path from the NAR, compute the hash
of the NAR. */
HashSink hashSink(htSHA256);
LambdaSource wrapperSource([&](unsigned char * data, size_t len) -> size_t {
size_t n = source.read(data, len);
hashSink(data, n);
return n;
});
restorePath(realPath, wrapperSource);
auto hashResult = hashSink.finish();
if (hashResult.first != info.narHash)
throw Error("hash mismatch importing path '%s'; expected hash '%s', got '%s'",
info.path, info.narHash.to_string(), hashResult.first.to_string());
if (hashResult.second != info.narSize)
throw Error("size mismatch importing path '%s'; expected %s, got %s",
info.path, info.narSize, hashResult.second);
autoGC();