1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-28 22:01:15 +02:00

lookupInFlakeCache(): Fix O(n) time lookup

This commit is contained in:
Eelco Dolstra 2024-12-10 15:30:55 +01:00
parent d1f20e3510
commit d2e1d4916a
4 changed files with 18 additions and 17 deletions

View file

@ -104,6 +104,11 @@ public:
bool operator ==(const Input & other) const noexcept; bool operator ==(const Input & other) const noexcept;
auto operator <=>(const Input & other) const
{
return attrs <=> other.attrs;
}
bool contains(const Input & other) const; bool contains(const Input & other) const;
/** /**

View file

@ -27,22 +27,17 @@ struct FetchedFlake
StorePath storePath; StorePath storePath;
}; };
typedef std::vector<std::pair<FlakeRef, FetchedFlake>> FlakeCache; typedef std::map<FlakeRef, FetchedFlake> FlakeCache;
static std::optional<FetchedFlake> lookupInFlakeCache( static std::optional<FetchedFlake> lookupInFlakeCache(
const FlakeCache & flakeCache, const FlakeCache & flakeCache,
const FlakeRef & flakeRef) const FlakeRef & flakeRef)
{ {
// FIXME: inefficient. auto i = flakeCache.find(flakeRef);
for (auto & i : flakeCache) { if (i == flakeCache.end()) return std::nullopt;
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.lockedRef);
flakeRef, i.first, i.second.lockedRef); return i->second;
return i.second;
}
}
return std::nullopt;
} }
static std::tuple<StorePath, FlakeRef, FlakeRef> fetchOrSubstituteTree( static std::tuple<StorePath, FlakeRef, FlakeRef> fetchOrSubstituteTree(
@ -72,13 +67,13 @@ static std::tuple<StorePath, FlakeRef, FlakeRef> fetchOrSubstituteTree(
auto [storePath, lockedRef] = resolvedRef.fetchTree(state.store); auto [storePath, lockedRef] = resolvedRef.fetchTree(state.store);
fetched.emplace(FetchedFlake{.lockedRef = lockedRef, .storePath = storePath}); fetched.emplace(FetchedFlake{.lockedRef = lockedRef, .storePath = storePath});
} }
flakeCache.push_back({resolvedRef, *fetched}); flakeCache.insert_or_assign(resolvedRef, *fetched);
} }
else { else {
throw Error("'%s' is an indirect flake reference, but registry lookups are not allowed", originalRef); throw Error("'%s' is an indirect flake reference, but registry lookups are not allowed", originalRef);
} }
} }
flakeCache.push_back({originalRef, *fetched}); flakeCache.insert_or_assign(originalRef, *fetched);
} }
debug("got tree '%s' from '%s'", debug("got tree '%s' from '%s'",

View file

@ -49,6 +49,8 @@ struct FlakeRef
bool operator ==(const FlakeRef & other) const = default; bool operator ==(const FlakeRef & other) const = default;
auto operator <=>(const FlakeRef &) const = default;
FlakeRef(fetchers::Input && input, const Path & subdir) FlakeRef(fetchers::Input && input, const Path & subdir)
: input(std::move(input)), subdir(subdir) : input(std::move(input)), subdir(subdir)
{ } { }

View file

@ -43,10 +43,9 @@ template<typename T>
struct Explicit { struct Explicit {
T t; T t;
bool operator ==(const Explicit<T> & other) const bool operator ==(const Explicit<T> & other) const = default;
{
return t == other.t; auto operator <=>(const Explicit<T> & other) const = default;
}
}; };