mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 22:16:16 +02:00
Fix accidental O(n^2 * log n) performance in NixRepl::addAttrsToScope
Only sort once, after adding all of the attrs first. This reduces my `nix repl '<nixpkgs>'` loading time from 1.07s to 103ms. Fixes #5823
This commit is contained in:
parent
af553b2090
commit
a4ab0a74d9
2 changed files with 17 additions and 2 deletions
|
@ -368,6 +368,13 @@ struct StaticEnv
|
||||||
[](const Vars::value_type & a, const Vars::value_type & b) { return a.first < b.first; });
|
[](const Vars::value_type & a, const Vars::value_type & b) { return a.first < b.first; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void deduplicate()
|
||||||
|
{
|
||||||
|
const auto last = std::unique(vars.begin(), vars.end(),
|
||||||
|
[] (const Vars::value_type & a, const Vars::value_type & b) { return a.first == b.first; });
|
||||||
|
vars.erase(last, vars.end());
|
||||||
|
}
|
||||||
|
|
||||||
Vars::const_iterator find(const Symbol & name) const
|
Vars::const_iterator find(const Symbol & name) const
|
||||||
{
|
{
|
||||||
Vars::value_type key(name, 0);
|
Vars::value_type key(name, 0);
|
||||||
|
|
|
@ -661,8 +661,16 @@ void NixRepl::reloadFiles()
|
||||||
void NixRepl::addAttrsToScope(Value & attrs)
|
void NixRepl::addAttrsToScope(Value & attrs)
|
||||||
{
|
{
|
||||||
state->forceAttrs(attrs);
|
state->forceAttrs(attrs);
|
||||||
for (auto & i : *attrs.attrs)
|
if (displ + attrs.attrs->size() >= envSize)
|
||||||
addVarToScope(i.name, *i.value);
|
throw Error("environment full; cannot add more variables");
|
||||||
|
|
||||||
|
for (auto & i : *attrs.attrs) {
|
||||||
|
staticEnv.vars.emplace_back(i.name, displ);
|
||||||
|
env->values[displ++] = i.value;
|
||||||
|
varNames.insert((string) i.name);
|
||||||
|
}
|
||||||
|
staticEnv.sort();
|
||||||
|
staticEnv.deduplicate();
|
||||||
notice("Added %1% variables.", attrs.attrs->size());
|
notice("Added %1% variables.", attrs.attrs->size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue