Perform bytecode optimisations for inline classes

#KT-23742 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2018-06-15 15:36:47 +03:00
parent 7465289811
commit 01d9be65bc
16 changed files with 204 additions and 71 deletions

View File

@@ -1,3 +1,5 @@
// !LANGUAGE: +InlineClasses
// https://youtrack.jetbrains.com/issue/KT-15871
// FILE: Test.kt
@@ -10,8 +12,20 @@ fun getAndCheckInt(a: Int, b: Int) =
// 0 Value
// 0 areEqual
// FILE: TestInlined.kt
fun getAndCheckInlinedInt(a: InlinedInt, b: InlinedInt) =
getAndCheck({ a }, { b })
// @TestInlinedKt.class:
// 0 valueOf
// 0 Value
// 0 areEqual
// 0 INVOKESTATIC InlinedInt\$Erased.box
// 0 INVOKEVIRTUAL InlinedInt.unbox
// FILE: Inline.kt
inline fun <T> getAndCheck(getFirst: () -> T, getSecond: () -> T) =
getFirst() == getSecond()
inline class InlinedInt(val x: Int)

View File

@@ -1,3 +1,6 @@
// !LANGUAGE: +InlineClasses
// FILE: Test.kt
inline fun <R, T> foo(x : R, y : R, block : (R) -> T) : T {
val a = x is Number
@@ -17,7 +20,26 @@ fun bar() {
foo(1, 2) { x -> x is Int }
}
// @TestKt.class:
// 0 valueOf
// 0 Value\s\(\)
// 2 INSTANCEOF
// 1 CHECKCAST
// FILE: Inline.kt
inline class InlinedInt(val x: Int)
// FILE: TestInlined.kt
fun baz() {
foo(InlinedInt(1), InlinedInt(2)) { x -> x is InlinedInt }
}
// @TestInlinedKt.class:
// 0 valueOf
// 0 Value\s\(\)
// 0 INSTANCEOF
// 0 CHECKCAST
// 0 INVOKESTATIC InlinedInt\$Erased.box
// 0 INVOKEVIRTUAL InlinedInt.unbox

View File

@@ -0,0 +1,36 @@
// !LANGUAGE: +InlineClasses
// FILE: dependency.kt
inline class InlinedInt(val internal: Int)
inline class InlinedString(val internal: String)
inline fun <T> foo(callback: () -> T): T {
return callback()
}
inline fun bar(callback: () -> InlinedInt): InlinedInt {
return callback()
}
inline fun baz(callback: () -> InlinedString): InlinedString {
return callback()
}
// FILE: test.kt
fun test(i: InlinedInt, s: InlinedString) {
foo { i }
bar { i }
foo { s }
baz { s }
}
// @TestKt.class:
// 0 valueOf
// 0 INVOKESTATIC InlinedInt\$Erased.box
// 0 INVOKEVIRTUAL InlinedInt.unbox
// 0 INVOKESTATIC InlinedString\$Erased.box
// 0 INVOKEVIRTUAL InlinedString.unbox

View File

@@ -6,7 +6,7 @@ fun f() {
val unull = UInt(1) ?: null
}
// 1 INVOKESTATIC UInt\$Erased.box
// 0 INVOKESTATIC UInt\$Erased.box
// 0 INVOKEVIRTUAL UInt.unbox
// 0 valueOf

View File

@@ -12,8 +12,8 @@ fun test(x: UInt?, y: UInt) {
}
}
// 2 INVOKESTATIC UInt\$Erased.box
// 3 INVOKEVIRTUAL UInt.unbox
// 0 INVOKESTATIC UInt\$Erased.box
// 1 INVOKEVIRTUAL UInt.unbox
// 0 valueOf
// 0 intValue

View File

@@ -9,17 +9,17 @@ fun <T> T.idExtension(): T = this
inline fun <T> T.inlinedIdExtension(): T = this
fun test(f: Foo) {
inlinedId(f) // box
inlinedId(f)
inlinedId(f).idExtension() // box
f.inlinedIdExtension() // box
f.inlinedIdExtension()
val a = inlinedId(f).idExtension() // box unbox
val b = inlinedId(f).inlinedIdExtension() // box unbox
val b = inlinedId(f).inlinedIdExtension()
}
// 5 INVOKESTATIC Foo\$Erased.box
// 2 INVOKEVIRTUAL Foo.unbox
// 2 INVOKESTATIC Foo\$Erased.box
// 1 INVOKEVIRTUAL Foo.unbox
// 0 valueOf
// 0 intValue

View File

@@ -7,39 +7,39 @@ inline class InlineNullableReference(val a: Any?)
fun test1(a: InlineNotNullPrimitive) {
val a0 = a
val a1: Any = a // box
val a2: Any? = a // box
val a1: Any = a
val a2: Any? = a
val a3: InlineNotNullPrimitive = a
val a4: InlineNotNullPrimitive? = a // box
val a4: InlineNotNullPrimitive? = a
}
fun test2(b: InlineNullablePrimitive) {
val b0 = b
val b1: Any = b // box
val b2: Any? = b // box
val b1: Any = b
val b2: Any? = b
val b3: InlineNullablePrimitive = b
val b4: InlineNullablePrimitive? = b // box
val b4: InlineNullablePrimitive? = b
}
fun test3(c: InlineNotNullReference) {
val c0 = c
val c1: Any = c // box
val c2: Any? = c // box
val c1: Any = c
val c2: Any? = c
val c3: InlineNotNullReference = c
val c4: InlineNotNullReference? = c
}
fun test4(d: InlineNullableReference) {
val d0 = d
val d1: Any = d // box
val d2: Any? = d // box
val d1: Any = d
val d2: Any? = d
val d3: InlineNullableReference = d
val d4: InlineNullableReference? = d // box
val d4: InlineNullableReference? = d
}
// 3 INVOKESTATIC InlineNotNullPrimitive\$Erased.box
// 3 INVOKESTATIC InlineNullablePrimitive\$Erased.box
// 2 INVOKESTATIC InlineNotNullReference\$Erased.box
// 3 INVOKESTATIC InlineNullableReference\$Erased.box
// 0 INVOKESTATIC InlineNotNullPrimitive\$Erased.box
// 0 INVOKESTATIC InlineNullablePrimitive\$Erased.box
// 0 INVOKESTATIC InlineNotNullReference\$Erased.box
// 0 INVOKESTATIC InlineNullableReference\$Erased.box
// 0 valueOf

View File

@@ -16,8 +16,8 @@ fun UIntArray.swap(i: Int, j: Int) {
this[j] = this[i].also { this[i] = this[j] }
}
// 2 INVOKEVIRTUAL UInt.unbox
// 1 INVOKESTATIC UInt\$Erased.box
// 0 INVOKEVIRTUAL UInt.unbox
// 0 INVOKESTATIC UInt\$Erased.box
// 0 intValue
// 0 valueOf