I understand there might be restrictions in your job, but why don't you guys start using it in your own projects? The earlier you start the better and, in my option, the transition is inevitable when java samples or resources start to be neglected by Google.
@@ArturStaniec well we're taking that into consideration as we realize Kotlin is getting more and more popular and pushed by Google, it's just a joke to make people like me who have not fully mastered the logic behind the language 😂
@@nadeemshaikh7863 It may be that you're joking but that is actually impossible. Flutter and Dart are completely independent runtime and implementation of components where Kotlin is just a language which compiles down to Java Bytecode essentially making it same as Java and using the native platform APIs and components
@@areebjamaliam You didn't get my point. I'm saying Google could or won't (more likely) to make flutter as the preferred way to make Android apps which would technically be fuchsia apps if Google transitions to fuchsia OS.
Good video, great demo of the Kotlin Kotlin bytecode Java decompiled code, nice editing work too! Would be nice to also see `crossinline` for lambdas that are invoked inside another lambda
If the rule is that simple (small functions which take a lambda, but don't pass or store it), why can't the Kotlin compiler decide by itself whether to inline a function or not?
Because inlining adds extra bytecodes to your apk (increase apk size). Some prefer smaller apk size because for them the runtime overhead isn't a problem. I think this is why they provided it as a keyword as an option.
2:34 You say it's not recommended to inline a function that doesn't have any function as parameter, but then on 2:52 you show so many functions that are inlined even though they don't have a function as parameter... What is going on?
I hope you found the answer. Still I’ll clarify, the inline functions show at 2:52 have lambdas as parameters which is just a fancy way of writing a function.
@@zeusalmighty6740 What do you mean? Those are very simple functions, and they could be auto-inlined anyway during runtime. What's so special about them?
@@zeusalmighty6740 Please explain. She said to avoid this in these cases, and yet I see "inline" for them. Why is that? Also, explain how could this be better than not putting "inline"? Show a sample snippet in which it behaves differently (with "inline" vs without it).
@@LiranBarsisa Plain kotlin won’t be enough to explain this. These are optimisations at byte-code level. I recommend you to make two functions one inline and other not inline and both take a function as parameter. Check the decompiled byte-code and also you can check the memory it uses.
I only came across this when accepting a suspend lambda as an argument in an inline function. I suspect it would be preferred over noinline but I'm not sure. Explanation would be really helpful!
@@JolanRensen I did the same, then tried to decompile that piece of code. It took about 40 mins of 100% CPU usage. At the end, I got java class which weight about 320 MB while origin was 2 KB :D
very true, Kotlin gives flexibility of the C++ like inline, coroutines and so on, next big step will be manual alloc/dealloc objects and reference counting - really dead end. Just my subjective opinion - Kotlin should be clear and easier to understand then Java but on practice - Java is much more readable and has less complicated constructions.
As a Kotlin GDE, can you tell me why the compiler itself doesn't take care of this itself? Can't it figure out when functions should be inlined to gain the performance benefit?
@@MerthanE That's a good question. To some extent, the tooling can give some hints as to whether or not a function being inlined will help. This comes in the form of lint warnings that can let you know if there will be a performance benefit or not from adding the inline modifier. Beyond that, in the realm of actually optimizing the code for us, I'm not sure. One challenge with this could be that the compiler doesn't know how a function will be used, and therefore can't necessarily make an educated decision about whether to modify the code for us. A large function, inlined once, might have minimal impact to performance and generated code size. Or, that same function might be used many times and greatly increase the amount of generated code. Because of that, perhaps that tradeoff is simply left to the developer?
@@goobar makes sense as developers know how functions will be used in the future, but at the same time... the compiler should know how functions are called while compiling so dynamically making them inline or not seems like it would be possible. But this is pretty specific, no idea how the compiler actually handles this, just thought that this might be one of the things that are possible improvements under the hood. I also don't know examples where the compiler couldn't figure it out (but a developer could) but there might be some
So would it be accurate to say that the trade off of using the inline keyword is between performance and compiled code size? Inlining your functions can increase performance as the lambdas don't need to be instantiated as objects at runtime, but the size of your inlined function and the number of places you're using it both contribute to increasing the total size of your compiled code?
Oh yeah, there are nothing was said about reified types and crossinline: the first - it’s compile-time known generic type, using in most cases as “Class” type replacement, so convenient, the second one used to pass inlined blocks to another , new created objects inside of function (for example, pass block to new Runnable instance inside function)
Google does not need Kotlin. Google needs IntelliJ for Android Studio. But the point is that both Kotlin and IntelliJ are the products of the same company...
I wrote exactly the same method in Kotlin 1.3.50 and 1.3.72. The decompiled java class is different. It uses an interface and an invoke method. Also, by using the inline keyword, there is no difference in the decompiled java class because of using that interface. Even, there is no IDE guidance to remove it
I might be able to help answer that... When we create a higher order function, each function parameter is treated as an instance of a Function class in the underlying bytecode. This means, each time we invoke the higher order function, we are instantiating a new object. If we are operating on some large collection, this could means 100s or 1000s of allocations. By inlining the function, we avoid the need for this Function class allocation. So for non-higher-order classes, this performance increase doesn't apply. And for the regular execution of the code, the compiler already does a good job optimizing so the gains are minimal.
This is nice and all, but I find myself missing how the ins and outs of the compiler work and I don't really understand some stuff about object creation. Therefore, I miss the problem inline functions are trying to solve... :|
I will start using kotlin as soon as my project is done but honestly apart from kotlin UI and soon being able to use it to create swift code I don't see the appeal reminds me more of JavaScript and I don't particularly like it
But for a more... contrived, but real? example, this one line of code (github.com/Zhuinden/livedata-combinetuple-kt/blob/master/livedata-combinetuple-kt/src/main/java/com/zhuinden/livedatacombinetuplekt/LiveDataCombineTuple.kt#L752 ) would be 16 lines of code EACH in a Java file. So I personally also really like tuple decomposition for data classes (where applicable).
It is an extension function on the SharedPreferences.Editor class that accepts no parameters and returns nothing. The implementation of this function will be passed as a parameter to the edit method.
Hey there! We'd love to lend a helping hand 😁 Have you checked out the Android Basics in Kotlin course? There are even earnable badges, so you can show off your Kotlin love 😍: goo.gle/3ekZInO
Using inline function everywhere does not improve performance, but on the contrary, decreases it because JVM inlining is better than manual inlining. Also, using inline everywhere significantly bloats the bytecode because it creates a copy of the code for each call. So please, PLEASE, do not recommend using inlines for optimizations. And it definitely should not be ever used in large functions. Performance optimizations like that should not ever be used without need and without profiler measurements.
because if the inline method receives just one parameter, which needs an instance, this must be noinline. Therefore, an instance will always be created every time the inline method is called. Check the min 3:39 :)
Kotlin crew has invented the ugly fix on what should work under the hood and continue extending the set of operators saying: "Kotlin is so idiomatic language". As an application developer I should think about Business Rules instead of: "Inline or not inline"? For me the whole picture looks like the following way: "I shoot my leg and lets make a party it is so cool" - No, guys -> its better to go to the hospital. The next step is to add C++ pointers and start allocate/deallocate objects manually saying it's so powerful and provides a lot of flexibility - joke. I've seen a lot of projects and I would say there is no consistency between them, because on trivial tasks each developer wants to demonstrate how does he know Kotlin and uses all his knowledge to make simple solution really complex and thats a real pain for those who will inherit the project and those who will join. That's a real shame. On the other hand Coroutines is a big win and thank you for that.
Sounds too complicated and time consuming while you're coding, as you need to focus on the code logic, while paying attention to these minor details becomes a distraction. Kotlin should take care of it by itself under the hood
I don’t understand people, which don’t use Kotlin in their projects in 2020: come on, guys, Kotlin is official Android development language already for more than 2 years. Also, it’s pretty more beautiful, more convenient, than Java. You just need to exit from your “comfort zone”)
Huge company with multiple teams all working on different projects. I'm fairness too, Google didn't create Kotlin, they simply decided to make it a first party language for Android after the community decided they wanted it.
thank you Mr Goobar You are really fairness but as Mobil developer it is really difficult to decided which language I should master with all this Features in every language
@@hamzahalhajji7032 Uh, both are completely different things. Kotlin is for easing Native Android Development and uses the underlying platform and components and Flutter/Dart is for multiplatform development using their own rendering engine to show the UI. The choice is not between Kotlin and Dart at all. It is between Android Platform SDK or independent cross-platform rendering engine which completely ignores prior art and reimplements and mimics the underlying system. The choice is yours. One may lag behind and even get deprecated in future (flutter), where the other is literally the Android Platform itself. If in an impossible scenario, Kotlin gets deprecated, you can de-kotlinify it and convert it back to Java. Which you'll never be able to do in Flutter/Dart. Deprecation of the Platform SDK would mean the end of Android Platform itself, so it will never happen. And if it does, then Android will not be present itself, so Flutter v Native will be irrelevant