Kotlin inline / crossinline / noinline Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back to another kotlin bites episode my name is jacob and in this episode we're going to be talking about inline functions and how they differ from cross in line and no inline let's get started you may want to consider inline functions if the respective functions contain lambdas as a parameter inlining often promotes higher performance when executing such a function if you're unfamiliar with lambdas or in this case closures watch my video on callbacks closures and lambdas linked below let's first show an example of a function that can be inlined however it's not yet i've already created this extension function upon a collection of integers it is very similar to the built-in function for reach except it only returns the integers on the even indices you'll notice that i loop through all values in the collection if the index is even i call the lambda closure in my main function i call this function in this case i am printing the integers that are on the even indices before inlining let's first take a look at the bytecode for this file observing the bytecode is essential to seeing the difference between inline functions and inline functions visually to do this double tap shift and type show kotlin bytecode the bytecode will appear on the side here feel free to look at the bytecode yourself however it's a bit too complex for this video so instead we'll decompile the bytecode to java here is the decompiled bytecode you will see pretty much what we expect within the main function you'll see the creation of the list and then we call the for every other function okay now let's inline the function and observe the bytecode again you'll notice that instead of calling the for every other function after creating the list the compiler is actually embedded the code into the main function most importantly the print statement we had in our closure is now embedded within the for loop inline here significantly reduces the function stack while executing your program usually resulting in better runtime performance here is another inline function example it takes two integers and compares them then returns a string as a response to a closure however this response closure needs to be marked as cross inline since it is being called from within a local closure callback the local callback is this runnable object you'll notice that if we remove the cross inline modifier there's an error explaining why we need it let's look at the bytecode to see how this cross-inline function is compiled as you can see the majority of the function has been inlined the exception is this runnable object it looks like the compiler has created a new class to handle this runnable here it is again the critical thing here is to note the callbacks have been removed our print statement has been copied three times into the run function since that is what we had in our original closure so the critical thing that an inline function does is twofold first it inlines the function definition wherever it's called if possible second it inlines the closure body into the embedded function wherever it calls the closure the difference between inline functions and cross and line functions that the cross inline lambda is separated into its own class finally let's look at no inline there may be a situation where inline and lambda is not possible for example if we have a super secret function that is not inlined and we would like to call it within another inline function here we have two choices we can either cross in line the response lambda or since the response lambda has the same signature as the super secret function response lambda we could simply pass it along but in this case we'll have to mark the responses no inline since it is being passed to a non-inline function observing the bytecode shows that the function was indeed mostly inlined even the first lambda was in line however the response to the super secret function is not it is just called like normal when should you not use the inline modifier well when there's no closures there's no reason to the code still could be inlined however you were just increasing your binary size since this code will be copied whatever it's called fortunately the compiler warns us about this with that being said there is a situation where inline a lambda-less function is appropriate refine generics if you mark it generic as a ridify it will require the function to be inlined areified generic allows you to access the class information using reflection without passing an instance to its class object in this example you can see i'm printing each class's simple name this will work on any object when we look at the bytecode you'll notice that this works because the compiler simply replaces the generic with the required object that is it for this episode of collard bytes thanks for watching if you like what you see please like and consider subscribing source code and additional resources are in the comments below if there's anything you would like to see me explain in a later video please message me or leave a comment otherwise have a great day [Music] you
Info
Channel: KotlinBytes
Views: 3,891
Rating: undefined out of 5
Keywords:
Id: QsOXgTwLqIo
Channel Id: undefined
Length: 6min 14sec (374 seconds)
Published: Sun Nov 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.