Skip to content

Commit

Permalink
During a node traversal, it is known that the "inputId" does not chan…
Browse files Browse the repository at this point in the history
…ge between the script nodes, avoid a hash lookup every time "getInput" is called by caching it.

Note: the whole concept of InputId<-->Input was created to support the original Dart to JS compiler (DartC) which needed support for multiple inputs with the same original file name.  While this is a useful concept it isn't currently used in any meaningful way and it might be possible to eliminate the whole abstraction.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133675962
  • Loading branch information
concavelenz authored and blickly committed Sep 20, 2016
1 parent 3a2fea1 commit 8df87fa
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/com/google/javascript/jscomp/NodeTraversal.java
Expand Up @@ -76,6 +76,7 @@ public class NodeTraversal {

/** The current input */
private InputId inputId;
private CompilerInput compilerInput;

/** The scope creator */
private final ScopeCreator scopeCreator;
Expand Down Expand Up @@ -281,8 +282,7 @@ public NodeTraversal(AbstractCompiler compiler, Callback cb,
this.scopeCallback = (ScopedCallback) cb;
}
this.compiler = compiler;
this.inputId = null;
this.sourceName = "";
setInputId(null, "");
this.scopeCreator = scopeCreator;
this.useBlockScope = scopeCreator.hasBlockScope();
}
Expand Down Expand Up @@ -319,8 +319,7 @@ private String formatNodeContext(String label, Node n) {
*/
public void traverse(Node root) {
try {
inputId = NodeUtil.getInputId(root);
sourceName = "";
setInputId(NodeUtil.getInputId(root), "");
curNode = root;
pushScope(root);
// null parent ensures that the shallow callbacks will traverse root
Expand All @@ -336,8 +335,7 @@ void traverseRoots(Node externs, Node root) {
Node scopeRoot = externs.getParent();
Preconditions.checkNotNull(scopeRoot);

inputId = NodeUtil.getInputId(scopeRoot);
sourceName = "";
setInputId(NodeUtil.getInputId(scopeRoot), "");
curNode = scopeRoot;
pushScope(scopeRoot);

Expand Down Expand Up @@ -377,8 +375,7 @@ private String formatNodePosition(Node n) {
void traverseWithScope(Node root, Scope s) {
Preconditions.checkState(s.isGlobal());
try {
inputId = null;
sourceName = "";
setInputId(null, "");
curNode = root;
pushScope(s);
traverseBranch(root, null);
Expand All @@ -398,9 +395,8 @@ void traverseAtScope(Scope s) {
// We need to do some extra magic to make sure that the scope doesn't
// get re-created when we dive into the function.
if (inputId == null) {
inputId = NodeUtil.getInputId(n);
setInputId(NodeUtil.getInputId(n), getSourceName(n));
}
sourceName = getSourceName(n);
curNode = n;
pushScope(s);

Expand All @@ -412,9 +408,8 @@ void traverseAtScope(Scope s) {
popScope();
} else if (n.isBlock()) {
if (inputId == null) {
inputId = NodeUtil.getInputId(n);
setInputId(NodeUtil.getInputId(n), getSourceName(n));
}
sourceName = getSourceName(n);
curNode = n;
pushScope(s);

Expand All @@ -440,7 +435,7 @@ public void traverseFunctionOutOfBand(Node node, Scope scope) {
Preconditions.checkState(node.isFunction());
Preconditions.checkState(scope.getRootNode() != null);
if (inputId == null) {
inputId = NodeUtil.getInputId(node);
setInputId(NodeUtil.getInputId(node), getSourceName(node));
}
curNode = node.getParent();
pushScope(scope, true /* quietly */);
Expand All @@ -461,7 +456,7 @@ public void traverseFunctionOutOfBand(Node node, Scope scope) {
void traverseInnerNode(Node node, Node parent, Scope refinedScope) {
Preconditions.checkNotNull(parent);
if (inputId == null) {
inputId = NodeUtil.getInputId(node);
setInputId(NodeUtil.getInputId(node), getSourceName(node));
}
if (refinedScope != null && getScope() != refinedScope) {
curNode = node;
Expand Down Expand Up @@ -522,7 +517,10 @@ public String getSourceName() {
* Gets the current input source.
*/
public CompilerInput getInput() {
return compiler.getInput(inputId);
if (compilerInput == null) {
compilerInput = compiler.getInput(inputId);
}
return compilerInput;
}

/**
Expand Down Expand Up @@ -620,8 +618,7 @@ public static void traverseRootsTyped(
private void traverseBranch(Node n, Node parent) {
Token type = n.getToken();
if (type == Token.SCRIPT) {
inputId = n.getInputId();
sourceName = getSourceName(n);
setInputId(n.getInputId(), getSourceName(n));
}

curNode = n;
Expand Down Expand Up @@ -934,6 +931,12 @@ private static String getSourceName(Node n) {
return nullToEmpty(name);
}

private void setInputId(InputId id, String sourceName) {
inputId = id;
this.sourceName = sourceName;
compilerInput = null;
}

InputId getInputId() {
return inputId;
}
Expand Down

0 comments on commit 8df87fa

Please sign in to comment.