diff options
author | 2016-12-02 17:58:33 +0000 | |
---|---|---|
committer | 2016-12-02 19:09:07 +0000 | |
commit | e0a330577d9fe98169645cb68d9fc22cc787eeb6 (patch) | |
tree | 9b1d3dc955baa99133511b9134715e9c73649a85 /src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java | |
parent | a110ac400190c90a45856f15482c8d0952c542f5 (diff) |
For all function expressions of the form f(..., e1, ..., e2, ..., eK, ...), ensure that all of e1, e2, ..., and eK are elligble for parallel evaluation. This is _not_ the same as providing a parallel implementation of f, which we can do separately in followup CLs.
--
PiperOrigin-RevId: 140861694
MOS_MIGRATED_REVID=140861694
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java b/src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java index 482b4cd89b..6c37e41ff7 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java +++ b/src/main/java/com/google/devtools/build/lib/query2/engine/DepsFunction.java @@ -18,7 +18,7 @@ import com.google.common.collect.Sets; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.Argument; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.ArgumentType; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction; - +import com.google.devtools.build.lib.query2.engine.QueryUtil.ProcessorWithUniquifier; import java.util.Collection; import java.util.List; import java.util.Set; @@ -50,39 +50,66 @@ final class DepsFunction implements QueryFunction { return ImmutableList.of(ArgumentType.EXPRESSION, ArgumentType.INTEGER); } + private static class ProcessorImpl<T> implements ProcessorWithUniquifier<T> { + private final QueryExpression expression; + private final int depthBound; + private final QueryEnvironment<T> env; + + private ProcessorImpl(QueryExpression expression, int depthBound, QueryEnvironment<T> env) { + this.expression = expression; + this.depthBound = depthBound; + this.env = env; + } + + @Override + public void process(Iterable<T> partialResult, Uniquifier<T> uniquifier, Callback<T> callback) + throws QueryException, InterruptedException { + Collection<T> current = Sets.newHashSet(partialResult); + env.buildTransitiveClosure(expression, (Set<T>) current, depthBound); + + // We need to iterate depthBound + 1 times. + for (int i = 0; i <= depthBound; i++) { + // Filter already visited nodes: if we see a node in a later round, then we don't need to + // visit it again, because the depth at which we see it at must be greater than or equal + // to the last visit. + ImmutableList<T> toProcess = uniquifier.unique(current); + callback.process(toProcess); + current = ImmutableList.copyOf(env.getFwdDeps(toProcess)); + if (current.isEmpty()) { + // Exit when there are no more nodes to visit. + break; + } + } + } + } + + private static <T> void doEval( + QueryEnvironment<T> env, + VariableContext<T> context, + QueryExpression expression, + List<Argument> args, + Callback<T> callback) throws QueryException, InterruptedException { + int depthBound = args.size() > 1 ? args.get(1).getInteger() : Integer.MAX_VALUE; + env.eval( + args.get(0).getExpression(), + context, + QueryUtil.compose( + new ProcessorImpl<T>(expression, depthBound, env), + env.createUniquifier(), + callback)); + } + /** * Breadth-first search from the arguments. */ @Override public <T> void eval( - final QueryEnvironment<T> env, + QueryEnvironment<T> env, VariableContext<T> context, - final QueryExpression expression, + QueryExpression expression, List<Argument> args, - final Callback<T> callback) throws QueryException, InterruptedException { - final int depthBound = args.size() > 1 ? args.get(1).getInteger() : Integer.MAX_VALUE; - final Uniquifier<T> uniquifier = env.createUniquifier(); - env.eval(args.get(0).getExpression(), context, new Callback<T>() { - @Override - public void process(Iterable<T> partialResult) throws QueryException, InterruptedException { - Collection<T> current = Sets.newHashSet(partialResult); - env.buildTransitiveClosure(expression, (Set<T>) current, depthBound); - - // We need to iterate depthBound + 1 times. - for (int i = 0; i <= depthBound; i++) { - // Filter already visited nodes: if we see a node in a later round, then we don't need to - // visit it again, because the depth at which we see it at must be greater than or equal - // to the last visit. - ImmutableList<T> toProcess = uniquifier.unique(current); - callback.process(toProcess); - current = ImmutableList.copyOf(env.getFwdDeps(toProcess)); - if (current.isEmpty()) { - // Exit when there are no more nodes to visit. - break; - } - } - } - }); + Callback<T> callback) throws QueryException, InterruptedException { + doEval(env, context, expression, args, callback); } @Override @@ -93,6 +120,6 @@ final class DepsFunction implements QueryFunction { List<Argument> args, ThreadSafeCallback<T> callback, ForkJoinPool forkJoinPool) throws QueryException, InterruptedException { - eval(env, context, expression, args, callback); + doEval(env, context, expression, args, callback); } } |