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

OCD performance fix: {find,count}+insert => insert

This commit is contained in:
Eelco Dolstra 2019-10-09 15:51:52 +02:00
parent e6e61f0a54
commit 99b73fb507
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
16 changed files with 32 additions and 65 deletions

View file

@ -46,11 +46,10 @@ static void printValue(std::ostream & str, std::set<const Value *> & active, con
{
checkInterrupt();
if (active.find(&v) != active.end()) {
if (!active.insert(&v).second) {
str << "<CYCLE>";
return;
}
active.insert(&v);
switch (v.type) {
case tInt:
@ -1446,8 +1445,7 @@ void EvalState::forceValueDeep(Value & v)
std::function<void(Value & v)> recurse;
recurse = [&](Value & v) {
if (seen.find(&v) != seen.end()) return;
seen.insert(&v);
if (!seen.insert(&v).second) return;
forceValue(v);
@ -1865,8 +1863,7 @@ size_t valueSize(Value & v)
std::set<const void *> seen;
auto doString = [&](const char * s) -> size_t {
if (seen.find(s) != seen.end()) return 0;
seen.insert(s);
if (!seen.insert(s).second) return 0;
return strlen(s) + 1;
};
@ -1874,8 +1871,7 @@ size_t valueSize(Value & v)
std::function<size_t(Env & v)> doEnv;
doValue = [&](Value & v) -> size_t {
if (seen.find(&v) != seen.end()) return 0;
seen.insert(&v);
if (!seen.insert(&v).second) return 0;
size_t sz = sizeof(Value);
@ -1890,8 +1886,7 @@ size_t valueSize(Value & v)
sz += doString(v.path);
break;
case tAttrs:
if (seen.find(v.attrs) == seen.end()) {
seen.insert(v.attrs);
if (seen.insert(v.attrs).second) {
sz += sizeof(Bindings) + sizeof(Attr) * v.attrs->capacity();
for (auto & i : *v.attrs)
sz += doValue(*i.value);
@ -1900,8 +1895,7 @@ size_t valueSize(Value & v)
case tList1:
case tList2:
case tListN:
if (seen.find(v.listElems()) == seen.end()) {
seen.insert(v.listElems());
if (seen.insert(v.listElems()).second) {
sz += v.listSize() * sizeof(Value *);
for (size_t n = 0; n < v.listSize(); ++n)
sz += doValue(*v.listElems()[n]);
@ -1922,8 +1916,7 @@ size_t valueSize(Value & v)
sz += doValue(*v.primOpApp.right);
break;
case tExternal:
if (seen.find(v.external) != seen.end()) break;
seen.insert(v.external);
if (!seen.insert(v.external).second) break;
sz += v.external->valueSize(seen);
break;
default:
@ -1934,8 +1927,7 @@ size_t valueSize(Value & v)
};
doEnv = [&](Env & env) -> size_t {
if (seen.find(&env) != seen.end()) return 0;
seen.insert(&env);
if (!seen.insert(&env).second) return 0;
size_t sz = sizeof(Env) + sizeof(Value *) * env.size;

View file

@ -277,8 +277,7 @@ static bool getDerivation(EvalState & state, Value & v,
/* Remove spurious duplicates (e.g., a set like `rec { x =
derivation {...}; y = x;}'. */
if (done.find(v.attrs) != done.end()) return false;
done.insert(v.attrs);
if (!done.insert(v.attrs).second) return false;
DrvInfo drv(state, attrPath, v.attrs);

View file

@ -138,11 +138,10 @@ static void addAttr(ExprAttrs * attrs, AttrPath & attrPath,
static void addFormal(const Pos & pos, Formals * formals, const Formal & formal)
{
if (formals->argNames.find(formal.name) != formals->argNames.end())
if (!formals->argNames.insert(formal.name).second)
throw ParseError(format("duplicate formal function argument '%1%' at %2%")
% formal.name % pos);
formals->formals.push_front(formal);
formals->argNames.insert(formal.name);
}

View file

@ -396,8 +396,7 @@ static void prim_genericClosure(EvalState & state, const Pos & pos, Value * * ar
throw EvalError(format("attribute 'key' required, at %1%") % pos);
state.forceValue(*key->value);
if (doneKeys.find(key->value) != doneKeys.end()) continue;
doneKeys.insert(key->value);
if (!doneKeys.insert(key->value).second) continue;
res.push_back(e);
/* Call the `operator' function with `e' as argument. */
@ -1273,13 +1272,12 @@ static void prim_listToAttrs(EvalState & state, const Pos & pos, Value * * args,
string name = state.forceStringNoCtx(*j->value, pos);
Symbol sym = state.symbols.create(name);
if (seen.find(sym) == seen.end()) {
if (seen.insert(sym).second) {
Bindings::iterator j2 = v2.attrs->find(state.symbols.create(state.sValue));
if (j2 == v2.attrs->end())
throw TypeError(format("'value' attribute missing in a call to 'listToAttrs', at %1%") % pos);
v.attrs->push_back(Attr(sym, j2->value, j2->pos));
seen.insert(sym);
}
}

View file

@ -105,10 +105,9 @@ static void printValueAsXML(EvalState & state, bool strict, bool location,
XMLOpenElement _(doc, "derivation", xmlAttrs);
if (drvPath != "" && drvsSeen.find(drvPath) == drvsSeen.end()) {
drvsSeen.insert(drvPath);
if (drvPath != "" && drvsSeen.insert(drvPath).second)
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
} else
else
doc.writeEmptyElement("repeated");
}