Store::computeFSClosure(): Use thread pool

This speeds up queries against the binary cache.
This commit is contained in:
Eelco Dolstra 2016-07-21 14:13:35 +02:00
parent 77c2739c25
commit a728780fbd

View file

@ -2,6 +2,7 @@
#include "globals.hh" #include "globals.hh"
#include "local-store.hh" #include "local-store.hh"
#include "store-api.hh" #include "store-api.hh"
#include "thread-pool.hh"
namespace nix { namespace nix {
@ -10,43 +11,63 @@ namespace nix {
void Store::computeFSClosure(const Path & path, void Store::computeFSClosure(const Path & path,
PathSet & paths, bool flipDirection, bool includeOutputs, bool includeDerivers) PathSet & paths, bool flipDirection, bool includeOutputs, bool includeDerivers)
{ {
if (paths.find(path) != paths.end()) return; ThreadPool pool;
paths.insert(path);
PathSet edges; Sync<bool> state_;
std::function<void(Path)> doPath;
doPath = [&](const Path & path) {
{
auto state(state_.lock());
if (paths.count(path)) return;
paths.insert(path);
}
auto info = queryPathInfo(path);
if (flipDirection) { if (flipDirection) {
queryReferrers(path, edges);
PathSet referrers;
queryReferrers(path, referrers);
for (auto & ref : referrers)
if (ref != path)
pool.enqueue(std::bind(doPath, ref));
if (includeOutputs) { if (includeOutputs) {
PathSet derivers = queryValidDerivers(path); PathSet derivers = queryValidDerivers(path);
for (auto & i : derivers) for (auto & i : derivers)
edges.insert(i); pool.enqueue(std::bind(doPath, i));
} }
if (includeDerivers && isDerivation(path)) { if (includeDerivers && isDerivation(path)) {
PathSet outputs = queryDerivationOutputs(path); PathSet outputs = queryDerivationOutputs(path);
for (auto & i : outputs) for (auto & i : outputs)
if (isValidPath(i) && queryPathInfo(i)->deriver == path) if (isValidPath(i) && queryPathInfo(i)->deriver == path)
edges.insert(i); pool.enqueue(std::bind(doPath, i));
} }
} else { } else {
auto info = queryPathInfo(path);
edges = info->references; for (auto & ref : info->references)
if (ref != path)
pool.enqueue(std::bind(doPath, ref));
if (includeOutputs && isDerivation(path)) { if (includeOutputs && isDerivation(path)) {
PathSet outputs = queryDerivationOutputs(path); PathSet outputs = queryDerivationOutputs(path);
for (auto & i : outputs) for (auto & i : outputs)
if (isValidPath(i)) edges.insert(i); if (isValidPath(i)) pool.enqueue(std::bind(doPath, i));
} }
if (includeDerivers && isValidPath(info->deriver)) if (includeDerivers && isValidPath(info->deriver))
edges.insert(info->deriver); pool.enqueue(std::bind(doPath, info->deriver));
}
for (auto & i : edges) }
computeFSClosure(i, paths, flipDirection, includeOutputs, includeDerivers); };
pool.enqueue(std::bind(doPath, path));
pool.process();
} }