Spill stack for inline functions only if it's required

Stack should be spilled before inline function call and restored after
call only if one of the following conditions is met:
- inline function is a suspend function
- inline function has try-catch blocks
- inline function has loops (backward jumps)

Note that there're quite some "simple" inline functions in Kotlin stdlib
besides run/let/with/apply. For example, many string operations are
implemented as inline wrappers over Java method calls.
This commit is contained in:
Dmitry Petrov
2017-10-06 14:26:54 +03:00
parent 1845a87813
commit e71090ae4c
6 changed files with 92 additions and 20 deletions

View File

@@ -1,4 +1,9 @@
inline fun bar(x: Int, y: Long, z: Byte, s: String) = x.toString() + y.toString() + z.toString() + s
inline fun <T> runAfterLoop(fn: () -> T): T {
for (i in 1..2);
return fn()
}
inline fun bar(x: Int, y: Long, z: Byte, s: String) = runAfterLoop { x.toString() + y.toString() + z.toString() + s }
fun foobar(x: Int, y: Long, s: String, z: Byte) = x.toString() + y.toString() + s + z.toString()
@@ -6,10 +11,10 @@ fun foo() : String {
return foobar(1, 2L, bar(3, 4L, 5.toByte(), "6"), 7.toByte())
}
// 3 ISTORE
// 7 ILOAD
// 9 ISTORE
// 13 ILOAD
// 2 ASTORE
// 6 ALOAD
// 8 ALOAD
// 2 LSTORE
// 4 LLOAD
// 1 MAXLOCALS = 10

View File

@@ -1,17 +1,22 @@
inline fun <T> runAfterLoop(fn: () -> T): T {
for (i in 1..2);
return fn()
}
fun bar() : Boolean = true
fun foobar(x: Boolean, y: String, z: String) = x.toString() + y + z
inline fun foo() = "-"
inline fun foo() = runAfterLoop { "-" }
fun test() {
val result = foobar(if (1 == 1) true else bar(), foo(), "OK")
}
// 1 ISTORE
// 2 ILOAD
// 7 ISTORE
// 8 ILOAD
// 2 ASTORE
// 5 ALOAD
// 7 ALOAD
// 1 MAXLOCALS = 3
// 1 MAXLOCALS = 4
// 0 InlineMarker

View File

@@ -9,6 +9,6 @@ fun foo() : Int {
return foobar(1, bar(2), 3)
}
// 3 ISTORE
// 7 ILOAD
// 1 ISTORE
// 5 ILOAD
// 0 InlineMarker

View File

@@ -1,5 +1,10 @@
inline fun <T> runAfterLoop(fn: () -> T): T {
for (i in 1..2);
return fn()
}
inline fun bar(block: () -> String) : String {
return block()
return runAfterLoop(block)
}
inline fun bar2() : String {
@@ -16,6 +21,6 @@ fun foo() : String {
)
}
// 10 ALOAD
// 12 ALOAD
// 2 ASTORE
// 0 InlineMarker

View File

@@ -1,4 +1,10 @@
inline fun bar(x: String, block: (String) -> String) = "def" + block(x)
inline fun <T> runAfterLoop(fn: () -> T): T {
for (i in 1..2);
return fn()
}
inline fun bar(x: String, block: (String) -> String) = runAfterLoop { "def" + block(x) }
fun foobar(x: String, y: String, z: String) = x + y + z
fun foo() : String {
@@ -6,6 +12,6 @@ fun foo() : String {
}
// 6 ASTORE
// 16 ALOAD
// 18 ALOAD
// 1 MAXLOCALS = 7
// 0 InlineMarker