Skip to content

Commit 06c368b

Browse files
concavelenzcopybara-github
authored andcommitted
Add a "alias_string_mode" ALL_AGGRESSIVE.
PiperOrigin-RevId: 701985985
1 parent c238ce8 commit 06c368b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/com/google/javascript/jscomp/AliasStrings.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.javascript.jscomp;
1818

19+
import static com.google.common.base.Preconditions.checkState;
1920
import static java.lang.Math.min;
2021

2122
import com.google.javascript.jscomp.CompilerOptions.AliasStringsMode;
@@ -92,6 +93,7 @@ class AliasStrings implements CompilerPass, NodeTraversal.Callback {
9293
this.compiler = compiler;
9394
this.chunkGraph = chunkGraph;
9495
this.outputStringUsage = outputStringUsage;
96+
checkState(aliasStringsMode != AliasStringsMode.NONE);
9597
this.aliasStringsMode = aliasStringsMode;
9698
}
9799

@@ -226,7 +228,12 @@ private void addAliasDeclarationNodes() {
226228
* @param str The string literal
227229
* @param info Accumulated information about a string
228230
*/
229-
private static boolean shouldReplaceWithAlias(String str, StringInfo info) {
231+
private boolean shouldReplaceWithAlias(String str, StringInfo info) {
232+
// Always alias strings if the mode is ALL_AGGRESSIVE.
233+
if (aliasStringsMode == AliasStringsMode.ALL_AGGRESSIVE) {
234+
return true;
235+
}
236+
230237
// Optimize for code size. Are aliases smaller than strings?
231238
//
232239
// This logic optimizes for the size of uncompressed code, but it tends to

src/com/google/javascript/jscomp/CompilerOptions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3183,7 +3183,8 @@ public static enum IsolationMode {
31833183
public static enum AliasStringsMode {
31843184
NONE, // Do not alias string literals.
31853185
LARGE, // Alias all string literals with a length greater than 100 characters.
3186-
ALL // Alias all string literals.
3186+
ALL, // Alias all string literals where it may improve code size
3187+
ALL_AGGRESSIVE // Alias all string regardless of code size
31873188
}
31883189

31893190
/**

test/com/google/javascript/jscomp/AliasStringsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.javascript.jscomp.CompilerOptions.AliasStringsMode;
2020
import com.google.javascript.jscomp.testing.JSChunkGraphBuilder;
21+
import org.junit.Before;
2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
2324
import org.junit.runners.JUnit4;
@@ -46,6 +47,13 @@ protected CompilerPass getProcessor(Compiler compiler) {
4647
return pass;
4748
}
4849

50+
@Override
51+
@Before
52+
public void setUp() throws Exception {
53+
super.setUp();
54+
aliasStringsMode = AliasStringsMode.ALL;
55+
}
56+
4957
@Test
5058
public void testTemplateLiteral() {
5159
// TODO(bradfordcsmith): Maybe implement using aliases in template literals?
@@ -61,6 +69,18 @@ public void testTemplateLiteral() {
6169
"const AB = `${A}aliasable string${B}`"));
6270
}
6371

72+
@Test
73+
public void testAliasAggressively() {
74+
testSame(lines("function f() { return 'aliasable string'; }"));
75+
76+
aliasStringsMode = AliasStringsMode.ALL_AGGRESSIVE;
77+
test(
78+
lines("function f() { return 'aliasable string'; }"),
79+
lines(
80+
"var $$S_aliasable$20string = 'aliasable string';",
81+
"function f() { return $$S_aliasable$20string; }"));
82+
}
83+
6484
@Test
6585
public void testProtectedMessage() {
6686
test(

0 commit comments

Comments
 (0)