Hi, I am Amit Shekhar, Co-Founder @ Outcome School • IIT 2010-14 • I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we will learn about the inline
function in Kotlin.
This article was originally published at Outcome School.
What is an inline
function in Kotlin?
Inline
function instruct compiler to insert the complete body of the function wherever that function gets used in the code.
Let's understand it with an example:
fun guide() {
print("guide start")
teach()
print("guide end")
}
fun teach() {
print("teach")
}
In this example, we have two functions:
guide()
teach()
Both are normal functions. guide()
function calls the teach()
function.
Let's see the decompiled code in order to understand it.
For that, we will have to convert this Kotlin source file to a Java source file.
Steps to convert from Kotlin source file to Java source file and decompile in Android Studio:
-
Tools
>Kotlin
>Show Kotlin Bytecode
. You will get the bytecode of your Kotlin file. - Now click on the
Decompile
button to get your Java code from the bytecode.
We will get the following:
public void guide() {
System.out.print("guide start");
teach();
System.out.print("guide end");
}
public void teach() {
System.out.print("teach");
}
Here, we can see that the guide()
function calls the teach()
function as usual.
Now let's add the inline
keyword to the teach()
function.
fun guide() {
print("guide start")
teach()
print("guide end")
}
inline fun teach() {
print("teach")
}
Again, let's see the decompiled code. The decompiled code is as below:
public void guide() {
System.out.print("guide start");
System.out.print("teach");
System.out.print("guide end");
}
Now, we can see that the code of teach()
function is copied inside the guide()
function. And the guide()
function is no more calling the teach()
function.
This is because we have used the inline
keyword.
Advantage of inline
function: Function call overhead doesn't occur. Less overhead and faster program execution.
So, when to make the function inline
and when not:
- When the function code is very small, it's a good idea to make the function
inline
. - When the function code is large and called from so many places, it's a bad idea to make the function
inline
, as the large code will be repeated again and again.
Now, let's take an example with Higher-Order Function and Lambdas.
fun guide() {
print("guide start")
teach {
print("teach")
}
print("guide end")
}
fun teach(abc: () -> Unit) {
abc()
}
Again, let's go to the decompiled code. The decompiled code is as below:
public void guide() {
System.out.print("guide start");
teach(new Function() {
@Override
public void invoke() {
System.out.print("teach");
}
});
System.out.print("guide end");
}
public void teach(Function abc) {
abc.invoke();
}
Now let's add the inline
keyword to the teach()
function.
fun guide() {
print("guide start")
teach {
print("teach")
}
print("guide end")
}
inline fun teach(abc: () -> Unit) {
abc()
}
Again, let's go to the decompiled code. The decompiled code is as below:
public void guide() {
System.out.print("guide start");
System.out.print("teach");
System.out.print("guide end");
}
As we can see that the code of teach()
function is placed inside the guide()
function.
This is how inline
can help us to make the execution fast by avoiding the function calls.
Now, we have understood the inline
keyword in Kotlin.
Learn about noinline: noinline in Kotlin
Learn about crossinline: crossinline in Kotlin
Watch the video format: inline function in Kotlin
Master Kotlin Coroutines from here: Mastering Kotlin Coroutines
That's it for now.
Thanks
Amit Shekhar
Co-Founder @ Outcome School
You can connect with me on:
Top comments (0)