Skip to content

Commit

Permalink
lookupInFlakeCache(): Fix O(n) time lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Dec 10, 2024
1 parent f1060b0 commit 82ec92f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/libfetchers/fetchers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
21 changes: 8 additions & 13 deletions src/libflake/flake/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -66,13 +61,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'",
Expand Down
2 changes: 2 additions & 0 deletions src/libflake/flake/flakeref.hh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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)
{ }
Expand Down
7 changes: 3 additions & 4 deletions src/libutil/types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down

0 comments on commit 82ec92f

Please sign in to comment.