| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.Maps; |
| 20 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 21 |
import com.google.javascript.rhino.IR; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
import com.google.javascript.rhino.Token; |
| 24 |
|
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.HashMap; |
| 27 |
import java.util.LinkedHashSet; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Map; |
| 30 |
import java.util.Map.Entry; |
| 31 |
import java.util.Set; |
| 32 |
import java.util.SortedMap; |
| 33 |
import java.util.logging.Logger; |
| 34 |
import java.util.regex.Matcher; |
| 35 |
import java.util.regex.Pattern; |
| 36 |
import java.util.zip.CRC32; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class AliasStrings extends AbstractPostOrderCallback |
| 46 |
implements CompilerPass { |
| 47 |
|
| 48 |
private static final Logger logger = |
| 49 |
Logger.getLogger(AliasStrings.class.getName()); |
| 50 |
|
| 51 |
|
| 52 |
private static final String STRING_ALIAS_PREFIX = "$$S_"; |
| 53 |
|
| 54 |
private final AbstractCompiler compiler; |
| 55 |
|
| 56 |
private final JSModuleGraph moduleGraph; |
| 57 |
|
| 58 |
|
| 59 |
private Matcher blacklist = null; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private final Set<String> aliasableStrings; |
| 66 |
|
| 67 |
private final boolean outputStringUsage; |
| 68 |
|
| 69 |
private final SortedMap<String, StringInfo> stringInfoMap = Maps.newTreeMap(); |
| 70 |
|
| 71 |
private final Set<String> usedHashedAliases = new LinkedHashSet<String>(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private final Map<JSModule, Node> moduleVarParentMap = |
| 78 |
new HashMap<JSModule, Node>(); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
long unitTestHashReductionMask = ~0L; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
AliasStrings(AbstractCompiler compiler, |
| 96 |
JSModuleGraph moduleGraph, |
| 97 |
Set<String> strings, |
| 98 |
String blacklistRegex, |
| 99 |
boolean outputStringUsage) { |
| 100 |
this.compiler = compiler; |
| 101 |
this.moduleGraph = moduleGraph; |
| 102 |
this.aliasableStrings = strings; |
| 103 |
if (blacklistRegex.length() != 0) { |
| 104 |
this.blacklist = Pattern.compile(blacklistRegex).matcher(""); |
| 105 |
} else { |
| 106 |
this.blacklist = null; |
| 107 |
} |
| 108 |
this.outputStringUsage = outputStringUsage; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void process(Node externs, Node root) { |
| 113 |
logger.fine("Aliasing common strings"); |
| 114 |
|
| 115 |
|
| 116 |
NodeTraversal.traverse(compiler, root, this); |
| 117 |
|
| 118 |
|
| 119 |
replaceStringsWithAliases(); |
| 120 |
|
| 121 |
|
| 122 |
addAliasDeclarationNodes(); |
| 123 |
|
| 124 |
if (outputStringUsage) { |
| 125 |
outputStringUsage(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 131 |
if (n.isString() && |
| 132 |
!parent.isGetProp() && |
| 133 |
!parent.isRegExp()) { |
| 134 |
|
| 135 |
String str = n.getString(); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if ("undefined".equals(str)) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if (blacklist != null && blacklist.reset(str).find()) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if (aliasableStrings == null || aliasableStrings.contains(str)) { |
| 149 |
StringOccurrence occurrence = new StringOccurrence(n, parent); |
| 150 |
StringInfo info = getOrCreateStringInfo(str); |
| 151 |
|
| 152 |
info.occurrences.add(occurrence); |
| 153 |
info.numOccurrences++; |
| 154 |
|
| 155 |
if (t.inGlobalScope() || isInThrowExpression(n)) { |
| 156 |
info.numOccurrencesInfrequentlyExecuted++; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
JSModule module = t.getModule(); |
| 161 |
if (info.numOccurrences != 1) { |
| 162 |
|
| 163 |
|
| 164 |
if (module != null && |
| 165 |
info.moduleToContainDecl != null && |
| 166 |
module != info.moduleToContainDecl && |
| 167 |
!moduleGraph.dependsOn(module, info.moduleToContainDecl)) { |
| 168 |
|
| 169 |
|
| 170 |
module = moduleGraph.getDeepestCommonDependency( |
| 171 |
module, info.moduleToContainDecl); |
| 172 |
} else { |
| 173 |
|
| 174 |
return; |
| 175 |
} |
| 176 |
} |
| 177 |
Node varParent = moduleVarParentMap.get(module); |
| 178 |
if (varParent == null) { |
| 179 |
varParent = compiler.getNodeForCodeInsertion(module); |
| 180 |
moduleVarParentMap.put(module, varParent); |
| 181 |
} |
| 182 |
info.moduleToContainDecl = module; |
| 183 |
info.parentForNewVarDecl = varParent; |
| 184 |
info.siblingToInsertVarDeclBefore = varParent.getFirstChild(); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private StringInfo getOrCreateStringInfo(String string) { |
| 194 |
StringInfo info = stringInfoMap.get(string); |
| 195 |
if (info == null) { |
| 196 |
info = new StringInfo(stringInfoMap.size()); |
| 197 |
stringInfoMap.put(string, info); |
| 198 |
} |
| 199 |
return info; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private static boolean isInThrowExpression(Node n) { |
| 206 |
|
| 207 |
for (Node ancestor : n.getAncestors()) { |
| 208 |
switch (ancestor.getType()) { |
| 209 |
case Token.THROW: |
| 210 |
return true; |
| 211 |
case Token.IF: |
| 212 |
case Token.WHILE: |
| 213 |
case Token.DO: |
| 214 |
case Token.FOR: |
| 215 |
case Token.SWITCH: |
| 216 |
case Token.CASE: |
| 217 |
case Token.DEFAULT_CASE: |
| 218 |
case Token.BLOCK: |
| 219 |
case Token.SCRIPT: |
| 220 |
case Token.FUNCTION: |
| 221 |
case Token.TRY: |
| 222 |
case Token.CATCH: |
| 223 |
case Token.RETURN: |
| 224 |
case Token.EXPR_RESULT: |
| 225 |
|
| 226 |
return false; |
| 227 |
} |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private void replaceStringsWithAliases() { |
| 236 |
for (Entry<String, StringInfo> entry : stringInfoMap.entrySet()) { |
| 237 |
String literal = entry.getKey(); |
| 238 |
StringInfo info = entry.getValue(); |
| 239 |
if (shouldReplaceWithAlias(literal, info)) { |
| 240 |
for (StringOccurrence occurrence : info.occurrences) { |
| 241 |
replaceStringWithAliasName( |
| 242 |
occurrence, info.getVariableName(literal), info); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
private void addAliasDeclarationNodes() { |
| 253 |
for (Entry<String, StringInfo> entry : stringInfoMap.entrySet()) { |
| 254 |
StringInfo info = entry.getValue(); |
| 255 |
if (!info.isAliased) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
String alias = info.getVariableName(entry.getKey()); |
| 259 |
Node var = IR.var(IR.name(alias), IR.string(entry.getKey())); |
| 260 |
if (info.siblingToInsertVarDeclBefore == null) { |
| 261 |
info.parentForNewVarDecl.addChildToFront(var); |
| 262 |
} else { |
| 263 |
info.parentForNewVarDecl.addChildBefore( |
| 264 |
var, info.siblingToInsertVarDeclBefore); |
| 265 |
} |
| 266 |
compiler.reportCodeChange(); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
private static boolean shouldReplaceWithAlias(String str, StringInfo info) { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if (info.numOccurrences > info.numOccurrencesInfrequentlyExecuted) { |
| 281 |
return true; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
int sizeOfLiteral = 2 + str.length(); |
| 296 |
int sizeOfStrings = info.numOccurrences * sizeOfLiteral; |
| 297 |
int sizeOfVariable = 3; |
| 298 |
|
| 299 |
int sizeOfAliases = 6 + sizeOfVariable + sizeOfLiteral |
| 300 |
+ info.numOccurrences * sizeOfVariable; |
| 301 |
|
| 302 |
return sizeOfAliases < sizeOfStrings; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
private void replaceStringWithAliasName(StringOccurrence occurrence, |
| 309 |
String name, |
| 310 |
StringInfo info) { |
| 311 |
occurrence.parent.replaceChild(occurrence.node, |
| 312 |
IR.name(name)); |
| 313 |
info.isAliased = true; |
| 314 |
compiler.reportCodeChange(); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
private void outputStringUsage() { |
| 321 |
StringBuilder sb = new StringBuilder("Strings used more than once:\n"); |
| 322 |
for (Entry<String, StringInfo> stringInfoEntry : stringInfoMap.entrySet()) { |
| 323 |
StringInfo info = stringInfoEntry.getValue(); |
| 324 |
if (info.numOccurrences > 1) { |
| 325 |
sb.append(info.numOccurrences); |
| 326 |
sb.append(": "); |
| 327 |
sb.append(stringInfoEntry.getKey()); |
| 328 |
sb.append('\n'); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
logger.fine(sb.toString()); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private static final class StringOccurrence { |
| 341 |
final Node node; |
| 342 |
final Node parent; |
| 343 |
|
| 344 |
StringOccurrence(Node node, Node parent) { |
| 345 |
this.node = node; |
| 346 |
this.parent = parent; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
private final class StringInfo { |
| 355 |
final int id; |
| 356 |
|
| 357 |
boolean isAliased; |
| 358 |
|
| 359 |
final List<StringOccurrence> occurrences; |
| 360 |
int numOccurrences; |
| 361 |
int numOccurrencesInfrequentlyExecuted; |
| 362 |
|
| 363 |
JSModule moduleToContainDecl; |
| 364 |
Node parentForNewVarDecl; |
| 365 |
Node siblingToInsertVarDeclBefore; |
| 366 |
|
| 367 |
String aliasName; |
| 368 |
|
| 369 |
StringInfo(int id) { |
| 370 |
this.id = id; |
| 371 |
this.occurrences = new ArrayList<StringOccurrence>(); |
| 372 |
this.isAliased = false; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
String getVariableName(String stringLiteral) { |
| 377 |
if (aliasName == null) { |
| 378 |
aliasName = |
| 379 |
encodeStringAsIdentifier(STRING_ALIAS_PREFIX, stringLiteral); |
| 380 |
} |
| 381 |
return aliasName; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
String encodeStringAsIdentifier(String prefix, String s) { |
| 404 |
|
| 405 |
final int MAX_LIMIT = 20; |
| 406 |
final int length = s.length(); |
| 407 |
final int limit = Math.min(length, MAX_LIMIT); |
| 408 |
|
| 409 |
StringBuilder sb = new StringBuilder(); |
| 410 |
sb.append(prefix); |
| 411 |
boolean protectHex = false; |
| 412 |
|
| 413 |
for (int i = 0; i < limit; i++) { |
| 414 |
char ch = s.charAt(i); |
| 415 |
|
| 416 |
if (protectHex) { |
| 417 |
if ((ch >= '0' && ch <= '9') || |
| 418 |
(ch >= 'a' && ch <= 'f')) { |
| 419 |
sb.append('_'); |
| 420 |
} |
| 421 |
protectHex = false; |
| 422 |
} |
| 423 |
|
| 424 |
if ((ch >= '0' && ch <= '9') || |
| 425 |
(ch >= 'A' && ch <= 'Z') || |
| 426 |
(ch >= 'a' && ch <= 'z')) { |
| 427 |
sb.append(ch); |
| 428 |
} else { |
| 429 |
sb.append('$'); |
| 430 |
sb.append(Integer.toHexString(ch)); |
| 431 |
protectHex = true; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if (length == limit) { |
| 436 |
return sb.toString(); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
CRC32 crc32 = new CRC32(); |
| 442 |
crc32.update(s.getBytes()); |
| 443 |
long hash = crc32.getValue() & unitTestHashReductionMask; |
| 444 |
sb.append('_'); |
| 445 |
sb.append(Long.toHexString(hash)); |
| 446 |
String encoded = sb.toString(); |
| 447 |
if (!usedHashedAliases.add(encoded)) { |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
encoded += "_" + id; |
| 452 |
} |
| 453 |
return encoded; |
| 454 |
} |
| 455 |
} |
| 456 |
} |