Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)
Inline Functions, `noinline`, and `crossinline`: Non-local returns and bytecode inlining.
senior LevelPerformance
In Simple Words:
Passing lambdas in Kotlin creates runtime Function object allocations on the heap:
inline: Instructs the compiler to paste the function's bytecode and the lambda's bytecode directly into the call site, eliminating heap allocations.- Non-Local Returns: Inlined lambdas can return from the enclosing caller function directly (
return). crossinline: Allows inlining while forbidding non-local returns (required when the lambda is executed inside a local object or sub-thread).noinline: Prevents inlining for specific lambda parameters if they need to be stored in a variable.