JVM optimize unneeded temporary vals

This commit is contained in:
Dmitry Petrov
2021-08-23 16:59:17 +03:00
committed by TeamCityServer
parent d8f6d82411
commit f4a1e27124
10 changed files with 122 additions and 47 deletions
@@ -12,4 +12,8 @@ suspend fun suspendThere(param: Int, param2: String, param3: Long): String {
return a + b
}
// JVM_TEMPLATES
// 1 ASTORE 4
// JVM_IR_TEMPLATES
// 0 ASTORE 4
@@ -41,8 +41,28 @@ fun box(): String {
return result
}
// 1 LOCALVARIABLE i Ljava/lang/String; L.* 3
// 1 PUTFIELD VarValueConflictsWithTableSameSortKt\$box\$1.L\$0 : Ljava/lang/Object;
// We merge LVT records for two consequent branches, but we split the local over the restore code.
// JVM_IR_TEMPLATES
/* 1 load in VarValueConflictsWithTableSameSortKt$box$1 <init> ($completion) */
// 1 ALOAD 2\s+INVOKESPECIAL kotlin/coroutines/jvm/internal/SuspendLambda\.\<init\>
/* 1 load in the catch (e: Throwable) { throw e } block which is implicitly wrapped around try/finally */
// 1 ALOAD 2\s+ATHROW
/* 1 load in result = s */
// 1 ALOAD 2\s+PUTFIELD kotlin/jvm/internal/Ref\$ObjectRef\.element
/* 1 load in spill */
// 1 ALOAD 2\s+PUTFIELD VarValueConflictsWithTableSameSortKt\$box\$1\.L\$0 : Ljava/lang/Object;
/* 2 loads in println(s) */
// 2 ALOAD 2\s+INVOKEVIRTUAL java/io/PrintStream.println \(Ljava/lang/Object;\)V
/* But no further load when spilling 's' to the continuation */
// 6 ALOAD 2
// 1 LOCALVARIABLE i Ljava/lang/String; L.* 2
// 3 LOCALVARIABLE s Ljava/lang/String; L.* 2
// 1 LOCALVARIABLE \$completion Lkotlin/coroutines/Continuation; L.* 2
// JVM_TEMPLATES
/* 1 load in the catch (e: Throwable) { throw e } block which is implicitly wrapped around try/finally */
// 1 ALOAD 3\s+ATHROW
/* 1 load in result = s */
@@ -53,9 +73,5 @@ fun box(): String {
// 2 ALOAD 3\s+INVOKEVIRTUAL java/io/PrintStream.println \(Ljava/lang/Object;\)V
/* But no further load when spilling 's' to the continuation */
// 5 ALOAD 3
// We merge LVT records for two consequent branches, but we split the local over the restore code.
// JVM_IR_TEMPLATES
// 3 LOCALVARIABLE s Ljava/lang/String; L.* 3
// JVM_TEMPLATES
// 1 LOCALVARIABLE i Ljava/lang/String; L.* 3
// 4 LOCALVARIABLE s Ljava/lang/String; L.* 3
@@ -17,8 +17,17 @@ suspend fun ApplicationCall.test(authenticationService: AuthenticationService) {
respond(authenticationService.execute(receiveJSON()))
}
// JVM_TEMPLATES
// $i$f$respond x1, $i$f$receiveJSON x2: before and after suspension point
// 3 ISTORE 5
// 0 ILOAD 5
// 2 \$i\$f\$receiveJSON I .* 5
// 1 \$i\$f\$respond I .* 5
// JVM_IR_TEMPLATES
// 5 ISTORE 3
// 1 ISTORE 2
// 0 ILOAD 3
// 0 ILOAD 2
// 1 \$i\$f\$receiveJSON I .* 2
// 3 \$i\$f\$respond I .* 3
@@ -21,6 +21,13 @@ fun foo() : String {
)
}
// JVM_TEMPLATES
// 12 ALOAD
// 2 ASTORE
// 0 InlineMarker
// JVM_IR_TEMPLATES
// 11 ALOAD
// 1 ASTORE
// 0 InlineMarker
@@ -11,7 +11,14 @@ fun foo() : String {
return foobar("abc", bar("ghi") { x -> x + "jkl" }, "mno")
}
// JVM_TEMPLATES
// 6 ASTORE
// 18 ALOAD
// 1 MAXLOCALS = 7
// 0 InlineMarker
// JVM_IR_TEMPLATES
// 4 ASTORE
// 16 ALOAD
// 1 MAXLOCALS = 7
// 0 InlineMarker