FLORINA MUNTENESCU: You know all of those util
files you create with all sorts of small functions
that you end up using a lot throughout your app?
If your utility functions get other functions as parameters,
chances are you can improve the performance of your app
by saving some extra object allocations
with one keyword, inline.
Let's see what happens when you pass these short functions
around, what inline does under the hood,
and what you should be aware of when
working with inline functions.
If you learn something new, like the video
and subscribe to the channel but only
if you think we've earned it.
[MUSIC PLAYING]
So let's take an example.
Let's say that you use shared preferences a lot in your app,
so you create this utility function
to reduce the boilerplate.
Then you use it to save a string token.
Now let's see what's going on under the hood
when preferences dot edit is called.
If we look at the Kotlin byte code,
we see that there is a new called.
So a new object is being created even if in our code
we actually didn't call any object constructor.
Let's check the decompile code to make this a bit friendlier.
The edit share preferences extension
function we define is here in the decompile code.
The share preferences object is passed in.
For the commit flag, the default value false is passed.
For the action, we can see that a new function
object is created.
And finally, our action implementation is now here.
So remember each higher order function
we create leads to a function object creation and memory
allocation that introduces runtime overhead.
To improve the performance of our app,
we can avoid the new function object creation
by using the inline keyword.
Now the called in byte code does not contain any new calls,
and here's how our decompiled Java code looks like.
We can now see that things have changed a lot
for our safe token method.
Here we have the content of the shared preferences
dot edit function directly in our save token method.
And then here, we have the content
of the action implementation.
So what happened?
Because of the inline keyword, the compiler
copies the content of the inline function
to the call site avoiding creating a new function object.
If you're trying to mark as inline a function that
doesn't accept another function as a parameter,
then you won't get significant performance benefits.
And actually the ID would even tell you that,
suggesting you to remove it.
Because in lining may cause the generated code to grow a lot,
make sure that you avoid inlining the large functions.
So, for example, if you check the Kotlin standard library
in files like collections of KT, you'll
see that most of the inline functions
have only one to three lines.
When using inline functions, you're
not allowed to keep a reference to the functions passed
as a parameter or pass it or a different function.
You'll get a compiler error saying illegal usage
of inline parameter.
So, for example, let's modify the edit method
and add another parameter and important action let's
call it that is then passed to a different function.
We can see that my function of important action
produces an error.
We have two cases here.
Case 1, if you have multiple functions as parameters
and you only need to keep a reference to one of them,
then you can market it as no inline.
By using no inline, the compiler will create a new function
object only for that specific function,
but the rest will be inlined.
If we check the byte code, we see that a new call indeed
appeared.
In the decompile code, we can see
that everything is inline except for that important action
function, and only here on new function object is created.
Case 2, if your function only has one function as parameter
and you need to start with that reference.
So here just prefer not using inline at all.
If you do add in line, then you have
to mark your parameter with no inline,
but this could cause low performance benefits
by inlining the method.
So there's no point in using it.
So to summarize, to decrease the memory allocations caused
by lambda expressions, use the inline keyword.
Make sure you apply to small functions
that they call lambda as a parameter.
If you need to keep a reference to a lambda
or pass it as an argument or another function,
use that no inline keyword.
So start inlining to start saving with Kotlin.
[MUSIC PLAYING]