| 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.HashMultimap; |
| 20 |
import com.google.common.collect.ImmutableList; |
| 21 |
import com.google.common.collect.Multimap; |
| 22 |
import com.google.javascript.jscomp.NodeTraversal.Callback; |
| 23 |
import com.google.javascript.rhino.IR; |
| 24 |
import com.google.javascript.rhino.Node; |
| 25 |
import com.google.javascript.rhino.Token; |
| 26 |
|
| 27 |
import java.util.Collection; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class FunctionRewriter implements CompilerPass { |
| 48 |
private final AbstractCompiler compiler; |
| 49 |
|
| 50 |
|
| 51 |
private static final int SAVINGS_THRESHOLD = 16; |
| 52 |
|
| 53 |
FunctionRewriter(AbstractCompiler compiler) { |
| 54 |
this.compiler = compiler; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void process(Node externs, Node root) { |
| 59 |
List<Reducer> reducers = ImmutableList.of(new ReturnConstantReducer(), |
| 60 |
new GetterReducer(), |
| 61 |
new SetterReducer(), |
| 62 |
new EmptyFunctionReducer(), |
| 63 |
new IdentityReducer()); |
| 64 |
|
| 65 |
Multimap<Reducer, Reduction> reductionMap = HashMultimap.create(); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
NodeTraversal.traverse(compiler, root, |
| 70 |
new ReductionGatherer(reducers, reductionMap)); |
| 71 |
|
| 72 |
|
| 73 |
for (Reducer reducer : reducers) { |
| 74 |
Collection<Reduction> reductions = reductionMap.get(reducer); |
| 75 |
if (reductions.isEmpty()) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
Node helperCode = parseHelperCode(reducer); |
| 80 |
if (helperCode == null) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
int helperCodeCost = InlineCostEstimator.getCost(helperCode); |
| 85 |
|
| 86 |
|
| 87 |
int savings = 0; |
| 88 |
for (Reduction reduction : reductions) { |
| 89 |
savings += reduction.estimateSavings(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (savings > (helperCodeCost + SAVINGS_THRESHOLD)) { |
| 95 |
for (Reduction reduction : reductions) { |
| 96 |
reduction.apply(); |
| 97 |
} |
| 98 |
|
| 99 |
Node addingRoot = compiler.getNodeForCodeInsertion(null); |
| 100 |
addingRoot.addChildrenToFront(helperCode); |
| 101 |
compiler.reportCodeChange(); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public Node parseHelperCode(Reducer reducer) { |
| 112 |
Node root = compiler.parseSyntheticCode( |
| 113 |
reducer.getClass().toString() + ":helper", reducer.getHelperSource()); |
| 114 |
return (root != null) ? root.removeFirstChild() : null; |
| 115 |
} |
| 116 |
|
| 117 |
private static boolean isReduceableFunctionExpression(Node n) { |
| 118 |
return NodeUtil.isFunctionExpression(n) |
| 119 |
&& !NodeUtil.isGetOrSetKey(n.getParent()); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private class Reduction { |
| 126 |
private final Node parent; |
| 127 |
private final Node oldChild; |
| 128 |
private final Node newChild; |
| 129 |
|
| 130 |
Reduction(Node parent, Node oldChild, Node newChild) { |
| 131 |
this.parent = parent; |
| 132 |
this.oldChild = oldChild; |
| 133 |
this.newChild = newChild; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
void apply() { |
| 140 |
parent.replaceChild(oldChild, newChild); |
| 141 |
compiler.reportCodeChange(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
int estimateSavings() { |
| 148 |
return InlineCostEstimator.getCost(oldChild) - |
| 149 |
InlineCostEstimator.getCost(newChild); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private class ReductionGatherer implements Callback { |
| 159 |
private final List<Reducer> reducers; |
| 160 |
private final Multimap<Reducer, Reduction> reductions; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
ReductionGatherer(List<Reducer> reducers, |
| 168 |
Multimap<Reducer, Reduction> reductions) { |
| 169 |
this.reducers = reducers; |
| 170 |
this.reductions = reductions; |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public boolean shouldTraverse(NodeTraversal raversal, |
| 175 |
Node node, |
| 176 |
Node parent) { |
| 177 |
for (Reducer reducer : reducers) { |
| 178 |
Node replacement = reducer.reduce(node); |
| 179 |
if (replacement != node) { |
| 180 |
reductions.put(reducer, new Reduction(parent, node, replacement)); |
| 181 |
return false; |
| 182 |
} |
| 183 |
} |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
@Override |
| 189 |
public void visit(NodeTraversal traversal, Node node, Node parent) { |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
abstract static class Reducer { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
abstract String getHelperSource(); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
abstract Node reduce(Node node); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
protected final Node buildCallNode(String methodName, Node argumentNode, |
| 216 |
Node srcref) { |
| 217 |
Node call = IR.call(IR.name(methodName)).srcref(srcref); |
| 218 |
call.putBooleanProp(Node.FREE_CALL, true); |
| 219 |
if (argumentNode != null) { |
| 220 |
call.addChildToBack(argumentNode.cloneTree()); |
| 221 |
} |
| 222 |
return call; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private static class EmptyFunctionReducer extends Reducer { |
| 236 |
static final String FACTORY_METHOD_NAME = "JSCompiler_emptyFn"; |
| 237 |
static final String HELPER_SOURCE = |
| 238 |
"function " + FACTORY_METHOD_NAME + "() {" + |
| 239 |
" return function() {}" + |
| 240 |
"}"; |
| 241 |
|
| 242 |
@Override |
| 243 |
public String getHelperSource() { |
| 244 |
return HELPER_SOURCE; |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public Node reduce(Node node) { |
| 249 |
if (NodeUtil.isEmptyFunctionExpression(node)) { |
| 250 |
return buildCallNode(FACTORY_METHOD_NAME, null, node); |
| 251 |
} else { |
| 252 |
return node; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
abstract static class SingleReturnStatementReducer extends Reducer { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
protected final Node maybeGetSingleReturnRValue(Node functionNode) { |
| 268 |
Node body = functionNode.getLastChild(); |
| 269 |
if (!body.hasOneChild()) { |
| 270 |
return null; |
| 271 |
} |
| 272 |
|
| 273 |
Node statement = body.getFirstChild(); |
| 274 |
if (statement.isReturn()) { |
| 275 |
return statement.getFirstChild(); |
| 276 |
} |
| 277 |
return null; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private static class IdentityReducer extends SingleReturnStatementReducer { |
| 291 |
static final String FACTORY_METHOD_NAME = "JSCompiler_identityFn"; |
| 292 |
static final String HELPER_SOURCE = |
| 293 |
"function " + FACTORY_METHOD_NAME + "() {" + |
| 294 |
" return function(" + FACTORY_METHOD_NAME + "_value) {" + |
| 295 |
"return " + FACTORY_METHOD_NAME + "_value}" + |
| 296 |
"}"; |
| 297 |
|
| 298 |
@Override |
| 299 |
public String getHelperSource() { |
| 300 |
return HELPER_SOURCE; |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public Node reduce(Node node) { |
| 305 |
if (!isReduceableFunctionExpression(node)) { |
| 306 |
return node; |
| 307 |
} |
| 308 |
|
| 309 |
if (isIdentityFunction(node)) { |
| 310 |
return buildCallNode(FACTORY_METHOD_NAME, null, node); |
| 311 |
} else { |
| 312 |
return node; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private boolean isIdentityFunction(Node functionNode) { |
| 323 |
Node argList = functionNode.getFirstChild().getNext(); |
| 324 |
Node paramNode = argList.getFirstChild(); |
| 325 |
if (paramNode == null) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
Node value = maybeGetSingleReturnRValue(functionNode); |
| 330 |
if (value != null && |
| 331 |
value.isName() && |
| 332 |
value.getString().equals(paramNode.getString())) { |
| 333 |
return true; |
| 334 |
} |
| 335 |
return false; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
private static class ReturnConstantReducer |
| 349 |
extends SingleReturnStatementReducer { |
| 350 |
static final String FACTORY_METHOD_NAME = "JSCompiler_returnArg"; |
| 351 |
static final String HELPER_SOURCE = |
| 352 |
"function " + FACTORY_METHOD_NAME + |
| 353 |
"(" + FACTORY_METHOD_NAME + "_value) {" + |
| 354 |
" return function() {return " + FACTORY_METHOD_NAME + "_value}" + |
| 355 |
"}"; |
| 356 |
|
| 357 |
@Override |
| 358 |
public String getHelperSource() { |
| 359 |
return HELPER_SOURCE; |
| 360 |
} |
| 361 |
|
| 362 |
@Override |
| 363 |
public Node reduce(Node node) { |
| 364 |
if (!isReduceableFunctionExpression(node)) { |
| 365 |
return node; |
| 366 |
} |
| 367 |
|
| 368 |
Node valueNode = getValueNode(node); |
| 369 |
if (valueNode != null) { |
| 370 |
return buildCallNode(FACTORY_METHOD_NAME, valueNode, node); |
| 371 |
} else { |
| 372 |
return node; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
private Node getValueNode(Node functionNode) { |
| 384 |
Node value = maybeGetSingleReturnRValue(functionNode); |
| 385 |
if (value != null && |
| 386 |
NodeUtil.isImmutableValue(value)) { |
| 387 |
return value; |
| 388 |
} |
| 389 |
return null; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
private static class GetterReducer extends SingleReturnStatementReducer { |
| 403 |
static final String FACTORY_METHOD_NAME = "JSCompiler_get"; |
| 404 |
static final String HELPER_SOURCE = |
| 405 |
"function " + FACTORY_METHOD_NAME + "(" + |
| 406 |
FACTORY_METHOD_NAME + "_name) {" + |
| 407 |
" return function() {return this[" + FACTORY_METHOD_NAME + "_name]}" + |
| 408 |
"}"; |
| 409 |
|
| 410 |
@Override |
| 411 |
public String getHelperSource() { |
| 412 |
return HELPER_SOURCE; |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public Node reduce(Node node) { |
| 417 |
if (!isReduceableFunctionExpression(node)) { |
| 418 |
return node; |
| 419 |
} |
| 420 |
|
| 421 |
Node propName = getGetPropertyName(node); |
| 422 |
if (propName != null) { |
| 423 |
if (!propName.isString()) { |
| 424 |
throw new IllegalStateException( |
| 425 |
"Expected STRING, got " + Token.name(propName.getType())); |
| 426 |
} |
| 427 |
|
| 428 |
return buildCallNode(FACTORY_METHOD_NAME, propName, node); |
| 429 |
} else { |
| 430 |
return node; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
private Node getGetPropertyName(Node functionNode) { |
| 442 |
Node value = maybeGetSingleReturnRValue(functionNode); |
| 443 |
if (value != null && |
| 444 |
value.isGetProp() && |
| 445 |
value.getFirstChild().isThis()) { |
| 446 |
return value.getLastChild(); |
| 447 |
} |
| 448 |
return null; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
private static class SetterReducer extends Reducer { |
| 462 |
static final String FACTORY_METHOD_NAME = "JSCompiler_set"; |
| 463 |
static final String HELPER_SOURCE = |
| 464 |
"function " + FACTORY_METHOD_NAME + "(" + |
| 465 |
FACTORY_METHOD_NAME + "_name) {" + |
| 466 |
" return function(" + FACTORY_METHOD_NAME + "_value) {" + |
| 467 |
"this[" + FACTORY_METHOD_NAME + "_name] = " + |
| 468 |
FACTORY_METHOD_NAME + "_value}" + |
| 469 |
"}"; |
| 470 |
|
| 471 |
@Override |
| 472 |
public String getHelperSource() { |
| 473 |
return HELPER_SOURCE; |
| 474 |
} |
| 475 |
|
| 476 |
@Override |
| 477 |
public Node reduce(Node node) { |
| 478 |
if (!isReduceableFunctionExpression(node)) { |
| 479 |
return node; |
| 480 |
} |
| 481 |
|
| 482 |
Node propName = getSetPropertyName(node); |
| 483 |
if (propName != null) { |
| 484 |
if (!propName.isString()) { |
| 485 |
throw new IllegalStateException( |
| 486 |
"Expected STRING, got " + Token.name(propName.getType())); |
| 487 |
} |
| 488 |
|
| 489 |
return buildCallNode(FACTORY_METHOD_NAME, propName, node); |
| 490 |
} else { |
| 491 |
return node; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
private Node getSetPropertyName(Node functionNode) { |
| 503 |
Node body = functionNode.getLastChild(); |
| 504 |
if (!body.hasOneChild()) { |
| 505 |
return null; |
| 506 |
} |
| 507 |
|
| 508 |
Node argList = functionNode.getFirstChild().getNext(); |
| 509 |
Node paramNode = argList.getFirstChild(); |
| 510 |
if (paramNode == null) { |
| 511 |
return null; |
| 512 |
} |
| 513 |
|
| 514 |
Node statement = body.getFirstChild(); |
| 515 |
if (!NodeUtil.isExprAssign(statement)) { |
| 516 |
return null; |
| 517 |
} |
| 518 |
|
| 519 |
Node assign = statement.getFirstChild(); |
| 520 |
Node lhs = assign.getFirstChild(); |
| 521 |
if (lhs.isGetProp() && lhs.getFirstChild().isThis()) { |
| 522 |
Node rhs = assign.getLastChild(); |
| 523 |
if (rhs.isName() && |
| 524 |
rhs.getString().equals(paramNode.getString())) { |
| 525 |
Node propertyName = lhs.getLastChild(); |
| 526 |
return propertyName; |
| 527 |
} |
| 528 |
} |
| 529 |
return null; |
| 530 |
} |
| 531 |
} |
| 532 |
} |