JS inline: inline simple-return functions directly
This commit is contained in:
committed by
Zalim Bashorov
parent
da817e7ed2
commit
cbb9e9c40e
@@ -37,7 +37,7 @@ class FunctionInlineMutator {
|
|||||||
private final RenamingContext<JsBlock> renamingContext;
|
private final RenamingContext<JsBlock> renamingContext;
|
||||||
private final InsertionPoint<JsStatement> insertionPoint;
|
private final InsertionPoint<JsStatement> insertionPoint;
|
||||||
private JsBlock body;
|
private JsBlock body;
|
||||||
private JsNameRef resultExpr = null;
|
private JsExpression resultExpr = null;
|
||||||
private JsLabel breakLabel = null;
|
private JsLabel breakLabel = null;
|
||||||
|
|
||||||
public static InlineableResult getInlineableCallReplacement(
|
public static InlineableResult getInlineableCallReplacement(
|
||||||
@@ -80,9 +80,15 @@ class FunctionInlineMutator {
|
|||||||
replaceThis();
|
replaceThis();
|
||||||
removeRedundantDefaultInitializers(arguments, parameters, body);
|
removeRedundantDefaultInitializers(arguments, parameters, body);
|
||||||
aliasArgumentsIfNeeded(renamingContext, arguments, parameters);
|
aliasArgumentsIfNeeded(renamingContext, arguments, parameters);
|
||||||
renameLocals(renamingContext, invokedFunction);
|
renameLocalNames(renamingContext, invokedFunction);
|
||||||
applyRenaming();
|
|
||||||
replaceReturns();
|
if (canBeExpression(body)) {
|
||||||
|
applyRenaming();
|
||||||
|
doDirectInline();
|
||||||
|
} else {
|
||||||
|
replaceReturns();
|
||||||
|
applyRenaming();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void replaceThis() {
|
private void replaceThis() {
|
||||||
@@ -121,6 +127,11 @@ class FunctionInlineMutator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doDirectInline() {
|
||||||
|
resultExpr = asExpression(body);
|
||||||
|
body.getStatements().clear();
|
||||||
|
}
|
||||||
|
|
||||||
private void doReplaceReturns(int returnCount) {
|
private void doReplaceReturns(int returnCount) {
|
||||||
JsReturn returnOnTop = ContainerUtil.findInstance(body.getStatements(), JsReturn.class);
|
JsReturn returnOnTop = ContainerUtil.findInstance(body.getStatements(), JsReturn.class);
|
||||||
boolean hasReturnOnTopLevel = returnOnTop != null;
|
boolean hasReturnOnTopLevel = returnOnTop != null;
|
||||||
@@ -153,4 +164,17 @@ class FunctionInlineMutator {
|
|||||||
private List<JsParameter> getParameters() {
|
private List<JsParameter> getParameters() {
|
||||||
return invokedFunction.getParameters();
|
return invokedFunction.getParameters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean canBeExpression(JsBlock body) {
|
||||||
|
List<JsStatement> statements = body.getStatements();
|
||||||
|
return statements.size() == 1 && statements.get(0) instanceof JsReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JsExpression asExpression(JsBlock body) {
|
||||||
|
assert canBeExpression(body);
|
||||||
|
|
||||||
|
List<JsStatement> statements = body.getStatements();
|
||||||
|
JsReturn returnStatement = (JsReturn) statements.get(0);
|
||||||
|
return returnStatement.getExpression();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user