Optimize recursive string concatenation

This commit is contained in:
Michael Bogdanov
2016-02-15 16:42:18 +03:00
parent 8835b0599a
commit 0283fea835
3 changed files with 22 additions and 2 deletions
@@ -3184,7 +3184,9 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
public void invokeAppend(KtExpression expr) {
if (expr instanceof KtBinaryExpression) {
ConstantValue<?> compileTimeConstant = getPrimitiveOrStringCompileTimeConstant(expr, bindingContext);
if (compileTimeConstant == null && expr instanceof KtBinaryExpression) {
KtBinaryExpression binaryExpression = (KtBinaryExpression) expr;
if (binaryExpression.getOperationToken() == KtTokens.PLUS) {
KtExpression left = binaryExpression.getLeft();
@@ -3198,8 +3200,13 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
}
}
Type exprType = expressionType(expr);
gen(expr, exprType);
if (compileTimeConstant != null) {
StackValue.constant(compileTimeConstant.getValue(), exprType).put(exprType, v);
} else {
gen(expr, exprType);
}
genInvokeAppendMethod(v, exprType.getSort() == Type.ARRAY ? OBJECT_TYPE : exprType);
}
@@ -0,0 +1,7 @@
fun box(): String {
val z = 1;
return "O" + "K".toString() + z
}
// 1 LDC "OK"
// 1 LDC
@@ -625,6 +625,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("partialString.kt")
public void testPartialString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/constants/partialString.kt");
doTest(fileName);
}
@TestMetadata("short.kt")
public void testShort() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/constants/short.kt");