From 8df87fa80e0a00ef32c852a4b502e5e181974387 Mon Sep 17 00:00:00 2001 From: johnlenz Date: Tue, 20 Sep 2016 01:00:19 -0700 Subject: [PATCH] During a node traversal, it is known that the "inputId" does not change 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 --- .../javascript/jscomp/NodeTraversal.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/com/google/javascript/jscomp/NodeTraversal.java b/src/com/google/javascript/jscomp/NodeTraversal.java index 6082ce5717c..e1bc2138f31 100644 --- a/src/com/google/javascript/jscomp/NodeTraversal.java +++ b/src/com/google/javascript/jscomp/NodeTraversal.java @@ -76,6 +76,7 @@ public class NodeTraversal { /** The current input */ private InputId inputId; + private CompilerInput compilerInput; /** The scope creator */ private final ScopeCreator scopeCreator; @@ -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(); } @@ -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 @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 */); @@ -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; @@ -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; } /** @@ -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; @@ -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; }