Reduce iterator usage on hot code paths
Micro-optimize a few hot code paths which have a habit of iterating over short O(1)-access
lists.
RELNOTES: None
PiperOrigin-RevId: 171347524
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Eval.java b/src/main/java/com/google/devtools/build/lib/syntax/Eval.java
index e0fdbff..4842374 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Eval.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Eval.java
@@ -14,6 +14,7 @@
package com.google.devtools.build.lib.syntax;
+import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -58,9 +59,7 @@
void execIfBranch(IfStatement.ConditionalStatements node)
throws EvalException, InterruptedException {
- for (Statement stmt : node.getStatements()) {
- exec(stmt);
- }
+ execStatements(node.getStatements());
}
void execFor(ForStatement node) throws EvalException, InterruptedException {
@@ -72,9 +71,7 @@
node.getVariable().assign(it, env, node.getLocation());
try {
- for (Statement stmt : node.getBlock()) {
- exec(stmt);
- }
+ execStatements(node.getBlock());
} catch (FlowException ex) {
if (ex == breakException) {
return;
@@ -123,9 +120,7 @@
return;
}
}
- for (Statement stmt : node.getElseBlock()) {
- exec(stmt);
- }
+ execStatements(node.getElseBlock());
}
void execLoad(LoadStatement node) throws EvalException, InterruptedException {
@@ -216,4 +211,12 @@
break;
}
}
+
+ private void execStatements(ImmutableList<Statement> statements)
+ throws EvalException, InterruptedException {
+ // Hot code path, good chance of short lists which don't justify the iterator overhead.
+ for (int i = 0; i < statements.size(); i++) {
+ exec(statements.get(i));
+ }
+ }
}