Skip to content

Commit

Permalink
Remove two boolean arguments to the InlineFunctions pass constructor.
Browse files Browse the repository at this point in the history
Replace the inlineGlobalFunctions and inlineLocalFunctions booleans with
a single Reach enum instead.  Once remaining references to the public booleans
in the CompilerOptions are removed, we can clean up the representation in
CompilerOptions as well.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179122024
  • Loading branch information
blickly authored and brad4d committed Dec 15, 2017
1 parent 110b3b7 commit 3d9ecb4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 56 deletions.
54 changes: 39 additions & 15 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -72,7 +72,11 @@ public class CompilerOptions implements Serializable {
public enum Reach { public enum Reach {
ALL, ALL,
LOCAL_ONLY, LOCAL_ONLY,
NONE NONE;

boolean includesGlobals() {
return this == ALL;
}
} }


public enum PropertyCollapseLevel { public enum PropertyCollapseLevel {
Expand Down Expand Up @@ -428,19 +432,13 @@ public void setNumParallelThreads(int parallelism) {
/** Inlines constants (symbols that are all CAPS) */ /** Inlines constants (symbols that are all CAPS) */
public boolean inlineConstantVars; public boolean inlineConstantVars;


/** Inlines global functions */
public boolean inlineFunctions;

/** /**
* For projects that want to avoid the creation of giant functions after * For projects that want to avoid the creation of giant functions after
* inlining. * inlining.
*/ */
int maxFunctionSizeAfterInlining; int maxFunctionSizeAfterInlining;
static final int UNLIMITED_FUN_SIZE_AFTER_INLINING = -1; static final int UNLIMITED_FUN_SIZE_AFTER_INLINING = -1;


/** Inlines functions defined in local scopes */
public boolean inlineLocalFunctions;

/** More aggressive function inlining */ /** More aggressive function inlining */
boolean assumeClosuresOnlyCaptureReferences; boolean assumeClosuresOnlyCaptureReferences;


Expand Down Expand Up @@ -1633,6 +1631,27 @@ public void setIdGeneratorsMap(String previousMappings) {
this.idGeneratorsMapSerialized = previousMappings; this.idGeneratorsMapSerialized = previousMappings;
} }


/** Use {@link #getInlineFunctionsLevel()} instead */
@Deprecated
public boolean inlineFunctions;

/** Use {@link #getInlineFunctionsLevel()} instead */
@Deprecated
public boolean inlineLocalFunctions;

/** Use {@link #setInlineFunctions(Reach)} instead */
@Deprecated
public void setInlineFunctions(boolean inlineFunctions) {
this.inlineFunctions = inlineFunctions;
}

/** Use {@link #setInlineFunctions(Reach)} instead */
@Deprecated
public void setInlineLocalFunctions(boolean inlineLocalFunctions) {
this.inlineLocalFunctions = inlineLocalFunctions;
}


/** /**
* Set the function inlining policy for the compiler. * Set the function inlining policy for the compiler.
*/ */
Expand All @@ -1655,6 +1674,19 @@ public void setInlineFunctions(Reach reach) {
} }
} }


/**
* Get the function inlining policy for the compiler.
*/
public Reach getInlineFunctionsLevel() {
if (this.inlineFunctions) {
return Reach.ALL;
}
if (this.inlineLocalFunctions) {
return Reach.LOCAL_ONLY;
}
return Reach.NONE;
}

public void setMaxFunctionSizeAfterInlining(int funAstSize) { public void setMaxFunctionSizeAfterInlining(int funAstSize) {
checkArgument(funAstSize > 0); checkArgument(funAstSize > 0);
this.maxFunctionSizeAfterInlining = funAstSize; this.maxFunctionSizeAfterInlining = funAstSize;
Expand Down Expand Up @@ -2226,14 +2258,6 @@ public void setInlineConstantVars(boolean inlineConstantVars) {
this.inlineConstantVars = inlineConstantVars; this.inlineConstantVars = inlineConstantVars;
} }


public void setInlineFunctions(boolean inlineFunctions) {
this.inlineFunctions = inlineFunctions;
}

public void setInlineLocalFunctions(boolean inlineLocalFunctions) {
this.inlineLocalFunctions = inlineLocalFunctions;
}

public void setCrossModuleCodeMotion(boolean crossModuleCodeMotion) { public void setCrossModuleCodeMotion(boolean crossModuleCodeMotion) {
this.crossModuleCodeMotion = crossModuleCodeMotion; this.crossModuleCodeMotion = crossModuleCodeMotion;
} }
Expand Down
Expand Up @@ -48,12 +48,11 @@ static void preprocess(CompilerOptions options) {
+ "remove_unused_prototype_props to be turned on."); + "remove_unused_prototype_props to be turned on.");
} }


if (!options.inlineFunctions if (options.getInlineFunctionsLevel() == CompilerOptions.Reach.NONE
&& options.maxFunctionSizeAfterInlining && options.maxFunctionSizeAfterInlining
!= CompilerOptions.UNLIMITED_FUN_SIZE_AFTER_INLINING) { != CompilerOptions.UNLIMITED_FUN_SIZE_AFTER_INLINING) {
throw new InvalidOptionsException( throw new InvalidOptionsException(
"max_function_size_after_inlining has no effect if inlining is" "max_function_size_after_inlining has no effect if inlining is disabled.");
+ " disabled.");
} }


if (options.getNewTypeInference()) { if (options.getNewTypeInference()) {
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -34,6 +34,7 @@
import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage; import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage;
import com.google.javascript.jscomp.AbstractCompiler.MostRecentTypechecker; import com.google.javascript.jscomp.AbstractCompiler.MostRecentTypechecker;
import com.google.javascript.jscomp.CompilerOptions.ExtractPrototypeMemberDeclarationsMode; import com.google.javascript.jscomp.CompilerOptions.ExtractPrototypeMemberDeclarationsMode;
import com.google.javascript.jscomp.CompilerOptions.Reach;
import com.google.javascript.jscomp.CoverageInstrumentationPass.CoverageReach; import com.google.javascript.jscomp.CoverageInstrumentationPass.CoverageReach;
import com.google.javascript.jscomp.CoverageInstrumentationPass.InstrumentOption; import com.google.javascript.jscomp.CoverageInstrumentationPass.InstrumentOption;
import com.google.javascript.jscomp.ExtractPrototypeMemberDeclarations.Pattern; import com.google.javascript.jscomp.ExtractPrototypeMemberDeclarations.Pattern;
Expand Down Expand Up @@ -982,7 +983,7 @@ private List<PassFactory> getMainOptimizationLoop() {


passes.addAll(getCodeRemovingPasses()); passes.addAll(getCodeRemovingPasses());


if (options.inlineFunctions || options.inlineLocalFunctions) { if (options.getInlineFunctionsLevel() != Reach.NONE) {
passes.add(inlineFunctions); passes.add(inlineFunctions);
} }


Expand Down Expand Up @@ -2821,8 +2822,7 @@ protected CompilerPass create(AbstractCompiler compiler) {
return new InlineFunctions( return new InlineFunctions(
compiler, compiler,
compiler.getUniqueNameIdSupplier(), compiler.getUniqueNameIdSupplier(),
options.inlineFunctions, options.getInlineFunctionsLevel(),
options.inlineLocalFunctions,
options.assumeStrictThis() || options.expectStrictModeInput(), options.assumeStrictThis() || options.expectStrictModeInput(),
options.assumeClosuresOnlyCaptureReferences, options.assumeClosuresOnlyCaptureReferences,
options.maxFunctionSizeAfterInlining); options.maxFunctionSizeAfterInlining);
Expand Down
26 changes: 8 additions & 18 deletions src/com/google/javascript/jscomp/InlineFunctions.java
Expand Up @@ -24,10 +24,10 @@
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.CompilerOptions.Reach;
import com.google.javascript.jscomp.FunctionInjector.CanInlineResult; import com.google.javascript.jscomp.FunctionInjector.CanInlineResult;
import com.google.javascript.jscomp.FunctionInjector.InliningMode; import com.google.javascript.jscomp.FunctionInjector.InliningMode;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
Expand Down Expand Up @@ -67,8 +67,7 @@ class InlineFunctions implements CompilerPass {


private final FunctionInjector injector; private final FunctionInjector injector;


private final boolean inlineGlobalFunctions; private final Reach reach;
private final boolean inlineLocalFunctions;
private final boolean assumeMinimumCapture; private final boolean assumeMinimumCapture;


private final boolean enforceMaxSizeAfterInlining; private final boolean enforceMaxSizeAfterInlining;
Expand All @@ -77,17 +76,17 @@ class InlineFunctions implements CompilerPass {
InlineFunctions( InlineFunctions(
AbstractCompiler compiler, AbstractCompiler compiler,
Supplier<String> safeNameIdSupplier, Supplier<String> safeNameIdSupplier,
boolean inlineGlobalFunctions, Reach reach,
boolean inlineLocalFunctions,
boolean assumeStrictThis, boolean assumeStrictThis,
boolean assumeMinimumCapture, boolean assumeMinimumCapture,
int maxSizeAfterInlining) { int maxSizeAfterInlining) {
checkArgument(compiler != null); checkArgument(compiler != null);
checkArgument(safeNameIdSupplier != null); checkArgument(safeNameIdSupplier != null);
checkArgument(reach != Reach.NONE);

this.compiler = compiler; this.compiler = compiler;


this.inlineGlobalFunctions = inlineGlobalFunctions; this.reach = reach;
this.inlineLocalFunctions = inlineLocalFunctions;
this.assumeMinimumCapture = assumeMinimumCapture; this.assumeMinimumCapture = assumeMinimumCapture;


this.maxSizeAfterInlining = maxSizeAfterInlining; this.maxSizeAfterInlining = maxSizeAfterInlining;
Expand Down Expand Up @@ -169,22 +168,13 @@ private boolean targetSizeAfterInlineExceedsLimit(NodeTraversal t, FunctionState
} }


/** Find functions that might be inlined. */ /** Find functions that might be inlined. */
private class FindCandidateFunctions implements Callback { private class FindCandidateFunctions extends AbstractPostOrderCallback {
private int callsSeen = 0; private int callsSeen = 0;


@Override
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
// Don't traverse into function bodies
// if we aren't inlining local functions.
return inlineLocalFunctions || nodeTraversal.inGlobalHoistScope();
}

@Override @Override
public void visit(NodeTraversal t, Node n, Node parent) { public void visit(NodeTraversal t, Node n, Node parent) {
if ((t.inGlobalHoistScope() && inlineGlobalFunctions) if (reach.includesGlobals() || !t.inGlobalHoistScope()) {
|| (!t.inGlobalHoistScope() && inlineLocalFunctions)) {
findNamedFunctions(t, n, parent); findNamedFunctions(t, n, parent);

findFunctionExpressions(t, n); findFunctionExpressions(t, n);
} }
} }
Expand Down
Expand Up @@ -402,13 +402,12 @@ public String getJavaInfo() {
INLINE_FUNCTIONS(ParamGroup.TYPE_CHECKING_OPTIMIZATION) { INLINE_FUNCTIONS(ParamGroup.TYPE_CHECKING_OPTIMIZATION) {
@Override @Override
public void apply(CompilerOptions options, boolean value) { public void apply(CompilerOptions options, boolean value) {
options.setInlineFunctions(value); options.setInlineFunctions(value ? Reach.ALL : Reach.NONE);
options.setInlineLocalFunctions(value);
} }


@Override @Override
public boolean isApplied(CompilerOptions options) { public boolean isApplied(CompilerOptions options) {
return options.inlineLocalFunctions && options.inlineFunctions; return options.getInlineFunctionsLevel() == Reach.ALL;
} }


@Override @Override
Expand Down
21 changes: 10 additions & 11 deletions test/com/google/javascript/jscomp/InlineFunctionsTest.java
Expand Up @@ -16,17 +16,17 @@


package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import com.google.javascript.jscomp.CompilerOptions.Reach;


/** /**
* Inline function tests. * Inline function tests.
* @author johnlenz@google.com (john lenz) * @author johnlenz@google.com (john lenz)
*/ */


public class InlineFunctionsTest extends CompilerTestCase { public class InlineFunctionsTest extends CompilerTestCase {
boolean allowGlobalFunctionInlining; Reach inliningReach;
final boolean allowExpressionDecomposition = true; final boolean allowExpressionDecomposition = true;
final boolean allowFunctionExpressionInlining = true; final boolean allowFunctionExpressionInlining = true;
final boolean allowLocalFunctionInlining = true;
boolean assumeStrictThis; boolean assumeStrictThis;
boolean assumeMinimumCapture; boolean assumeMinimumCapture;
int maxSizeAfterInlining; int maxSizeAfterInlining;
Expand All @@ -48,7 +48,7 @@ protected void setUp() throws Exception {
maybeEnableInferConsts(); maybeEnableInferConsts();
enableNormalize(); enableNormalize();
enableComputeSideEffects(); enableComputeSideEffects();
allowGlobalFunctionInlining = true; inliningReach = Reach.ALL;
assumeStrictThis = false; assumeStrictThis = false;
assumeMinimumCapture = false; assumeMinimumCapture = false;
maxSizeAfterInlining = CompilerOptions.UNLIMITED_FUN_SIZE_AFTER_INLINING; maxSizeAfterInlining = CompilerOptions.UNLIMITED_FUN_SIZE_AFTER_INLINING;
Expand All @@ -61,8 +61,7 @@ protected CompilerPass getProcessor(Compiler compiler) {
return new InlineFunctions( return new InlineFunctions(
compiler, compiler,
compiler.getUniqueNameIdSupplier(), compiler.getUniqueNameIdSupplier(),
allowGlobalFunctionInlining, inliningReach,
allowLocalFunctionInlining,
assumeStrictThis, assumeStrictThis,
assumeMinimumCapture, assumeMinimumCapture,
maxSizeAfterInlining); maxSizeAfterInlining);
Expand Down Expand Up @@ -1946,30 +1945,30 @@ public void testLocalFunctionInlining6() {
} }


public void testLocalFunctionInliningOnly1() { public void testLocalFunctionInliningOnly1() {
this.allowGlobalFunctionInlining = true; this.inliningReach = Reach.ALL;
test("function f(){} f()", "void 0;"); test("function f(){} f()", "void 0;");
this.allowGlobalFunctionInlining = false; this.inliningReach = Reach.LOCAL_ONLY;
testSame("function f(){} f()"); testSame("function f(){} f()");
} }


public void testLocalFunctionInliningOnly2() { public void testLocalFunctionInliningOnly2() {
this.allowGlobalFunctionInlining = false; this.inliningReach = Reach.LOCAL_ONLY;
testSame("function f(){} f()"); testSame("function f(){} f()");


test("function f(){ function g() {return 1} return g() }; f();", test("function f(){ function g() {return 1} return g() }; f();",
"function f(){ return 1 }; f();"); "function f(){ return 1 }; f();");
} }


public void testLocalFunctionInliningOnly3() { public void testLocalFunctionInliningOnly3() {
this.allowGlobalFunctionInlining = false; this.inliningReach = Reach.LOCAL_ONLY;
testSame("function f(){} f()"); testSame("function f(){} f()");


test("(function(){ function g() {return 1} return g() })();", test("(function(){ function g() {return 1} return g() })();",
"(function(){ return 1 })();"); "(function(){ return 1 })();");
} }


public void testLocalFunctionInliningOnly4() { public void testLocalFunctionInliningOnly4() {
this.allowGlobalFunctionInlining = false; this.inliningReach = Reach.LOCAL_ONLY;
testSame("function f(){} f()"); testSame("function f(){} f()");


test("(function(){ return (function() {return 1})() })();", test("(function(){ return (function() {return 1})() })();",
Expand Down Expand Up @@ -2195,7 +2194,7 @@ public void testInlineObject() {
disableCompareAsTree(); disableCompareAsTree();
enableMarkNoSideEffects(); enableMarkNoSideEffects();


allowGlobalFunctionInlining = false; this.inliningReach = Reach.LOCAL_ONLY;
assumeStrictThis = true; assumeStrictThis = true;
assumeMinimumCapture = true; assumeMinimumCapture = true;


Expand Down
7 changes: 5 additions & 2 deletions test/com/google/javascript/jscomp/MultiPassTest.java
Expand Up @@ -353,8 +353,11 @@ private void addInlineFunctions() {
@Override @Override
protected CompilerPass create(AbstractCompiler compiler) { protected CompilerPass create(AbstractCompiler compiler) {
return new InlineFunctions( return new InlineFunctions(
compiler, compiler.getUniqueNameIdSupplier(), compiler,
true, true, true, true, compiler.getUniqueNameIdSupplier(),
CompilerOptions.Reach.ALL,
true,
true,
CompilerOptions.UNLIMITED_FUN_SIZE_AFTER_INLINING); CompilerOptions.UNLIMITED_FUN_SIZE_AFTER_INLINING);
} }


Expand Down

0 comments on commit 3d9ecb4

Please sign in to comment.