* Main the `substitutes-rev' table again, but now in a way that

doesn't take \Theta(n^2) space/time complexity.
This commit is contained in:
Eelco Dolstra 2004-08-31 16:13:10 +00:00
parent c25f2883b1
commit 5c443b6550
3 changed files with 40 additions and 21 deletions

View file

@ -322,29 +322,44 @@ static void writeSubstitutes(const Transaction & txn,
} }
void registerSubstitute(const Transaction & txn, typedef map<Path, Paths> SubstitutesRev;
const Path & srcPath, const Substitute & sub)
void registerSubstitutes(const Transaction & txn,
const SubstitutePairs & subPairs)
{ {
SubstitutesRev revMap;
for (SubstitutePairs::const_iterator i = subPairs.begin();
i != subPairs.end(); ++i)
{
const Path & srcPath(i->first);
const Substitute & sub(i->second);
assertStorePath(srcPath); assertStorePath(srcPath);
assertStorePath(sub.storeExpr); assertStorePath(sub.storeExpr);
Substitutes subs = readSubstitutes(txn, srcPath); Substitutes subs = readSubstitutes(txn, srcPath);
if (find(subs.begin(), subs.end(), sub) != subs.end()) { /* New substitutes take precedence over old ones. If the
/* Nothing to do if the substitute is already known. */ substitute is already present, it's moved to the front. */
return; remove(subs.begin(), subs.end(), sub);
} subs.push_front(sub);
subs.push_front(sub); /* new substitutes take precedence */
writeSubstitutes(txn, srcPath, subs); writeSubstitutes(txn, srcPath, subs);
Paths revs; Paths & revs = revMap[sub.storeExpr];
if (revs.empty())
nixDB.queryStrings(txn, dbSubstitutesRev, sub.storeExpr, revs); nixDB.queryStrings(txn, dbSubstitutesRev, sub.storeExpr, revs);
if (find(revs.begin(), revs.end(), srcPath) == revs.end()) if (find(revs.begin(), revs.end(), srcPath) == revs.end())
revs.push_back(srcPath); revs.push_back(srcPath);
}
// !!! O(n^2) complexity in building this /* Re-write the reverse mapping in one go to prevent Theta(n^2)
// nixDB.setStrings(txn, dbSubstitutesRev, sub.storeExpr, revs); performance. (This would occur because the data fields of the
`substitutes-rev' table are lists). */
for (SubstitutesRev::iterator i = revMap.begin(); i != revMap.end(); ++i)
nixDB.setStrings(txn, dbSubstitutesRev, i->first, i->second);
} }

View file

@ -66,8 +66,9 @@ bool querySuccessor(const Path & srcPath, Path & sucPath);
Paths queryPredecessors(const Path & sucPath); Paths queryPredecessors(const Path & sucPath);
/* Register a substitute. */ /* Register a substitute. */
void registerSubstitute(const Transaction & txn, typedef list<pair<Path, Substitute> > SubstitutePairs;
const Path & srcPath, const Substitute & sub); void registerSubstitutes(const Transaction & txn,
const SubstitutePairs & subPairs);
/* Return the substitutes expression for the given path. */ /* Return the substitutes expression for the given path. */
Substitutes querySubstitutes(const Path & srcPath); Substitutes querySubstitutes(const Path & srcPath);

View file

@ -158,6 +158,7 @@ static void opSubstitute(Strings opFlags, Strings opArgs)
if (!opArgs.empty()) if (!opArgs.empty())
throw UsageError("no arguments expected"); throw UsageError("no arguments expected");
SubstitutePairs subPairs;
Transaction txn; Transaction txn;
createStoreTransaction(txn); createStoreTransaction(txn);
@ -179,9 +180,11 @@ static void opSubstitute(Strings opFlags, Strings opArgs)
sub.args.push_back(s); sub.args.push_back(s);
} }
if (!cin || cin.eof()) throw Error("missing input"); if (!cin || cin.eof()) throw Error("missing input");
registerSubstitute(txn, srcPath, sub); subPairs.push_back(pair<Path, Substitute>(srcPath, sub));
} }
registerSubstitutes(txn, subPairs);
txn.commit(); txn.commit();
} }