Skip to content

Commit

Permalink
fetchToStore(): Avoid duplicate copying if the input is already a sto…
Browse files Browse the repository at this point in the history
…re path

This is needed for the path:// input scheme (until it returns a
FSInputAccessor to the original path, but that's blocked by #10089)
and the Mercurial input scheme.
  • Loading branch information
edolstra committed Apr 19, 2024
1 parent 8c4c215 commit 8d08846
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/libfetchers/fetch-to-store.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "fetch-to-store.hh"
#include "fetchers.hh"
#include "cache.hh"
#include "posix-source-accessor.hh"

namespace nix {

Expand All @@ -13,8 +14,16 @@ StorePath fetchToStore(
PathFilter * filter,
RepairFlag repair)
{
// FIXME: add an optimisation for the case where the accessor is
// an FSInputAccessor pointing to a store path.
if (path.accessor->isStorePath
&& path.path.isRoot()
&& method == FileIngestionMethod::Recursive
&& !filter)
{
if (auto accessor = path.accessor.dynamic_pointer_cast<PosixSourceAccessor>())
if (auto storePath = store.maybeParseStorePath(accessor->root.string()))
if (storePath->name() == name)
return *storePath;
}

std::optional<fetchers::Attrs> cacheKey;

Expand Down Expand Up @@ -52,5 +61,4 @@ StorePath fetchToStore(
return storePath;
}


}
7 changes: 6 additions & 1 deletion src/libfetchers/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ struct PathInputScheme : InputScheme
}
input.attrs.insert_or_assign("lastModified", uint64_t(mtime));

return {makeStorePathAccessor(store, *storePath), std::move(input)};
auto accessor = makeStorePathAccessor(store, *storePath);

/* Optimize fetchToStore() calls on this path. */
accessor->isStorePath = true;

return {accessor, std::move(input)};
}

std::optional<std::string> getFingerprint(ref<Store> store, const Input & input) const override
Expand Down
3 changes: 3 additions & 0 deletions src/libfetchers/unix/mercurial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ struct MercurialInputScheme : InputScheme

accessor->setPathDisplay("«" + input.to_string() + "»");

/* Optimize fetchToStore() calls on this path. */
accessor->isStorePath = true;

return {accessor, input};
}

Expand Down
7 changes: 7 additions & 0 deletions src/libutil/input-accessor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ struct InputAccessor : virtual SourceAccessor, std::enable_shared_from_this<Inpu
{
std::optional<std::string> fingerprint;

/**
* Whether this is a store path using
* FileIngestionMethod::Recursive. This is used to optimize
* `fetchToStore()`.
*/
bool isStorePath = false;

/**
* Return the maximum last-modified time of the files in this
* tree, if available.
Expand Down

0 comments on commit 8d08846

Please sign in to comment.