From c4ea8c85f5005e9bd870ff5443e825d67e2c058e Mon Sep 17 00:00:00 2001 From: johnlenz Date: Fri, 2 Dec 2016 17:46:22 -0800 Subject: [PATCH] Avoid unnecessary "isEmpty" calls and iterators while working with scopes in NodeTraversal ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=140915014 --- .../javascript/jscomp/NodeTraversal.java | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/com/google/javascript/jscomp/NodeTraversal.java b/src/com/google/javascript/jscomp/NodeTraversal.java index 42b595d651c..a131b7e6548 100644 --- a/src/com/google/javascript/jscomp/NodeTraversal.java +++ b/src/com/google/javascript/jscomp/NodeTraversal.java @@ -24,7 +24,6 @@ import com.google.javascript.rhino.Token; import java.util.ArrayDeque; import java.util.Deque; -import java.util.Iterator; import java.util.Set; /** @@ -787,33 +786,29 @@ private void popScope(boolean quietly) { if (!quietly && scopeCallback != null) { scopeCallback.exitScope(this); } - Node scopeRoot; - if (scopeRoots.isEmpty()) { + Node scopeRoot = scopeRoots.pollFirst(); + if (scopeRoot == null) { scopeRoot = scopes.pop().getRootNode(); - } else { - scopeRoot = scopeRoots.pop(); } if (NodeUtil.isValidCfgRoot(scopeRoot)) { cfgs.pop(); } - if (hasScope()) { - compiler.setScope(getScopeRoot()); + Node newScopeRoot = getScopeRoot(); + if (newScopeRoot != null) { + compiler.setScope(newScopeRoot); } } /** Gets the current scope. */ public Scope getScope() { - Scope scope = scopes.isEmpty() ? null : scopes.peek(); - if (scopeRoots.isEmpty()) { - return scope; - } + Scope scope = scopes.peek(); - Iterator it = scopeRoots.descendingIterator(); - while (it.hasNext()) { - scope = scopeCreator.createScope(it.next(), scope); + Node root = null; + while ((root = scopeRoots.pollLast()) != null) { + scope = scopeCreator.createScope(root, scope); scopes.push(scope); } - scopeRoots.clear(); + // No need to call compiler.setScope; the top scopeRoot is now the top scope return scope; } @@ -851,10 +846,12 @@ public ControlFlowGraph getControlFlowGraph() { /** Returns the current scope's root. */ public Node getScopeRoot() { - if (scopeRoots.isEmpty()) { - return scopes.peek().getRootNode(); + Node root = scopeRoots.peek(); + if (root == null) { + Scope s = scopes.peek(); + return s != null ? s.getRootNode() : null; } else { - return scopeRoots.peek(); + return root; } } @@ -925,10 +922,6 @@ int getScopeDepth() { return sum - 1; // Use 0-based scope depth to be consistent within the compiler } - public boolean hasScope() { - return !(scopes.isEmpty() && scopeRoots.isEmpty()); - } - /** Reports a diagnostic (error or warning) */ public void report(Node n, DiagnosticType diagnosticType, String... arguments) {