Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Inline functions in Kotlin

Inline functions in Kotlin

Presentation I gave in the Kotlin Conf event in Lima, 2019.

Jose Flavio Quispe Irrazábal

September 19, 2019
Tweet

More Decks by Jose Flavio Quispe Irrazábal

Other Decks in Programming

Transcript

  1. 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
  2. Anonymous classes, anonymous functions and lambda expressions • Anonymous classes

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

    reference anonymous functions (or without name). Replace the use of anonymous classes. Kotlin Everywhere
  4. 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
  5. 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
  6. 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 ; } });
  7. 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
  8. JVM Languages • Every language that runs on the JVM:

    Scala, Java, Kotlin, Jython, etc. • Compile to Java Bytecode. Kotlin Everywhere
  9. Bytecode • Instruction set of the JVM. • Then, the

    JVM is independent of any OS. Kotlin Everywhere
  10. Inline functions • Using inline keyword, we tell the compiler

    to copy functions and parameters to the call site. Kotlin Everywhere
  11. 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
  12. 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
  13. 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.
  14. 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