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(); checkInterrupt();
if (active.find(&v) != active.end()) { if (!active.insert(&v).second) {
str << "<CYCLE>"; str << "<CYCLE>";
return; return;
} }
active.insert(&v);
switch (v.type) { switch (v.type) {
case tInt: case tInt:
@ -1446,8 +1445,7 @@ void EvalState::forceValueDeep(Value & v)
std::function<void(Value & v)> recurse; std::function<void(Value & v)> recurse;
recurse = [&](Value & v) { recurse = [&](Value & v) {
if (seen.find(&v) != seen.end()) return; if (!seen.insert(&v).second) return;
seen.insert(&v);
forceValue(v); forceValue(v);
@ -1865,8 +1863,7 @@ size_t valueSize(Value & v)
std::set<const void *> seen; std::set<const void *> seen;
auto doString = [&](const char * s) -> size_t { auto doString = [&](const char * s) -> size_t {
if (seen.find(s) != seen.end()) return 0; if (!seen.insert(s).second) return 0;
seen.insert(s);
return strlen(s) + 1; return strlen(s) + 1;
}; };
@ -1874,8 +1871,7 @@ size_t valueSize(Value & v)
std::function<size_t(Env & v)> doEnv; std::function<size_t(Env & v)> doEnv;
doValue = [&](Value & v) -> size_t { doValue = [&](Value & v) -> size_t {
if (seen.find(&v) != seen.end()) return 0; if (!seen.insert(&v).second) return 0;
seen.insert(&v);
size_t sz = sizeof(Value); size_t sz = sizeof(Value);
@ -1890,8 +1886,7 @@ size_t valueSize(Value & v)
sz += doString(v.path); sz += doString(v.path);
break; break;
case tAttrs: case tAttrs:
if (seen.find(v.attrs) == seen.end()) { if (seen.insert(v.attrs).second) {
seen.insert(v.attrs);
sz += sizeof(Bindings) + sizeof(Attr) * v.attrs->capacity(); sz += sizeof(Bindings) + sizeof(Attr) * v.attrs->capacity();
for (auto & i : *v.attrs) for (auto & i : *v.attrs)
sz += doValue(*i.value); sz += doValue(*i.value);
@ -1900,8 +1895,7 @@ size_t valueSize(Value & v)
case tList1: case tList1:
case tList2: case tList2:
case tListN: case tListN:
if (seen.find(v.listElems()) == seen.end()) { if (seen.insert(v.listElems()).second) {
seen.insert(v.listElems());
sz += v.listSize() * sizeof(Value *); sz += v.listSize() * sizeof(Value *);
for (size_t n = 0; n < v.listSize(); ++n) for (size_t n = 0; n < v.listSize(); ++n)
sz += doValue(*v.listElems()[n]); sz += doValue(*v.listElems()[n]);
@ -1922,8 +1916,7 @@ size_t valueSize(Value & v)
sz += doValue(*v.primOpApp.right); sz += doValue(*v.primOpApp.right);
break; break;
case tExternal: case tExternal:
if (seen.find(v.external) != seen.end()) break; if (!seen.insert(v.external).second) break;
seen.insert(v.external);
sz += v.external->valueSize(seen); sz += v.external->valueSize(seen);
break; break;
default: default:
@ -1934,8 +1927,7 @@ size_t valueSize(Value & v)
}; };
doEnv = [&](Env & env) -> size_t { doEnv = [&](Env & env) -> size_t {
if (seen.find(&env) != seen.end()) return 0; if (!seen.insert(&env).second) return 0;
seen.insert(&env);
size_t sz = sizeof(Env) + sizeof(Value *) * env.size; 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 = /* Remove spurious duplicates (e.g., a set like `rec { x =
derivation {...}; y = x;}'. */ derivation {...}; y = x;}'. */
if (done.find(v.attrs) != done.end()) return false; if (!done.insert(v.attrs).second) return false;
done.insert(v.attrs);
DrvInfo drv(state, attrPath, v.attrs); 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) 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%") throw ParseError(format("duplicate formal function argument '%1%' at %2%")
% formal.name % pos); % formal.name % pos);
formals->formals.push_front(formal); 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); throw EvalError(format("attribute 'key' required, at %1%") % pos);
state.forceValue(*key->value); state.forceValue(*key->value);
if (doneKeys.find(key->value) != doneKeys.end()) continue; if (!doneKeys.insert(key->value).second) continue;
doneKeys.insert(key->value);
res.push_back(e); res.push_back(e);
/* Call the `operator' function with `e' as argument. */ /* 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); string name = state.forceStringNoCtx(*j->value, pos);
Symbol sym = state.symbols.create(name); 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)); Bindings::iterator j2 = v2.attrs->find(state.symbols.create(state.sValue));
if (j2 == v2.attrs->end()) if (j2 == v2.attrs->end())
throw TypeError(format("'value' attribute missing in a call to 'listToAttrs', at %1%") % pos); throw TypeError(format("'value' attribute missing in a call to 'listToAttrs', at %1%") % pos);
v.attrs->push_back(Attr(sym, j2->value, j2->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); XMLOpenElement _(doc, "derivation", xmlAttrs);
if (drvPath != "" && drvsSeen.find(drvPath) == drvsSeen.end()) { if (drvPath != "" && drvsSeen.insert(drvPath).second)
drvsSeen.insert(drvPath);
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen); showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
} else else
doc.writeEmptyElement("repeated"); doc.writeEmptyElement("repeated");
} }

View file

@ -568,10 +568,9 @@ UserLock::UserLock()
{ {
auto lockedPaths(lockedPaths_.lock()); auto lockedPaths(lockedPaths_.lock());
if (lockedPaths->count(fnUserLock)) if (!lockedPaths->insert(fnUserLock).second)
/* We already have a lock on this one. */ /* We already have a lock on this one. */
continue; continue;
lockedPaths->insert(fnUserLock);
} }
try { try {
@ -620,8 +619,8 @@ UserLock::UserLock()
UserLock::~UserLock() UserLock::~UserLock()
{ {
auto lockedPaths(lockedPaths_.lock()); auto lockedPaths(lockedPaths_.lock());
assert(lockedPaths->count(fnUserLock)); auto erased = lockedPaths->erase(fnUserLock);
lockedPaths->erase(fnUserLock); assert(erased);
} }
@ -1125,11 +1124,9 @@ void DerivationGoal::addWantedOutputs(const StringSet & outputs)
needRestart = true; needRestart = true;
} else } else
for (auto & i : outputs) for (auto & i : outputs)
if (wantedOutputs.find(i) == wantedOutputs.end()) { if (wantedOutputs.insert(i).second)
wantedOutputs.insert(i);
needRestart = true; needRestart = true;
} }
}
void DerivationGoal::getDerivation() void DerivationGoal::getDerivation()

View file

@ -123,8 +123,7 @@ static Path out;
static void addPkg(const Path & pkgDir, int priority) static void addPkg(const Path & pkgDir, int priority)
{ {
if (done.count(pkgDir)) return; if (!done.insert(pkgDir).second) return;
done.insert(pkgDir);
createLinks(pkgDir, out, priority); createLinks(pkgDir, out, priority);
try { try {

View file

@ -1292,8 +1292,7 @@ void LocalStore::verifyPath(const Path & path, const PathSet & store,
{ {
checkInterrupt(); checkInterrupt();
if (done.find(path) != done.end()) return; if (!done.insert(path).second) return;
done.insert(path);
if (!isStorePath(path)) { if (!isStorePath(path)) {
printError(format("path '%1%' is not in the Nix store") % path); printError(format("path '%1%' is not in the Nix store") % path);

View file

@ -29,8 +29,7 @@ void Store::computeFSClosure(const PathSet & startPaths,
{ {
auto state(state_.lock()); auto state(state_.lock());
if (state->exc) return; if (state->exc) return;
if (state->paths.count(path)) return; if (!state->paths.insert(path).second) return;
state->paths.insert(path);
state->pending++; state->pending++;
} }
@ -175,8 +174,7 @@ void Store::queryMissing(const PathSet & targets,
{ {
auto state(state_.lock()); auto state(state_.lock());
if (state->done.count(path)) return; if (!state->done.insert(path).second) return;
state->done.insert(path);
} }
DrvPathWithOutputs i2 = parseDrvPathWithOutputs(path); DrvPathWithOutputs i2 = parseDrvPathWithOutputs(path);
@ -252,8 +250,7 @@ Paths Store::topoSortPaths(const PathSet & paths)
if (parents.find(path) != parents.end()) if (parents.find(path) != parents.end())
throw BuildError(format("cycle detected in the references of '%1%' from '%2%'") % path % *parent); throw BuildError(format("cycle detected in the references of '%1%' from '%2%'") % path % *parent);
if (visited.find(path) != visited.end()) return; if (!visited.insert(path).second) return;
visited.insert(path);
parents.insert(path); parents.insert(path);
PathSet references; PathSet references;

View file

@ -36,11 +36,10 @@ static void search(const unsigned char * s, size_t len,
} }
if (!match) continue; if (!match) continue;
string ref((const char *) s + i, refLength); string ref((const char *) s + i, refLength);
if (hashes.find(ref) != hashes.end()) { if (hashes.erase(ref)) {
debug(format("found reference to '%1%' at offset '%2%'") debug(format("found reference to '%1%' at offset '%2%'")
% ref % i); % ref % i);
seen.insert(ref); seen.insert(ref);
hashes.erase(ref);
} }
++i; ++i;
} }

View file

@ -954,8 +954,7 @@ std::list<ref<Store>> getDefaultSubstituters()
StringSet done; StringSet done;
auto addStore = [&](const std::string & uri) { auto addStore = [&](const std::string & uri) {
if (done.count(uri)) return; if (!done.insert(uri).second) return;
done.insert(uri);
try { try {
stores.push_back(openStore(uri)); stores.push_back(openStore(uri));
} catch (Error & e) { } catch (Error & e) {

View file

@ -124,11 +124,10 @@ static void getAllExprs(EvalState & state,
string attrName = i; string attrName = i;
if (hasSuffix(attrName, ".nix")) if (hasSuffix(attrName, ".nix"))
attrName = string(attrName, 0, attrName.size() - 4); attrName = string(attrName, 0, attrName.size() - 4);
if (attrs.find(attrName) != attrs.end()) { if (!attrs.insert(attrName).second) {
printError(format("warning: name collision in input Nix expressions, skipping '%1%'") % path2); printError(format("warning: name collision in input Nix expressions, skipping '%1%'") % path2);
continue; continue;
} }
attrs.insert(attrName);
/* Load the expression on demand. */ /* Load the expression on demand. */
Value & vFun = state.getBuiltin("import"); Value & vFun = state.getBuiltin("import");
Value & vArg(*state.allocValue()); Value & vArg(*state.allocValue());
@ -307,11 +306,9 @@ static DrvInfos filterBySelector(EvalState & state, const DrvInfos & allElems,
/* Insert only those elements in the final list that we /* Insert only those elements in the final list that we
haven't inserted before. */ haven't inserted before. */
for (auto & j : matches) for (auto & j : matches)
if (done.find(j.second) == done.end()) { if (done.insert(j.second).second)
done.insert(j.second);
elems.push_back(j.first); elems.push_back(j.first);
} }
}
checkSelectorUse(selectors); checkSelectorUse(selectors);

View file

@ -71,9 +71,7 @@ void printClosure(const Path & nePath, const StoreExpr & fs)
Path path = *(workList.begin()); Path path = *(workList.begin());
workList.erase(path); workList.erase(path);
if (doneSet.find(path) == doneSet.end()) { if (doneSet.insert(path).second) {
doneSet.insert(path);
ClosureElems::const_iterator elem = fs.closure.elems.find(path); ClosureElems::const_iterator elem = fs.closure.elems.find(path);
if (elem == fs.closure.elems.end()) if (elem == fs.closure.elems.end())
throw Error(format("bad closure, missing path '%1%'") % path); throw Error(format("bad closure, missing path '%1%'") % path);
@ -104,8 +102,7 @@ void printDotGraph(ref<Store> store, const PathSet & roots)
Path path = *(workList.begin()); Path path = *(workList.begin());
workList.erase(path); workList.erase(path);
if (doneSet.find(path) != doneSet.end()) continue; if (!doneSet.insert(path).second) continue;
doneSet.insert(path);
cout << makeNode(path, symbolicName(path), "#ff0000"); cout << makeNode(path, symbolicName(path), "#ff0000");

View file

@ -242,11 +242,10 @@ const string treeNull = " ";
static void printTree(const Path & path, static void printTree(const Path & path,
const string & firstPad, const string & tailPad, PathSet & done) const string & firstPad, const string & tailPad, PathSet & done)
{ {
if (done.find(path) != done.end()) { if (!done.insert(path).second) {
cout << format("%1%%2% [...]\n") % firstPad % path; cout << format("%1%%2% [...]\n") % firstPad % path;
return; return;
} }
done.insert(path);
cout << format("%1%%2%\n") % firstPad % path; cout << format("%1%%2%\n") % firstPad % path;

View file

@ -118,8 +118,7 @@ struct CmdVerify : StorePathsCommand
auto doSigs = [&](StringSet sigs) { auto doSigs = [&](StringSet sigs) {
for (auto sig : sigs) { for (auto sig : sigs) {
if (sigsSeen.count(sig)) continue; if (!sigsSeen.insert(sig).second) continue;
sigsSeen.insert(sig);
if (validSigs < ValidPathInfo::maxSigs && info->checkSignature(publicKeys, sig)) if (validSigs < ValidPathInfo::maxSigs && info->checkSignature(publicKeys, sig))
validSigs++; validSigs++;
} }

View file

@ -117,9 +117,7 @@ Path resolveSymlink(const Path & path)
std::set<string> resolveTree(const Path & path, PathSet & deps) std::set<string> resolveTree(const Path & path, PathSet & deps)
{ {
std::set<string> results; std::set<string> results;
if (deps.count(path)) if (!deps.insert(path).second) return {};
return {};
deps.insert(path);
for (auto & lib : runResolver(path)) { for (auto & lib : runResolver(path)) {
results.insert(lib); results.insert(lib);
for (auto & p : resolveTree(lib, deps)) { for (auto & p : resolveTree(lib, deps)) {