inline Functions: Eliminating Lambda Overhead
Mark functions as inline to avoid object creation for lambda parameters.
inline Functions: Eliminating Lambda Overhead is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is an inline Function?
Normally, a lambda passed to a function is compiled into an anonymous class. Each invocation allocates a new object, which costs memory and time. The inline keyword tells the compiler to copy the function body (and its lambda arguments) directly into the call site.
Declaring inline
Add the inline modifier before fun:
inline fun measureTime(block: () -> Unit): Long {
val start = System.nanoTime()
block()
return System.nanoTime() - start
}How the Compiler Rewrites the Call
A call like measureTime { doWork() } becomes, at the bytecode level, the body of measureTime pasted inline — no lambda object is ever allocated.
When to Use inline
Use inline when:
- The function takes one or more lambda parameters
- It is called frequently in hot paths (tight loops, composables)
- You need
reifiedtype parameters (covered next)
Performance Benchmark Example
Without inline, each call to a higher-order function allocates a lambda object. With it, the JVM sees straight-line code — no allocation, no virtual dispatch.
inline fun repeat(n: Int, block: () -> Unit) {
for (i in 0 until n) block()
}Non-Local Returns
A key benefit of inline functions: the lambda can use return to exit the enclosing function, not just the lambda. This is called a non-local return.
inline fun forEach(list: List<Int>, block: (Int) -> Unit) {
for (item in list) block(item)
}
fun findFirst(list: List<Int>): Int {
forEach(list) { if (it > 5) return it } // returns from findFirst
return -1
}Limitations of inline
Inline functions cannot be stored in variables or passed as objects because they do not exist as first-class entities at runtime. You cannot call f(::measureTime) where f expects a function reference of that signature.
Inlining Collections Pipelines
The standard library functions map, filter, forEach, etc. are all inline. This is why chaining them does not create extra closure objects per element.
val result = (1..1_000_000)
.filter { it % 2 == 0 } // inline
.map { it * it } // inline
.take(10)Code Size Trade-off
Each call site receives a copy of the inlined code. If the function body is large and called from many places, the bytecode size grows. Prefer inline for small, frequently-called lambdas.
Interaction with companion and top-level
inline works on top-level functions and member functions. It does not work on properties or constructors. The function must be visible at the call site (usually public or internal).
Standard Library Examples
Functions you already know that are inline: let, run, apply, also, with, use, synchronized, lazy (block variant). The inline modifier is what makes their lambdas zero-cost.
Quick Check
What is the primary benefit of marking a higher-order function as inline?
Recap: inline Functions
Key takeaways:
inlineeliminates lambda object allocation- Enables non-local returns from lambdas
- Required for
reifiedtype parameters - Best for small, frequently-called higher-order functions
- Standard library scope functions are all
inline
Frequently asked questions
Is the “inline Functions: Eliminating Lambda Overhead” lesson free?
Yes — the full text of “inline Functions: Eliminating Lambda Overhead” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “inline Functions: Eliminating Lambda Overhead”?
Mark functions as inline to avoid object creation for lambda parameters. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “inline Functions: Eliminating Lambda Overhead” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- inline Functions: Eliminating Lambda Overhead
- noinline and crossinline Modifiers
- reified Type Parameters: Accessing T at Runtime
- Practical Reified Patterns: Parsing, DI & Serialization