@GwtIncompatible(value="java.util.regex") class AliasStrings extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass
Compiler
pass for aliasing strings. String declarations
contribute to garbage collection, which becomes a problem in large
applications. Strings that should be aliased occur many times in the code,
or occur on codepaths that get executed frequently.Modifier and Type | Class and Description |
---|---|
private class |
AliasStrings.StringInfo
A class that holds information about a JavaScript string that might become
aliased.
|
private static class |
AliasStrings.StringOccurrence
A class that holds the location of a single JavaScript string literal
|
Modifier and Type | Field and Description |
---|---|
private java.util.Set<java.lang.String> |
aliasableStrings
Strings that can be aliased, or null if all strings except 'undefined'
should be aliased
|
private Matcher |
blacklist |
private AbstractCompiler |
compiler |
private static java.util.logging.Logger |
logger |
private JSModuleGraph |
moduleGraph |
private java.util.Map<JSModule,Node> |
moduleVarParentMap
Map from module to the node in that module that should parent any string
variable declarations that have to be moved into that module
|
private boolean |
outputStringUsage |
private static java.lang.String |
STRING_ALIAS_PREFIX
Prefix for variable names for the aliased strings
|
private java.util.SortedMap<java.lang.String,AliasStrings.StringInfo> |
stringInfoMap |
(package private) long |
unitTestHashReductionMask
package private.
|
private java.util.Set<java.lang.String> |
usedHashedAliases |
Constructor and Description |
---|
AliasStrings(AbstractCompiler compiler,
JSModuleGraph moduleGraph,
java.util.Set<java.lang.String> strings,
java.lang.String blacklistRegex,
boolean outputStringUsage)
Creates an instance.
|
Modifier and Type | Method and Description |
---|---|
private void |
addAliasDeclarationNodes()
Creates a var declaration for each aliased string.
|
private AliasStrings.StringInfo |
getOrCreateStringInfo(java.lang.String string)
Looks up the
AliasStrings.StringInfo object for a JavaScript string. |
private static boolean |
isInThrowExpression(Node n)
Is the
Node currently within a 'throw' expression? |
private void |
outputStringUsage()
Outputs a log of all strings used more than once in the code.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
replaceStringsWithAliases()
Replace strings with references to alias variables.
|
private void |
replaceStringWithAliasName(AliasStrings.StringOccurrence occurrence,
java.lang.String name,
AliasStrings.StringInfo info)
Replaces a string literal with a reference to the string's alias variable.
|
private static boolean |
shouldReplaceWithAlias(java.lang.String str,
AliasStrings.StringInfo info)
Dictates the policy for replacing a string with an alias.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
private static final java.util.logging.Logger logger
private static final java.lang.String STRING_ALIAS_PREFIX
private final AbstractCompiler compiler
private final JSModuleGraph moduleGraph
private Matcher blacklist
private final java.util.Set<java.lang.String> aliasableStrings
private final boolean outputStringUsage
private final java.util.SortedMap<java.lang.String,AliasStrings.StringInfo> stringInfoMap
private final java.util.Set<java.lang.String> usedHashedAliases
private final java.util.Map<JSModule,Node> moduleVarParentMap
long unitTestHashReductionMask
AliasStrings(AbstractCompiler compiler, JSModuleGraph moduleGraph, java.util.Set<java.lang.String> strings, java.lang.String blacklistRegex, boolean outputStringUsage)
compiler
- The compilermoduleGraph
- The module graph, or null if there are no modulesstrings
- Set of strings to be aliased. If null, all strings except
'undefined' will be aliased.blacklistRegex
- The regex to blacklist words in aliasing strings.outputStringUsage
- Outputs all strings and the number of times they
were used in the application to the server log.public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private AliasStrings.StringInfo getOrCreateStringInfo(java.lang.String string)
AliasStrings.StringInfo
object for a JavaScript string. Creates
it if necessary.private static boolean isInThrowExpression(Node n)
Node
currently within a 'throw' expression?private void replaceStringsWithAliases()
private void addAliasDeclarationNodes()
private static boolean shouldReplaceWithAlias(java.lang.String str, AliasStrings.StringInfo info)
str
- The string literalinfo
- Accumulated information about a stringprivate void replaceStringWithAliasName(AliasStrings.StringOccurrence occurrence, java.lang.String name, AliasStrings.StringInfo info)
private void outputStringUsage()