KT-5248 Don't wrap variable if it is captured only in inlined closures

Remove non-escaping Ref's on bytecode postprocessing pass.
This commit is contained in:
Dmitry Petrov
2017-01-31 15:43:17 +03:00
parent 3fc106572e
commit 3c09a26e16
38 changed files with 1115 additions and 26 deletions
@@ -0,0 +1,9 @@
inline fun runCrossInline(crossinline f: () -> Unit) {
f()
}
fun box(): String {
var x = ""
runCrossInline { x = "OK" }
return x
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun box(): String {
var x = ""
run { x = "OK" }
return x
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun box(): String {
var x = ""
run {
x += "O"
x += "K"
}
return x
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun box(): String {
var x = 0
run { x++ }
run { ++x }
return if (x == 2) "OK" else "Fail: $x"
}
@@ -0,0 +1,18 @@
// WITH_RUNTIME
class Host(var value: String) {
operator fun get(i: Int, j: Int, k: Int) = value
operator fun set(i: Int, j: Int, k: Int, newValue: String) {
value = newValue
}
}
fun box(): String {
var x = Host("")
run {
x[0, 0, 0] += "O"
x[0, 0, 0] += "K"
}
return x.value
}
@@ -0,0 +1,26 @@
// WITH_RUNTIME
fun box(): String {
var xl = 0L // Long, size 2
var xi = 0 // Int, size 1
var xd = 0.0 // Double, size 2
run {
xl++
xd += 1.0
xi++
}
run {
run {
xl++
xd += 1.0
xi++
}
}
if (xi != 2) return "fail: xi=$xi"
if (xl != 2L) return "fail: xl=$xl"
if (xd != 2.0) return "fail: xd=$xd"
return "OK"
}
@@ -0,0 +1,19 @@
// WITH_RUNTIME
fun box(): String {
run {
run {
var x = 0
run { ++x }
if (x == 0) return "fail"
}
run {
var x = 0
run { x++ }
if (x == 0) return "fail"
}
}
return "OK"
}
@@ -0,0 +1,41 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
class Controller {
var result = ""
suspend fun <T> suspendWithResult(value: T): T = suspendCoroutineOrReturn { c ->
c.resume(value)
COROUTINE_SUSPENDED
}
}
fun builder(c: suspend Controller.() -> Unit): String {
val controller = Controller()
c.startCoroutine(controller, EmptyContinuation)
return controller.result
}
fun box(): String {
val value = builder {
var r = ""
for (x in listOf("O", "$", "K")) {
if (x == "$") continue
run {
r += suspendWithResult(x)
}
}
run {
r += "."
}
result = r
}
if (value != "OK.") return "fail: suspend in for body: $value"
return "OK"
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
fun test() {
var x = 0
run {
run {
run {
++x
}
}
}
}
// 0 NEW
// 0 GETFIELD
// 0 PUTFIELD
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test() {
var x = 0
run { ++x }
}
// 0 NEW
// 0 GETFIELD
// 0 PUTFIELD
@@ -0,0 +1,15 @@
// WITH_RUNTIME
fun test() {
var x = 0
run {
val obj = object {
fun foo() { ++x }
}
obj.foo()
}
}
// 1 NEW kotlin/jvm/internal/Ref\$IntRef
// 2 GETFIELD kotlin/jvm/internal/Ref\$IntRef\.element
// 2 PUTFIELD kotlin/jvm/internal/Ref\$IntRef\.element
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun runNoInline(f: () -> Unit) = f()
fun test() {
var x = 0
runNoInline { ++x }
}
// 1 NEW kotlin/jvm/internal/Ref\$IntRef
// 2 GETFIELD kotlin/jvm/internal/Ref\$IntRef\.element
// 2 PUTFIELD kotlin/jvm/internal/Ref\$IntRef\.element
@@ -0,0 +1,20 @@
// WITH_RUNTIME
fun runNoInline(f: () -> Unit) = f()
fun test() {
var x = 0
run {
run {
runNoInline {
run {
++x
}
}
}
}
}
// 1 NEW kotlin/jvm/internal/Ref\$IntRef
// 2 GETFIELD kotlin/jvm/internal/Ref\$IntRef\.element
// 2 PUTFIELD kotlin/jvm/internal/Ref\$IntRef\.element
@@ -0,0 +1,25 @@
// WITH_RUNTIME
fun box(): String {
var xl = 0L // Long, size 2
var xi = 0 // Int, size 1
var xd = 0.0 // Double, size 2
run {
xl++
xd += 1.0
xi++
}
run {
run {
xl++
xd += 1.0
xi++
}
}
return "OK"
}
// 0 NEW
@@ -0,0 +1,32 @@
// WITH_RUNTIME
fun box(): String {
run {
run {
var x1 = 0
run { ++x1 }
if (x1 == 0) return "fail"
}
run {
var x2 = 0
run { x2++ }
if (x2 == 0) return "fail"
}
}
return "OK"
}
// Shared variable slots (x1, x2):
// 5 ILOAD 0
// 4 ISTORE 0
// Temporary variable slots for 'x2++':
// 1 ILOAD 1
// 1 ISTORE 1
// 0 NEW
// 0 GETFIELD
// 0 PUTFIELD
@@ -0,0 +1,14 @@
// WITH_RUNTIME
fun add(x: Int, y: Int) = x + y
fun test() {
var x = 0
run {
x += add(1, try { 1 } catch (e: Throwable) { 42 })
}
}
// 0 NEW
// 0 GETFIELD
// 0 PUTFIELD
@@ -0,0 +1,5 @@
@kotlin.Metadata
public final class CapturedInCrossinlineKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public final static method runCrossInline(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): void
}
@@ -0,0 +1,4 @@
@kotlin.Metadata
public final class CapturedInInlineOnlyAssignKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@@ -0,0 +1,4 @@
@kotlin.Metadata
public final class CapturedInInlineOnlyCAOKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@@ -0,0 +1,4 @@
@kotlin.Metadata
public final class CapturedInInlineOnlyIncrDecrKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@@ -0,0 +1,14 @@
@kotlin.Metadata
public final class CapturedInInlineOnlyIndexedCAOKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class Host {
private @org.jetbrains.annotations.NotNull field value: java.lang.String
public method <init>(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
public final @org.jetbrains.annotations.NotNull method get(p0: int, p1: int, p2: int): java.lang.String
public final @org.jetbrains.annotations.NotNull method getValue(): java.lang.String
public final method set(p0: int, p1: int, p2: int, @org.jetbrains.annotations.NotNull p3: java.lang.String): void
public final method setValue(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
}
@@ -0,0 +1,4 @@
@kotlin.Metadata
public final class CapturedVarsOfSize2Kt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@@ -0,0 +1,4 @@
@kotlin.Metadata
public final class SharedSlotsWithCapturedVarsKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@@ -0,0 +1,39 @@
@kotlin.Metadata
public final class Controller {
private @org.jetbrains.annotations.NotNull field result: java.lang.String
public method <init>(): void
public final @org.jetbrains.annotations.NotNull method getResult(): java.lang.String
public final method setResult(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
public final @org.jetbrains.annotations.Nullable method suspendWithResult(p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.experimental.Continuation): java.lang.Object
}
@kotlin.Metadata
public final class CoroutineUtilKt {
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.experimental.Continuation
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.experimental.Continuation
}
@kotlin.Metadata
public class EmptyContinuation {
public final static field Companion: EmptyContinuation.Companion
private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.experimental.CoroutineContext
inner class EmptyContinuation/Companion
public @synthetic.kotlin.jvm.GeneratedByJvmOverloads method <init>(): void
public method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.experimental.CoroutineContext): void
public synthetic method <init>(p0: kotlin.coroutines.experimental.CoroutineContext, p1: int, p2: kotlin.jvm.internal.DefaultConstructorMarker): void
public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.experimental.CoroutineContext
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
}
@kotlin.Metadata
public final static class EmptyContinuation/Companion {
inner class EmptyContinuation/Companion
private method <init>(): void
}
@kotlin.Metadata
public final class WithCoroutinesKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public final static @org.jetbrains.annotations.NotNull method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function2): java.lang.String
}