When to use inline
When a lambda is included in a method parameter
What is the role of inlining
To improve performance, let's not talk nonsense and go directly to the code
without inlining
import java.util.*
fun main() {
inlineTest() { result, code ->
println("resout:$result code:$code")
}
}
fun inlineTest(res: (String, Int) -> Unit) {
res("错了错了", 404)
}
View bytecode
Looking at the compiled bytecode, we can know that it is packaged as
The Function2 object then performs a series of operations. The process is more complicated
plus inline
Re-view the bytecode in the above way
Something magical happened. The code is more concise. Efficiency has also improved.
PS: with or without the effect, the output is the same
resout:错了错了 code:404
refer to: