Kotlin Functions
Estimated reading: 3 minutes 295 views

Kotlin – Inline Functions: Optimize Lambda Performance

Introduction – Why Learn Kotlin Inline Functions?

Kotlin lets you pass functions (lambdas) as arguments to other functions. But each lambda typically creates a new object at runtime—leading to memory overhead. That’s where inline functions come in. They instruct the compiler to copy the function code directly at the call site, reducing performance costs.

In this guide, you’ll learn:

  • What inline functions are and why they’re used
  • How they reduce memory and call overhead
  • When to use noinline and crossinline
  • Best practices and real-world use cases

What Is an Inline Function?

An inline function is a higher-order function where the function call is replaced with its actual code during compilation.

Basic Syntax:

inline fun greet(action: () -> Unit) {
    println("Preparing to greet...")
    action()
    println("Greeted successfully!")
}

Call Site:

greet { println("Hello, Kotlin!") }

Output:

Preparing to greet...  
Hello, Kotlin!  
Greeted successfully!
  • The lambda { println("Hello, Kotlin!") } is inlined into the greet call.

Benefits of Inline Functions

BenefitExplanation
Reduced object allocationLambda objects are not created at runtime
Better performanceFunction call overhead is eliminated
Enables non-local returnsReturn directly from outer function inside lambda

Problem Without Inline

fun logAndRun(action: () -> Unit) {
    println("Running...")
    action() // Creates object for lambda
}
  • Every call to logAndRun { ... } creates a lambda object.
  • Use inline to avoid this when performance matters.

Non-Local Returns – Only in Inline

Inline functions allow return from the outer function inside a lambda:

inline fun doTask(block: () -> Unit) {
    block()
    println("Task Done")
}

fun runExample() {
    doTask {
        println("Returning early")
        return  //  Returns from runExample, not just block
    }
    println("This won't print")
}

Output:

Returning early

noinline – Exclude Some Lambdas from Inlining

If an inline function takes multiple lambda parameters, and you don’t want all of them inlined, use noinline:

inline fun process(a: () -> Unit, noinline b: () -> Unit) {
    a()       // gets inlined
    b()       // remains as a lambda object
}

Useful when passing lambda to another function or storing it in a variable.


crossinline – Prevent Non-local Returns

Use crossinline when you want the lambda inlined but disallow return from the outer function:

inline fun execute(crossinline block: () -> Unit) {
    val runnable = Runnable { block() } // return not allowed here
    runnable.run()
}

Ensures safe inlining inside another execution context like Thread, Runnable, etc.


Summary – Recap & Next Steps

Kotlin’s inline functions let you write fast, efficient higher-order functions by reducing object creation and enabling non-local control flow. They’re essential in performance-sensitive and lambda-heavy code.

Key Takeaways:

  • Use inline to improve lambda performance.
  • Enables return from the outer function.
  • Use noinline to skip inlining for specific lambdas.
  • Use crossinline to prevent non-local returns in inlined lambdas.

Practical Use:
Ideal in collection operations, DSLs, callbacks, Android click listeners, and thread-safe code execution.


FAQs – Kotlin Inline Functions

What does inline do in Kotlin?
It copies the function body and passed lambda code directly into the call site, avoiding overhead from function calls and object creation.


What is a non-local return in Kotlin?
It allows returning from the calling function, not just the lambda, only inside inline functions.


Why use noinline in Kotlin?
To prevent a specific lambda from being inlined, especially if it’s passed elsewhere or stored.


When should I use crossinline?
When you want a lambda to be inlined but prevent non-local returns for safety in callbacks or other contexts.


Do inline functions always improve performance?
No. Use them selectively—over-inlining can increase code size and reduce clarity.


Share Now :
Share

Kotlin – Inline Functions

Or Copy Link

CONTENTS
Scroll to Top