Skip to content

Commit

Permalink
Make state for all the type-based optimizations private to CompilerOp…
Browse files Browse the repository at this point in the history
…tions.

This paves the way for making the useTypeBasedOptimizations option
turn them all on.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127198720
  • Loading branch information
blickly committed Jul 12, 2016
1 parent b669fdd commit 3069daa
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/com/google/javascript/jscomp/CompilationLevel.java
Expand Up @@ -183,13 +183,13 @@ public void setTypeBasedOptimizationOptions(CompilerOptions options) {
switch (this) {
case ADVANCED_OPTIMIZATIONS:
options.inferTypes = true;
options.disambiguateProperties = true;
options.ambiguateProperties = true;
options.inlineProperties = true;
options.useTypesForOptimization = true;
options.setDisambiguateProperties(true);
options.setAmbiguateProperties(true);
options.setInlineProperties(true);
options.setUseTypesForOptimization(true);
break;
case SIMPLE_OPTIMIZATIONS:
options.useTypesForOptimization = true;
options.setUseTypesForOptimization(true);
break;
case WHITESPACE_ONLY:
break;
Expand Down
16 changes: 8 additions & 8 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -316,15 +316,15 @@ public void initOptions(CompilerOptions options) {

// Turn off type-based optimizations when type checking is off
if (!options.checkTypes) {
options.disambiguateProperties = false;
options.ambiguateProperties = false;
options.inlineProperties = false;
options.useTypesForOptimization = false;
options.setDisambiguateProperties(false);
options.setAmbiguateProperties(false);
options.setInlineProperties(false);
options.setUseTypesForOptimization(false);
}

if (options.legacyCodeCompile) {
options.disambiguateProperties = false;
options.ambiguateProperties = false;
options.setDisambiguateProperties(false);
options.setAmbiguateProperties(false);
options.useNonStrictWarningsGuard();
}

Expand Down Expand Up @@ -1043,8 +1043,8 @@ public String get() {

@Override
boolean areNodesEqualForInlining(Node n1, Node n2) {
if (options.ambiguateProperties ||
options.disambiguateProperties) {
if (options.shouldAmbiguateProperties() ||
options.shouldDisambiguateProperties()) {
// The type based optimizations require that type information is preserved
// during other optimizations.
return n1.isEquivalentToTyped(n2);
Expand Down
18 changes: 15 additions & 3 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -345,7 +345,7 @@ public void setLegacyCodeCompile(boolean legacy) {
boolean assumeClosuresOnlyCaptureReferences;

/** Inlines properties */
boolean inlineProperties;
private boolean inlineProperties;

/** Move code to a deeper module */
public boolean crossModuleCodeMotion;
Expand Down Expand Up @@ -575,10 +575,10 @@ public void setCollapseObjectLiterals(boolean enabled) {
* Rename properties to disambiguate between unrelated fields based on
* type information.
*/
public boolean disambiguateProperties;
private boolean disambiguateProperties;

/** Rename unrelated properties to the same name to reduce code size. */
public boolean ambiguateProperties;
private boolean ambiguateProperties;

/** Input sourcemap files, indexed by the JS files they refer to */
ImmutableMap<String, SourceMapInput> inputSourceMaps;
Expand Down Expand Up @@ -1485,6 +1485,10 @@ public void setInlineProperties(boolean enable) {
inlineProperties = enable;
}

boolean shouldInlineProperties() {
return inlineProperties;
}

/**
* Set the variable removal policy for the compiler.
*/
Expand Down Expand Up @@ -2213,10 +2217,18 @@ public void setDisambiguateProperties(boolean disambiguateProperties) {
this.disambiguateProperties = disambiguateProperties;
}

boolean shouldDisambiguateProperties() {
return this.disambiguateProperties;
}

public void setAmbiguateProperties(boolean ambiguateProperties) {
this.ambiguateProperties = ambiguateProperties;
}

boolean shouldAmbiguateProperties() {
return this.ambiguateProperties;
}

public void setAnonymousFunctionNaming(
AnonymousFunctionNamingPolicy anonymousFunctionNaming) {
this.anonymousFunctionNaming = anonymousFunctionNaming;
Expand Down
3 changes: 2 additions & 1 deletion src/com/google/javascript/jscomp/DartSuperAccessorsPass.java
Expand Up @@ -56,7 +56,8 @@ public DartSuperAccessorsPass(AbstractCompiler compiler) {
// We currently rely on JSCompiler_renameProperty, which is not type-aware.
// We would need something like goog.reflect.object (with the super class type),
// but right now this would yield much larger code.
Preconditions.checkState(!options.ambiguateProperties && !options.disambiguateProperties,
Preconditions.checkState(
!options.shouldAmbiguateProperties() && !options.shouldDisambiguateProperties(),
"Dart super accessors pass is not compatible with property (dis)ambiguation yet");
}

Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -579,7 +579,7 @@ protected List<PassFactory> getOptimizations() {
// soon after type checking, both so that it can make use of type
// information and so that other passes can take advantage of the renamed
// properties.
if (options.disambiguateProperties) {
if (options.shouldDisambiguateProperties()) {
passes.add(disambiguateProperties);
}

Expand Down Expand Up @@ -731,7 +731,7 @@ protected List<PassFactory> getOptimizations() {
passes.add(extractPrototypeMemberDeclarations);
}

if (options.ambiguateProperties &&
if (options.shouldAmbiguateProperties() &&
(options.propertyRenaming == PropertyRenamingPolicy.ALL_UNQUOTED)) {
passes.add(ambiguateProperties);
}
Expand Down Expand Up @@ -862,7 +862,7 @@ private List<PassFactory> getMainOptimizationLoop() {
passes.add(inlineFunctions);
}

if (options.inlineProperties) {
if (options.shouldInlineProperties()) {
passes.add(inlineProperties);
}

Expand Down
8 changes: 4 additions & 4 deletions test/com/google/javascript/jscomp/IntegrationTest.java
Expand Up @@ -3240,7 +3240,7 @@ public void testInlineProperties() {
"/** @constructor */\n" +
"ns.C = function () {this.someProperty = 1}\n" +
"alert(new ns.C().someProperty + new ns.C().someProperty);\n";
assertTrue(options.inlineProperties);
assertTrue(options.shouldInlineProperties());
assertTrue(options.collapseProperties);
// CollapseProperties used to prevent inlining this property.
test(options, code, "alert(2);");
Expand All @@ -3260,7 +3260,7 @@ public void testGoogDefineClass1() {
" constructor: function () {this.someProperty = 1}\n" +
"});\n" +
"alert(new ns.C().someProperty + new ns.C().someProperty);\n";
assertTrue(options.inlineProperties);
assertTrue(options.shouldInlineProperties());
assertTrue(options.collapseProperties);
// CollapseProperties used to prevent inlining this property.
test(options, code, "alert(2);");
Expand All @@ -3279,7 +3279,7 @@ public void testGoogDefineClass2() {
" constructor: function () {this.someProperty = 1}\n" +
"});\n" +
"alert(new C().someProperty + new C().someProperty);\n";
assertTrue(options.inlineProperties);
assertTrue(options.shouldInlineProperties());
assertTrue(options.collapseProperties);
// CollapseProperties used to prevent inlining this property.
test(options, code, "alert(2);");
Expand All @@ -3304,7 +3304,7 @@ public void testGoogDefineClass3() {
"});" +
"var x = new C();\n" +
"x.someMethod(x.someProperty);\n";
assertTrue(options.inlineProperties);
assertTrue(options.shouldInlineProperties());
assertTrue(options.collapseProperties);
// CollapseProperties used to prevent inlining this property.
test(options, code, TypeValidator.TYPE_MISMATCH_WARNING);
Expand Down

0 comments on commit 3069daa

Please sign in to comment.