From 87084b21e26c8273a25a45ff509d1971b6aba019 Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Mon, 31 Jul 2017 14:29:17 -0700 Subject: [PATCH] Some minor cleanup/performance fixes ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=163748601 --- .../google/javascript/jscomp/AbstractCompiler.java | 2 +- src/com/google/javascript/jscomp/Compiler.java | 7 ++++--- .../google/javascript/jscomp/FunctionInjector.java | 13 +++++-------- .../google/javascript/jscomp/InlineFunctions.java | 2 +- src/com/google/javascript/jscomp/Normalize.java | 2 +- .../google/javascript/jscomp/ProcessDefines.java | 2 +- .../google/javascript/jscomp/RemoveUnusedVars.java | 2 +- src/com/google/javascript/jscomp/RenameLabels.java | 2 +- .../google/javascript/jscomp/IntegrationTest.java | 5 ++--- 9 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/com/google/javascript/jscomp/AbstractCompiler.java b/src/com/google/javascript/jscomp/AbstractCompiler.java index 994bc39e966..b6d18c4a9b7 100644 --- a/src/com/google/javascript/jscomp/AbstractCompiler.java +++ b/src/com/google/javascript/jscomp/AbstractCompiler.java @@ -224,7 +224,7 @@ static enum MostRecentTypechecker { /** * Logs a message under a central logger. */ - abstract void addToDebugLog(String message); + abstract void addToDebugLog(String... message); /** * Sets the CssRenamingMap. diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 24d5310a9ff..9030e10edf7 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -2809,11 +2809,12 @@ public boolean hasErrors() { /** Called from the compiler passes, adds debug info */ @Override - void addToDebugLog(String str) { + void addToDebugLog(String... strings) { if (options.useDebugLog) { - debugLog.append(str); + String log = Joiner.on("").join(strings); + debugLog.append(log); debugLog.append('\n'); - logger.fine(str); + logger.fine(log); } } diff --git a/src/com/google/javascript/jscomp/FunctionInjector.java b/src/com/google/javascript/jscomp/FunctionInjector.java index b6bc307a20d..dc6d38fd652 100644 --- a/src/com/google/javascript/jscomp/FunctionInjector.java +++ b/src/com/google/javascript/jscomp/FunctionInjector.java @@ -802,8 +802,7 @@ boolean inliningLowersCost( // Check if any of the references cross the module boundaries. if (checkModules && ref.module != null) { - if (ref.module != fnModule && - !moduleGraph.dependsOn(ref.module, fnModule)) { + if (ref.module != fnModule && !moduleGraph.dependsOn(ref.module, fnModule)) { // Calculate the cost as if the function were non-removable, // if it still lowers the cost inline it. isRemovable = false; @@ -812,8 +811,7 @@ boolean inliningLowersCost( } } - int referencesUsingDirectInlining = referenceCount - - referencesUsingBlockInlining; + int referencesUsingDirectInlining = referenceCount - referencesUsingBlockInlining; // Don't bother calculating the cost of function for simple functions where // possible. @@ -821,8 +819,7 @@ boolean inliningLowersCost( // larger than the original function if there are many returns (resulting // in additional assignments) or many parameters that need to be aliased // so use the cost estimating. - if (referenceCount == 1 && isRemovable && - referencesUsingDirectInlining == 1) { + if (referenceCount == 1 && isRemovable && referencesUsingDirectInlining == 1) { return true; } @@ -904,8 +901,8 @@ private static int inlineCostDelta( // "function xx(xx,xx){}" (15 + (param count * 3) -1; int paramCount = NodeUtil.getFunctionParameters(fnNode).getChildCount(); int commaCount = (paramCount > 1) ? paramCount - 1 : 0; - int costDeltaFunctionOverhead = 15 + commaCount + - (paramCount * InlineCostEstimator.ESTIMATED_IDENTIFIER_COST); + int costDeltaFunctionOverhead = + 15 + commaCount + (paramCount * InlineCostEstimator.ESTIMATED_IDENTIFIER_COST); Node block = fnNode.getLastChild(); if (!block.hasChildren()) { diff --git a/src/com/google/javascript/jscomp/InlineFunctions.java b/src/com/google/javascript/jscomp/InlineFunctions.java index c5eb04b7d0f..1dc97ad74ed 100644 --- a/src/com/google/javascript/jscomp/InlineFunctions.java +++ b/src/com/google/javascript/jscomp/InlineFunctions.java @@ -630,7 +630,7 @@ private void inlineFunction(NodeTraversal t, Reference ref, FunctionState functi if (!newExpr.isEquivalentTo(ref.callNode)) { t.getCompiler().reportChangeToEnclosingScope(newExpr); } - t.getCompiler().addToDebugLog("Inlined function: " + fn.getName()); + t.getCompiler().addToDebugLog("Inlined function: ", fn.getName()); } } diff --git a/src/com/google/javascript/jscomp/Normalize.java b/src/com/google/javascript/jscomp/Normalize.java index 37fd8bbe631..cf6b49a6b9f 100644 --- a/src/com/google/javascript/jscomp/Normalize.java +++ b/src/com/google/javascript/jscomp/Normalize.java @@ -209,7 +209,7 @@ public void process(Node externs, Node root) { @Override public void visit(NodeTraversal t, Node n, Node parent) { // Note: Constant properties annotations are not propagated. - if ((n.isName() || n.isStringKey())) { + if (n.isName() || n.isStringKey()) { if (n.getString().isEmpty()) { return; } diff --git a/src/com/google/javascript/jscomp/ProcessDefines.java b/src/com/google/javascript/jscomp/ProcessDefines.java index 26bb6c609a9..fc340ea986e 100644 --- a/src/com/google/javascript/jscomp/ProcessDefines.java +++ b/src/com/google/javascript/jscomp/ProcessDefines.java @@ -129,7 +129,7 @@ private void overrideDefines(Map allDefines) { Node finalValue = inputValue != null ? inputValue : info.getLastValue(); if (finalValue != info.initialValue) { - compiler.addToDebugLog("Overriding @define variable " + defineName); + compiler.addToDebugLog("Overriding @define variable ", defineName); boolean changed = finalValue.getToken() != info.initialValue.getToken() || !finalValue.isEquivalentTo(info.initialValue); diff --git a/src/com/google/javascript/jscomp/RemoveUnusedVars.java b/src/com/google/javascript/jscomp/RemoveUnusedVars.java index 6abac4ef968..9917ac09c48 100644 --- a/src/com/google/javascript/jscomp/RemoveUnusedVars.java +++ b/src/com/google/javascript/jscomp/RemoveUnusedVars.java @@ -889,7 +889,7 @@ private void removeUnreferencedVars() { // to other unreferenced variables. removeAllAssigns(var); - compiler.addToDebugLog("Unreferenced var: " + var.name); + compiler.addToDebugLog("Unreferenced var: ", var.name); Node nameNode = var.nameNode; Node toRemove = nameNode.getParent(); Node parent = toRemove.getParent(); diff --git a/src/com/google/javascript/jscomp/RenameLabels.java b/src/com/google/javascript/jscomp/RenameLabels.java index 5f5809fa6ff..c2106e24051 100644 --- a/src/com/google/javascript/jscomp/RenameLabels.java +++ b/src/com/google/javascript/jscomp/RenameLabels.java @@ -169,7 +169,7 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node, } String newName = getNameForId(currentDepth); - compiler.addToDebugLog("label renamed: " + name + " => " + newName); + compiler.addToDebugLog("label renamed: ", name, " => ", newName); } return true; diff --git a/test/com/google/javascript/jscomp/IntegrationTest.java b/test/com/google/javascript/jscomp/IntegrationTest.java index 97e4b9dee71..4e0f38221e8 100644 --- a/test/com/google/javascript/jscomp/IntegrationTest.java +++ b/test/com/google/javascript/jscomp/IntegrationTest.java @@ -3185,8 +3185,7 @@ public void testIssue1198() throws Exception { // http://blickly.github.io/closure-compiler-issues/#1131 public void testIssue1131() { CompilerOptions options = createCompilerOptions(); - CompilationLevel.ADVANCED_OPTIMIZATIONS - .setOptionsForCompilationLevel(options); + CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options); test(options, "function f(k) { return k(k); } alert(f(f));", "function a(b) { return b(b); } alert(a(a));"); @@ -3311,7 +3310,7 @@ public void process(Node externs, Node root) { "function f() { return x + z; }"); fail("Expected run-time exception"); } catch (RuntimeException e) { - assertThat(e.getMessage()).contains("Unexpected variable x"); + assertThat(e).hasMessageThat().contains("Unexpected variable x"); } }