keishi kubo
02/24/2020, 1:59 PMUsing higher-order functions imposes certain runtime penalties: each function is an object, and it captures a closure, i.e. those variables that are accessed in the body of the function. Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead.
But it appears that in many cases this kind of overhead can be eliminated by inlining the lambda expressions.
satyan
02/24/2020, 2:02 PMeach function is an object, and it captures a closure
keishi kubo
02/24/2020, 2:17 PMEvan R.
02/24/2020, 3:31 PMcontinue
or return
from inside the passed lambda targeting an outside function or loop
2. You incur the overhead of copying all the required values into the function call
To avoid this, you can write an “inline function” which, instead of doing all that copy and stack stuff, the compiler will just copy-paste the code from your inline function to the place you’re calling it. For example, when the following is compiled:
inline fun printlnYell(message: String) {
println(message.toUpperCase())
}
fun main() {
printlnYell("Hello world")
}
The bytecode will represent something more like this:
fun main() {
println("Hello world".toUpperCase())
}
keishi kubo
02/25/2020, 3:51 PMEvan R.
02/25/2020, 3:55 PM