mirror of
https://github.com/NixOS/nix
synced 2025-06-28 17:51:15 +02:00
lookupInFlakeCache(): Fix O(n) time lookup
This commit is contained in:
parent
d1f20e3510
commit
d2e1d4916a
4 changed files with 18 additions and 17 deletions
|
@ -104,6 +104,11 @@ public:
|
|||
|
||||
bool operator ==(const Input & other) const noexcept;
|
||||
|
||||
auto operator <=>(const Input & other) const
|
||||
{
|
||||
return attrs <=> other.attrs;
|
||||
}
|
||||
|
||||
bool contains(const Input & other) const;
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,22 +27,17 @@ struct FetchedFlake
|
|||
StorePath storePath;
|
||||
};
|
||||
|
||||
typedef std::vector<std::pair<FlakeRef, FetchedFlake>> FlakeCache;
|
||||
typedef std::map<FlakeRef, FetchedFlake> FlakeCache;
|
||||
|
||||
static std::optional<FetchedFlake> lookupInFlakeCache(
|
||||
const FlakeCache & flakeCache,
|
||||
const FlakeRef & flakeRef)
|
||||
{
|
||||
// FIXME: inefficient.
|
||||
for (auto & i : flakeCache) {
|
||||
if (flakeRef == i.first) {
|
||||
debug("mapping '%s' to previously seen input '%s' -> '%s",
|
||||
flakeRef, i.first, i.second.lockedRef);
|
||||
return i.second;
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
auto i = flakeCache.find(flakeRef);
|
||||
if (i == flakeCache.end()) return std::nullopt;
|
||||
debug("mapping '%s' to previously seen input '%s' -> '%s",
|
||||
flakeRef, i->first, i->second.lockedRef);
|
||||
return i->second;
|
||||
}
|
||||
|
||||
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);
|
||||
fetched.emplace(FetchedFlake{.lockedRef = lockedRef, .storePath = storePath});
|
||||
}
|
||||
flakeCache.push_back({resolvedRef, *fetched});
|
||||
flakeCache.insert_or_assign(resolvedRef, *fetched);
|
||||
}
|
||||
else {
|
||||
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'",
|
||||
|
|
|
@ -49,6 +49,8 @@ struct FlakeRef
|
|||
|
||||
bool operator ==(const FlakeRef & other) const = default;
|
||||
|
||||
auto operator <=>(const FlakeRef &) const = default;
|
||||
|
||||
FlakeRef(fetchers::Input && input, const Path & subdir)
|
||||
: input(std::move(input)), subdir(subdir)
|
||||
{ }
|
||||
|
|
|
@ -43,10 +43,9 @@ template<typename T>
|
|||
struct Explicit {
|
||||
T t;
|
||||
|
||||
bool operator ==(const Explicit<T> & other) const
|
||||
{
|
||||
return t == other.t;
|
||||
}
|
||||
bool operator ==(const Explicit<T> & other) const = default;
|
||||
|
||||
auto operator <=>(const Explicit<T> & other) const = default;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue