Slide 1

Slide 1 text

Jose Flavio Quispe Irrazábal Software Engineer @jflavio11 Inline functions in Kotlin A first approach Android Dev Perú

Slide 2

Slide 2 text

In this presentation ● Anonymous classes, functions and lambda expressions. ● Functions in Kotlin. ● Inline functions. ● What is the Java Bytecode? ● Things to keep in mind: return statement. ● Extra

Slide 3

Slide 3 text

The beginning

Slide 4

Slide 4 text

Anonymous classes, anonymous functions and lambda expressions ● Anonymous classes → create entities from an interface without creating an implementation class. Kotlin Everywhere

Slide 5

Slide 5 text

Anonymous classes, anonymous functions and lambda expressions ● Lambdas → reference anonymous functions (or without name). Replace the use of anonymous classes. Kotlin Everywhere

Slide 6

Slide 6 text

High Order Functions Functions in Kotlin are first-class: ● They can be passed as parameters ● They can be stored in variables ● They can be returned from another high-order function Kotlin Everywhere

Slide 7

Slide 7 text

High Order Functions Kotlin Everywhere But an object is created every time you assigned to a variable or pass it as a parameter to another function

Slide 8

Slide 8 text

Simple example Kotlin Everywhere fun myFilter(list: List, funcion: (Double, Double) -> Boolean): List myFilter(teachersList) { ratingYearOne, reatingYearTwo -> (ratingYearOne + reatingYearTwo)/2 >= 7.0 } myFilter(teachersList, new Funcion() { @Override public boolean function(Double ratingYearOne, reatingYearTwo) { return (ratingYearOne + reatingYearTwo)/2 >= 7.0 ; } });

Slide 9

Slide 9 text

Inline functions

Slide 10

Slide 10 text

Inline functions ● Using inline keyword, we tell the compiler to copy functions and parameters to the call site. ● In normal functions its code is set in memory. Everytime it is tried to be invoked, parameters are added to the memory stack and then it is invoked. Kotlin Everywhere

Slide 11

Slide 11 text

Inline functions Kotlin Everywhere

Slide 12

Slide 12 text

Decompiled bytecode (without inline)

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Bytecode and JVM Languages?

Slide 15

Slide 15 text

JVM Languages ● Every language that runs on the JVM: Scala, Java, Kotlin, Jython, etc. ● Compile to Java Bytecode. Kotlin Everywhere

Slide 16

Slide 16 text

Bytecode ● Instruction set of the JVM. ● Then, the JVM is independent of any OS. Kotlin Everywhere

Slide 17

Slide 17 text

Bytecode Kotlin Everywhere

Slide 18

Slide 18 text

Decompiled bytecode (without inline)

Slide 19

Slide 19 text

Inline functions Kotlin Everywhere

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Decompiled bytecode (using inline)

Slide 22

Slide 22 text

Inline functions Kotlin Everywhere

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Inline functions ● Using inline keyword, we tell the compiler to copy functions and parameters to the call site. Kotlin Everywhere

Slide 25

Slide 25 text

Inline functions To Keep in mind ● Normal “return” statement is not allowed inside a lambda, we must use a label → local return. ● Above behavior, is similar in anonymous functions. Kotlin Everywhere

Slide 26

Slide 26 text

Log Found Bart Did we find Bart? End of findStudent

Slide 27

Slide 27 text

Inline functions To Keep in mind ● “return” returns the nearest function declaration. ● In lambdas, the enclosing function is where the nearest fun declaration is. Kotlin Everywhere

Slide 28

Slide 28 text

Inline functions return statement Kotlin Everywhere

Slide 29

Slide 29 text

return statement: decompiled bytecode (without inline) Kotlin Everywhere return ??

Slide 30

Slide 30 text

Inline functions Kotlin Everywhere Log Found Bart

Slide 31

Slide 31 text

Conclusions

Slide 32

Slide 32 text

Inline functions ● Inline functions can reduce memory overhead (and time execution). Kotlin Everywhere ● Increase resulting bytecode. ● We should avoid them in large code logic. ● TODOs (read about) : ○ no inline ○ crossinline ○ refied ● We should keep in mind of return statement.

Slide 33

Slide 33 text

Extra

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Kotlin Everywhere Tested in a Macbook Pro early 2015 - i5 - 8GB RAM 1 2 3 4 5 6 7 8 9 10 Average 8432 7354 7840 6846 7633 7139 7113 8085 9147 6873 7646.2 ms Time comparison ● Using inline keyword ● Normal lambda 1 2 3 4 5 6 7 8 9 10 Average 15799 16067 16293 16293 15606 15216 15292 15667 15695 19755 16168.3 ms

Slide 36

Slide 36 text

Use of inline functions

Slide 37

Slide 37 text

Thank You! Jose Flavio Quispe Irrazábal Software Engineer @jflavio11 Android Dev Perú