Skip to content

Commit

Permalink
Speed up the Node.matchesQualifiedName() calls in ClosureRewriteModule
Browse files Browse the repository at this point in the history
Reduce the number of calls and replace them with the version that takes in a Node.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149023980
  • Loading branch information
Dominator008 authored and brad4d committed Mar 3, 2017
1 parent af0857a commit 86e3dd0
Showing 1 changed file with 61 additions and 31 deletions.
92 changes: 61 additions & 31 deletions src/com/google/javascript/jscomp/ClosureRewriteModule.java
Expand Up @@ -181,6 +181,17 @@ final class ClosureRewriteModule implements HotSwapCompilerPass {


private static final String MODULE_CONTENTS_PREFIX = "module$contents$"; private static final String MODULE_CONTENTS_PREFIX = "module$contents$";


// Prebuilt Nodes to speed up Node.matchesQualifiedName() calls
private static final Node GOOG_FORWARDDECLARE =
IR.getprop(IR.name("goog"), IR.string("forwardDeclare"));
private static final Node GOOG_LOADMODULE = IR.getprop(IR.name("goog"), IR.string("loadModule"));
private static final Node GOOG_MODULE = IR.getprop(IR.name("goog"), IR.string("module"));
private static final Node GOOG_MODULE_DECLARELEGACYNAMESPACE =
IR.getprop(GOOG_MODULE, IR.string("declareLegacyNamespace"));
private static final Node GOOG_MODULE_GET = IR.getprop(GOOG_MODULE.cloneTree(), IR.string("get"));
private static final Node GOOG_PROVIDE = IR.getprop(IR.name("goog"), IR.string("provide"));
private static final Node GOOG_REQUIRE = IR.getprop(IR.name("goog"), IR.string("require"));

private final AbstractCompiler compiler; private final AbstractCompiler compiler;
private final PreprocessorSymbolTable preprocessorSymbolTable; private final PreprocessorSymbolTable preprocessorSymbolTable;


Expand Down Expand Up @@ -222,7 +233,7 @@ private static final class ExportDefinition {
// Null if the export is of anything other than a name // Null if the export is of anything other than a name
@Nullable Var nameDecl; @Nullable Var nameDecl;


private static final Set<Token> INLINABLE_NAME_PARENTS = private static final ImmutableSet<Token> INLINABLE_NAME_PARENTS =
ImmutableSet.of(Token.VAR, Token.CONST, Token.LET, Token.FUNCTION, Token.CLASS); ImmutableSet.of(Token.VAR, Token.CONST, Token.LET, Token.FUNCTION, Token.CLASS);


static ExportDefinition newDefaultExport(NodeTraversal t, Node rhs) { static ExportDefinition newDefaultExport(NodeTraversal t, Node rhs) {
Expand Down Expand Up @@ -253,12 +264,19 @@ boolean hasInlinableName(Set<Var> exportedNames) {
return false; return false;
} }
Node initialValue = nameDecl.getInitialValue(); Node initialValue = nameDecl.getInitialValue();
if (initialValue == null) { if (initialValue == null || !initialValue.isCall()) {
return true;
}
Node method = initialValue.getFirstChild();
if (!method.isGetProp()) {
return true; return true;
} }
return !NodeUtil.isCallTo(initialValue, "goog.require") Node maybeGoog = method.getFirstChild();
&& !NodeUtil.isCallTo(initialValue, "goog.forwardDeclare") if (!maybeGoog.isName() || !maybeGoog.getString().equals("goog")) {
&& !NodeUtil.isCallTo(initialValue, "goog.getMsg"); return true;
}
String name = maybeGoog.getNext().getString();
return !name.equals("require") && !name.equals("forwardDeclare") && !name.equals("getMsg");
} }


String getLocalName() { String getLocalName() {
Expand Down Expand Up @@ -327,25 +345,23 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {


switch (n.getToken()) { switch (n.getToken()) {
case CALL: case CALL:
if (NodeUtil.isCallTo(n, "goog.loadModule") && n.getLastChild().isFunction()) { Node method = n.getFirstChild();
recordGoogLoadModule(n); if (!method.isGetProp()) {
break;
} }
if (NodeUtil.isCallTo(n, "goog.module")) { if (method.matchesQualifiedName(GOOG_LOADMODULE) && n.getLastChild().isFunction()) {
recordGoogLoadModule(n);
} else if (method.matchesQualifiedName(GOOG_MODULE)) {
recordGoogModule(t, n); recordGoogModule(t, n);
} } else if (method.matchesQualifiedName(GOOG_MODULE_DECLARELEGACYNAMESPACE)) {
if (NodeUtil.isCallTo(n, "goog.module.declareLegacyNamespace")) {
recordGoogDeclareLegacyNamespace(); recordGoogDeclareLegacyNamespace();
} } else if (method.matchesQualifiedName(GOOG_PROVIDE)) {
if (NodeUtil.isCallTo(n, "goog.provide")) {
recordGoogProvide(t, n); recordGoogProvide(t, n);
} } else if (method.matchesQualifiedName(GOOG_REQUIRE)) {
if (NodeUtil.isCallTo(n, "goog.require")) {
recordGoogRequire(t, n, true /** mustBeOrdered */); recordGoogRequire(t, n, true /** mustBeOrdered */);
} } else if (method.matchesQualifiedName(GOOG_FORWARDDECLARE) && !parent.isExprResult()) {
if (NodeUtil.isCallTo(n, "goog.forwardDeclare") && !parent.isExprResult()) {
recordGoogForwardDeclare(t, n); recordGoogForwardDeclare(t, n);
} } else if (method.matchesQualifiedName(GOOG_MODULE_GET)) {
if (NodeUtil.isCallTo(n, "goog.module.get")) {
recordGoogModuleGet(t, n); recordGoogModuleGet(t, n);
} }
break; break;
Expand Down Expand Up @@ -406,19 +422,19 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
break; break;


case CALL: case CALL:
if (NodeUtil.isCallTo(n, "goog.module")) { Node method = n.getFirstChild();
updateGoogModule(n); if (!method.isGetProp()) {
break;
} }
if (NodeUtil.isCallTo(n, "goog.module.declareLegacyNamespace")) { if (method.matchesQualifiedName(GOOG_MODULE)) {
updateGoogModule(n);
} else if (method.matchesQualifiedName(GOOG_MODULE_DECLARELEGACYNAMESPACE)) {
updateGoogDeclareLegacyNamespace(n); updateGoogDeclareLegacyNamespace(n);
} } else if (method.matchesQualifiedName(GOOG_REQUIRE)) {
if (NodeUtil.isCallTo(n, "goog.require")) {
updateGoogRequire(t, n); updateGoogRequire(t, n);
} } else if (method.matchesQualifiedName(GOOG_FORWARDDECLARE) && !parent.isExprResult()) {
if (NodeUtil.isCallTo(n, "goog.forwardDeclare") && !parent.isExprResult()) {
updateGoogForwardDeclare(t, n); updateGoogForwardDeclare(t, n);
} } else if (method.matchesQualifiedName(GOOG_MODULE_GET)) {
if (NodeUtil.isCallTo(n, "goog.module.get")) {
updateGoogModuleGetCall(n); updateGoogModuleGetCall(n);
} }
break; break;
Expand Down Expand Up @@ -474,8 +490,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
private static boolean isGoogLoadModuleStatement(Node exprResult) { private static boolean isGoogLoadModuleStatement(Node exprResult) {
Preconditions.checkArgument(exprResult.isExprResult()); Preconditions.checkArgument(exprResult.isExprResult());
Node call = exprResult.getFirstChild(); Node call = exprResult.getFirstChild();
return call != null return call != null && isCallTo(call, GOOG_LOADMODULE) && call.getLastChild().isFunction();
&& NodeUtil.isCallTo(call, "goog.loadModule") && call.getLastChild().isFunction();
} }


/** /**
Expand Down Expand Up @@ -815,7 +830,7 @@ private void recordGoogModuleGet(NodeTraversal t, Node call) {
// Even if it was to a var in our scope it should still only rewrite if the var looked like: // Even if it was to a var in our scope it should still only rewrite if the var looked like:
// let x = goog.forwardDeclare('a.namespace'); // let x = goog.forwardDeclare('a.namespace');
Node aliasVarNodeRhs = NodeUtil.getRValueOfLValue(aliasVar.getNode()); Node aliasVarNodeRhs = NodeUtil.getRValueOfLValue(aliasVar.getNode());
if (aliasVarNodeRhs == null || !NodeUtil.isCallTo(aliasVarNodeRhs, "goog.forwardDeclare")) { if (aliasVarNodeRhs == null || !isCallTo(aliasVarNodeRhs, GOOG_FORWARDDECLARE)) {
t.report(call, INVALID_GET_ALIAS); t.report(call, INVALID_GET_ALIAS);
return; return;
} }
Expand Down Expand Up @@ -1180,7 +1195,7 @@ private void maybeUpdateTopLevelName(NodeTraversal t, Node nameNode) {
&& nameNode.getParent().isStringKey() && nameNode.getParent().isStringKey()
&& nameNode.getGrandparent().isObjectPattern()) { && nameNode.getGrandparent().isObjectPattern()) {
Node destructuringLhsNode = nameNode.getGrandparent().getParent(); Node destructuringLhsNode = nameNode.getGrandparent().getParent();
if (NodeUtil.isCallTo(destructuringLhsNode.getLastChild(), "goog.require")) { if (isCallTo(destructuringLhsNode.getLastChild(), GOOG_REQUIRE)) {
return; return;
} }
} }
Expand Down Expand Up @@ -1638,4 +1653,19 @@ private static Node createNamespaceNode(Node n) {
node.putBooleanProp(Node.IS_MODULE_NAME, true); node.putBooleanProp(Node.IS_MODULE_NAME, true);
return node; return node;
} }

/**
* A faster version of NodeUtil.isCallTo() for methods in the GETPROP form.
*
* @param n The CALL node to be checked.
* @param targetMethod A prebuilt GETPROP node representing a target method.
* @return Whether n is a call to the target method.
*/
private static boolean isCallTo(Node n, Node targetMethod) {
if (!n.isCall()) {
return false;
}
Node method = n.getFirstChild();
return method.isGetProp() && method.matchesQualifiedName(targetMethod);
}
} }

0 comments on commit 86e3dd0

Please sign in to comment.