Complete guide on: inline vs no inline function in kotlin!
Functions are a very powerful feature in Kotlin, and when it comes to performance and code optimization, the use of inline
and noinline
keywords 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 inline
Function?
inline
function 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 noinline
function?
When we don’t want to inline a specific lambda inside an inline function, then we put the noinline
keyword 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 inline
function?
- If the function is very large, or our lambdas are performing very heavy operations, then
inline
can lead to code bloating.
2. If the function is rarely called, then inline
is of no use.
Conclusion
inline
and noinline
both are useful for specific scenarios in Kotlin. inline
is for performance, while noinline
is for flexibility. Both should be used smartly so that your code remains optimized and maintainable.
Hope you have become clear about the concept ofinline
and noinline
functions! 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!