Basics of the inline keyword in Kotlin functions

Subscribe to my newsletter and never miss my upcoming articles

As a developer, one of the most important things you have to be on the lookout for is your application’s memory usage. Overdo it and all the user has to do is simply uninstall your app and you have one less user. This is why Kotlin has a great feature that allows you to reduce memory allocation as much as possible. This feature comes in an extremely simple keyword; inline.

What does it do?

This keyword instructs the compiler to copy the inlined code of a function instead of allocating memory. This may sound ambiguous at first but, as usual, a code example will do wonders in helping with understanding this concept.

Consider the following code.

fun higherOrderFunction(string: String, function: (String) -> Unit) {
    function(string)
}

fun main() {
    higherOrderFunction("Testing inline", ::print) 
}
//Will print
Testing inline

This is simple code that passes a parameter to the passed-in function and then prints it out. For an understanding of what is happening, take a look at the following shot of the decompiled code.

inline1.jpeg

You will see the line function.invoke(string). By calling invoke on the function, the compiler has to create an extra call that increases memory usage. In the java world, this would be an anonymous class.

Assume that you have a utils class that has thousands of such methods that do not do much. You would end up with extra overhead during runtime that will affect your app.

Inline to the rescue

To see the benefit of inline, we have to modify our code by adding the word at the start of the function as below.

inline fun higherOrderFunction(string: String, function: (String) -> Unit) {
    function(string)
}

Just like that, we have massively improved the performance of the code. Let us pop the hood and see what is happening.

inline2.jpeg

Do you see the difference now? Unlike before, the main() method now copies the print lambda. Instead of the previous call to higherOrderFunction in main, the print statement is now copied and the printed out. Look at the following lines in the main method to see this.

String string$iv = "Testing inline";
.......// some code
System.out.print(string$iv);

Caveat: Only use the inline keyword to small functions. This is because inline increases the resulting bytecode (as you can see in the screenshots above), which may negatively affect code performance.

Additionally, the inline keyword is only useful to you as a developer if you have a higher-order function, that is, a function that takes it another function. If you try to inline a function that is not of a higher-order type, you will get one of the cold messages that Android Studio offers. See the following simple code.

fun concatenate(string1: String, string2: String) = "$string1$string2"

If you try to inline the above function, you will receive a warning message.

Expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types


So that is all about the basics of inline and how you can use it to improve your code performance. There is more about this keyword and I would advise you to read and watch videos for a better understanding.

Enjoy!

PS: Let us connect on GitHub, Twitter, and LinkedIn.

No Comments Yet