Merge pull request #3645 from mkenigs/fetchOrSubstituteTree-improvements

Cache tree in fetchOrSubstituteTree
This commit is contained in:
Eelco Dolstra 2020-06-02 11:58:20 +02:00 committed by GitHub
commit 7dbba0a94e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 59 deletions

View file

@ -12,25 +12,10 @@ using namespace flake;
namespace flake { namespace flake {
/* If 'allowLookup' is true, then resolve 'flakeRef' using the typedef std::pair<Tree, FlakeRef> FetchedFlake;
registries. */ typedef std::vector<std::pair<FlakeRef, FetchedFlake>> FlakeCache;
static FlakeRef maybeLookupFlake(
ref<Store> store,
const FlakeRef & flakeRef,
bool allowLookup)
{
if (!flakeRef.input.isDirect()) {
if (allowLookup)
return flakeRef.resolve(store);
else
throw Error("'%s' is an indirect flake reference, but registry lookups are not allowed", flakeRef);
} else
return flakeRef;
}
typedef std::vector<std::pair<FlakeRef, FlakeRef>> FlakeCache; static std::optional<FetchedFlake> lookupInFlakeCache(
static FlakeRef lookupInFlakeCache(
const FlakeCache & flakeCache, const FlakeCache & flakeCache,
const FlakeRef & flakeRef) const FlakeRef & flakeRef)
{ {
@ -38,12 +23,12 @@ static FlakeRef lookupInFlakeCache(
for (auto & i : flakeCache) { for (auto & i : flakeCache) {
if (flakeRef == i.first) { if (flakeRef == i.first) {
debug("mapping '%s' to previously seen input '%s' -> '%s", debug("mapping '%s' to previously seen input '%s' -> '%s",
flakeRef, i.first, i.second); flakeRef, i.first, i.second.second);
return i.second; return i.second;
} }
} }
return flakeRef; return std::nullopt;
} }
static std::tuple<fetchers::Tree, FlakeRef, FlakeRef> fetchOrSubstituteTree( static std::tuple<fetchers::Tree, FlakeRef, FlakeRef> fetchOrSubstituteTree(
@ -52,17 +37,32 @@ static std::tuple<fetchers::Tree, FlakeRef, FlakeRef> fetchOrSubstituteTree(
bool allowLookup, bool allowLookup,
FlakeCache & flakeCache) FlakeCache & flakeCache)
{ {
auto resolvedRef = lookupInFlakeCache(flakeCache, auto fetched = lookupInFlakeCache(flakeCache, originalRef);
maybeLookupFlake(state.store, FlakeRef resolvedRef = originalRef;
lookupInFlakeCache(flakeCache, originalRef), allowLookup));
auto [tree, lockedRef] = resolvedRef.fetchTree(state.store); if (!fetched) {
if (originalRef.input.isDirect()) {
fetched.emplace(originalRef.fetchTree(state.store));
} else {
if (allowLookup) {
resolvedRef = originalRef.resolve(state.store);
auto fetchedResolved = lookupInFlakeCache(flakeCache, originalRef);
if (!fetchedResolved) fetchedResolved.emplace(resolvedRef.fetchTree(state.store));
flakeCache.push_back({resolvedRef, fetchedResolved.value()});
fetched.emplace(fetchedResolved.value());
}
else {
throw Error("'%s' is an indirect flake reference, but registry lookups are not allowed", originalRef);
}
}
flakeCache.push_back({originalRef, fetched.value()});
}
auto [tree, lockedRef] = fetched.value();
debug("got tree '%s' from '%s'", debug("got tree '%s' from '%s'",
state.store->printStorePath(tree.storePath), lockedRef); state.store->printStorePath(tree.storePath), lockedRef);
flakeCache.push_back({originalRef, lockedRef});
flakeCache.push_back({resolvedRef, lockedRef});
if (state.allowedPaths) if (state.allowedPaths)
state.allowedPaths->insert(tree.actualPath); state.allowedPaths->insert(tree.actualPath);

View file

@ -117,7 +117,7 @@ std::pair<Tree, Input> Input::fetch(ref<Store> store) const
auto actualPath = store->toRealPath(storePath); auto actualPath = store->toRealPath(storePath);
return {fetchers::Tree { .actualPath = actualPath, .storePath = std::move(storePath) }, *this}; return {fetchers::Tree(std::move(actualPath), std::move(storePath)), *this};
} catch (Error & e) { } catch (Error & e) {
debug("substitution of input '%s' failed: %s", to_string(), e.what()); debug("substitution of input '%s' failed: %s", to_string(), e.what());
} }

View file

@ -16,6 +16,8 @@ struct Tree
{ {
Path actualPath; Path actualPath;
StorePath storePath; StorePath storePath;
Tree(Path && actualPath, StorePath && storePath) : actualPath(actualPath), storePath(std::move(storePath)) {}
Tree (const Tree & rhs) : actualPath(rhs.actualPath), storePath(rhs.storePath.clone()) {}
}; };
struct InputScheme; struct InputScheme;

View file

@ -189,10 +189,7 @@ struct GitInputScheme : InputScheme
input.attrs.insert_or_assign("revCount", getIntAttr(infoAttrs, "revCount")); input.attrs.insert_or_assign("revCount", getIntAttr(infoAttrs, "revCount"));
input.attrs.insert_or_assign("lastModified", getIntAttr(infoAttrs, "lastModified")); input.attrs.insert_or_assign("lastModified", getIntAttr(infoAttrs, "lastModified"));
return { return {
Tree { Tree(store->toRealPath(storePath), std::move(storePath)),
.actualPath = store->toRealPath(storePath),
.storePath = std::move(storePath),
},
input input
}; };
}; };
@ -273,10 +270,8 @@ struct GitInputScheme : InputScheme
haveCommits ? std::stoull(runProgram("git", true, { "-C", actualUrl, "log", "-1", "--format=%ct", "HEAD" })) : 0); haveCommits ? std::stoull(runProgram("git", true, { "-C", actualUrl, "log", "-1", "--format=%ct", "HEAD" })) : 0);
return { return {
Tree { Tree(store->printStorePath(storePath), std::move(storePath)),
.actualPath = store->printStorePath(storePath), input
.storePath = std::move(storePath),
}, input
}; };
} }
} }

View file

@ -140,10 +140,7 @@ struct GitArchiveInputScheme : InputScheme
if (auto res = getCache()->lookup(store, immutableAttrs)) { if (auto res = getCache()->lookup(store, immutableAttrs)) {
input.attrs.insert_or_assign("lastModified", getIntAttr(res->first, "lastModified")); input.attrs.insert_or_assign("lastModified", getIntAttr(res->first, "lastModified"));
return { return {
Tree{ Tree(store->toRealPath(res->second), std::move(res->second)),
.actualPath = store->toRealPath(res->second),
.storePath = std::move(res->second),
},
input input
}; };
} }

View file

@ -164,10 +164,10 @@ struct MercurialInputScheme : InputScheme
auto storePath = store->addToStore("source", actualUrl, true, htSHA256, filter); auto storePath = store->addToStore("source", actualUrl, true, htSHA256, filter);
return {Tree { return {
.actualPath = store->printStorePath(storePath), Tree(store->printStorePath(storePath), std::move(storePath)),
.storePath = std::move(storePath), input
}, input}; };
} }
} }
@ -189,10 +189,7 @@ struct MercurialInputScheme : InputScheme
assert(!_input.getRev() || _input.getRev() == input.getRev()); assert(!_input.getRev() || _input.getRev() == input.getRev());
input.attrs.insert_or_assign("revCount", getIntAttr(infoAttrs, "revCount")); input.attrs.insert_or_assign("revCount", getIntAttr(infoAttrs, "revCount"));
return { return {
Tree{ Tree(store->toRealPath(storePath), std::move(storePath)),
.actualPath = store->toRealPath(storePath),
.storePath = std::move(storePath),
},
input input
}; };
}; };

View file

@ -96,10 +96,7 @@ struct PathInputScheme : InputScheme
storePath = store->addToStore("source", path); storePath = store->addToStore("source", path);
return { return {
Tree { Tree(store->toRealPath(*storePath), std::move(*storePath)),
.actualPath = store->toRealPath(*storePath),
.storePath = std::move(*storePath),
},
input input
}; };
} }

View file

@ -117,10 +117,7 @@ std::pair<Tree, time_t> downloadTarball(
if (cached && !cached->expired) if (cached && !cached->expired)
return { return {
Tree { Tree(store->toRealPath(cached->storePath), std::move(cached->storePath)),
.actualPath = store->toRealPath(cached->storePath),
.storePath = std::move(cached->storePath),
},
getIntAttr(cached->infoAttrs, "lastModified") getIntAttr(cached->infoAttrs, "lastModified")
}; };
@ -157,10 +154,7 @@ std::pair<Tree, time_t> downloadTarball(
immutable); immutable);
return { return {
Tree { Tree(store->toRealPath(*unpackedStorePath), std::move(*unpackedStorePath)),
.actualPath = store->toRealPath(*unpackedStorePath),
.storePath = std::move(*unpackedStorePath),
},
lastModified, lastModified,
}; };
} }