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

Efficient Kotlin

GDG SPb
June 15, 2017

Efficient Kotlin

Доклад про языковые конструкции и методы stdlib'ы и их оверхед на рантайм. Предлагаются советы о том, что нужно делать, чтобы минимизировать этот самый оверхед.

Автор: Михаил Горюнов

Android-разработчик, иногда балуется back-endом.

GDG SPb

June 15, 2017
Tweet

More Decks by GDG SPb

Other Decks in Technology

Transcript

  1. Efficient Kotlin-JVM Comparisons, precautions and gotchas Disclaimer: you may have

    different performance with the same code constructs. All measurements were performed with Kotlin 1.1.3 on 64-bit HotSpot (build 25.131-b11) and on Android 7.1.1
  2. Flatten, filter and sort a list: Java 8 Truly functional:

    — concurrent processing — real lambdas — lambda creation overhead — virtual (interface) method invocation overhead fastest on big collections and with complex processing — stream creation overhead
  3. Flatten, filter and sort a list: Kotlin — cute &

    concise — imperative bytecode: no real lambdas and virtual calls — no automatic concurrency, single-thread processing fastest on small collections (hundreds of elements)
  4. Create a new map malloc(24) malloc(24) malloc(24) malloc(24) ArrayMap See

    also: — slow & compact SimpleArrayMap — SparseArray, SparseIntArray, SparseLongArray, LongSparseArray
  5. Iterators Iterator is a common contract for all Iterables, i.

    e. Lists, Sets etc. ...but for ArrayList, the most common collection, positional access is faster.
  6. SAM conversions – lambda is represented by a singleton implementing

    Runnable – <clinit>(), <init>(), run() methods are declared
  7. SAM conversions: crossinline – class representing lambda implements Runnable –

    gets instantiated every time – <init>(), run() methods are declared
  8. SAM conversions: noinline – class representing lambda is a singleton

    implementing Function0 – <clinit>, <init>, void invoke(), synthetic bridge Object invoke() methods are declared – Function0 is not Runnable, an adapter/proxy gets instantiated every time – <init>(Function0) and run() methods are declared
  9. Data class methods count per class: <init>(primary ctor args) <init>(no-arg)

    <init>(primary ctor params, bitmask, DefaultConstructorMarker ) toString, hashCode, equals copy & copy$default per property: getter & componentN setter if var
  10. Data class methods count per class: <init>(primary ctor args) <init>(no-arg)

    <init>(primary ctor params, bitmask, DefaultConstructorMarker ) toString, hashCode, equals copy & copy$default per property: getter & componentN setter if var
  11. See also • Anko, a library for Android development in

    Kotlin: github.com/Kotlin/anko • Enum problem (ru): javanese.online/статьи/enum_недоработка/ • enumMapOf(), enumSetOf() Gist: goo.gl/cS6tfR • arrayMapOf(), sparseArrayOf(), arraySetOf()… no library yet! • Set<Long> replacement if you ever need it: github.com/Miha-x64/Blitz/ • Exploring Java’s Hidden Costs by Jake Wharton jakewharton.com/exploring-java-hidden-costs/ javanese.online/статьи/exploring_java_hidden_costs/ • Eliminating Code Overhead by Jake Wharton jakewharton.com/eliminating-code-overhead/
  12. Thanks to • JetBrains, Oracle, Google • GDG • Fira

    Code • LibreOffice • github.com/amaembo/screenshoter