Complete guide on: inline vs no inline function in kotlin!

Android development with Kotlin
3 min readJan 19, 2025

--

Functions are a very powerful feature in Kotlin, and when it comes to performance and code optimization, the use of inline and noinlinekeywords play a very important role. Today we will try to understand both these concepts in simple words.

To dive deeper into the concepts of inline and noinline in Kotlin, make sure to check out our detailed article here.

What is inlineFunction?

inlinefunction is a function in which the code of the function becomes inline during execution, that is, the body of the function is directly replaced by its call. Its advantage is that the overhead of function calls is eliminated, and the program executes fast.

inline fun myInlineFunction(action: () -> Unit) {
println("Function started..")
action()
println("Function ended..")
}

fun main() {
myInlineFunction {
println("This is an inline function.")
}

Let’s try to decompile the code

public final class MainKt {
public static final void main() {
System.out.println("Function started..");
System.out.println("This is an inline function.");
System.out.println("Function ended..");
}
}

Here in the decompiled Java code, it can be seen that the body of myInlineFunction() is directly pasted inside the main function. That is, instead of treating the action lambda that was passed as a function call, its code was made directly inline. In this way, the overhead of function calls is eliminated, and the execution of the program becomes faster.

Output:

Function started..
This is an inline function.
Function ended..

What is noinlinefunction?

When we don’t want to inline a specific lambda inside an inline function, then we put the noinlinekeyword in front of that lambda. This means that it will treat lambda as a regular function, and it will not be inlined.

inline fun myNoInlineExample(noinline action: () -> Unit) {
println("Function execution started..")
action()
println("Function execution ended..")
}

fun main() {
myNoInlineExample {
println("This is a noinline lambda.")
}
}

Let’s try to decompile the above code in Java

public final class MainKt {
public static final void main() {
MainKt.myNoInlineExample(new Function0<Unit>() {
@Override
public Unit invoke() {
System.out.println("This is a noinline lambda.");
return Unit.INSTANCE;
}
});
}

public static final void myNoInlineExample(Function0<Unit> action) {
System.out.println("Function execution started..");
action.invoke();
System.out.println("Function execution ended..");
}
}

Here the action in the decompiled Java code is defined in the form of an anonymous class. When action.invoke() is called, the execution of lambda happens at runtime. This means that the action is not inlined, and a separate object is created for the JVM which is executed at runtime.

This kind of behavior is demanded when we have to pass the lambda to a higher-order function or invoke it dynamically. But this incurs the overhead of a regular function call because every time a new object is created and its invoke method will be executed.

Output:

Function execution started..
This is a noinline lambda.
Function execution ended..

When to useinline:

1. Performance is critical.

2. Lambda function is being called frequently.

3. Boilerplate code must be reduced.

When to use noinline:

1. Do not make the lambda inline (e.g., when passing the lambda to a higher-order function).

2. Code bloating should be avoided by increasing function size.

When not to use inlinefunction?

  1. If the function is very large, or our lambdas are performing very heavy operations, then inlinecan lead to code bloating.

2. If the function is rarely called, then inlineis of no use.

Conclusion

inlineand noinlineboth are useful for specific scenarios in Kotlin. inlineis for performance, while noinlineis for flexibility. Both should be used smartly so that your code remains optimized and maintainable.

Hope you have become clear about the concept ofinlineand noinlinefunctions! If you have doubts, please ask in the comment section.

“If you liked this article, please share it with your developer friends!”

Also please let me know your suggestions in the comments. Follow for more such easy and helpful tutorials.

Thanks for reading…

Happy Coding!

--

--

No responses yet