Skip to content

Commit

Permalink
Avoid unnecessary "isEmpty" calls and iterators while working with sc…
Browse files Browse the repository at this point in the history
…opes in NodeTraversal

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=140915014
  • Loading branch information
concavelenz authored and blickly committed Dec 5, 2016
1 parent 2cd40af commit c4ea8c8
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/com/google/javascript/jscomp/NodeTraversal.java
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<Node> 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;
}
Expand Down Expand Up @@ -851,10 +846,12 @@ public ControlFlowGraph<Node> 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;
}
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c4ea8c8

Please sign in to comment.