| 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.javascript.rhino.Node; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class InlineCostEstimator { |
| 28 |
|
| 29 |
private static final String ESTIMATED_IDENTIFIER = "ab"; |
| 30 |
static final int ESTIMATED_IDENTIFIER_COST = ESTIMATED_IDENTIFIER.length(); |
| 31 |
|
| 32 |
private InlineCostEstimator() { |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static int getCost(Node root) { |
| 39 |
return getCost(root, Integer.MAX_VALUE); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static int getCost(Node root, int costThreshhold) { |
| 46 |
CompiledSizeEstimator estimator = new CompiledSizeEstimator(costThreshhold); |
| 47 |
estimator.add(root); |
| 48 |
return estimator.getCost(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private static class CompiledSizeEstimator extends CodeConsumer { |
| 56 |
private int maxCost; |
| 57 |
private int cost = 0; |
| 58 |
private char last = '\0'; |
| 59 |
private boolean continueProcessing = true; |
| 60 |
|
| 61 |
CompiledSizeEstimator(int costThreshhold) { |
| 62 |
this.maxCost = costThreshhold; |
| 63 |
} |
| 64 |
|
| 65 |
void add(Node root) { |
| 66 |
CodeGenerator cg = CodeGenerator.forCostEstimation(this); |
| 67 |
cg.add(root); |
| 68 |
} |
| 69 |
|
| 70 |
int getCost() { |
| 71 |
return cost; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
boolean continueProcessing() { |
| 76 |
return continueProcessing; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
char getLastChar() { |
| 81 |
return last; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
void append(String str){ |
| 86 |
last = str.charAt(str.length() - 1); |
| 87 |
cost += str.length(); |
| 88 |
if (maxCost <= cost) { |
| 89 |
continueProcessing = false; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
void addIdentifier(String identifier) { |
| 95 |
add(ESTIMATED_IDENTIFIER); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@Override |
| 103 |
void addConstant(String newcode) { |
| 104 |
add("0"); |
| 105 |
} |
| 106 |
} |
| 107 |
} |