SlideShare a Scribd company logo
1 of 40
Download to read offline
Kotlin Bytecode Generation

and 

Runtime Performance
Dmitry Jemerov
Dmitry Jemerov, 11-13 May 2016
What is Kotlin
What is Kotlin
What is Kotlin
• Statically typed programming language for JVM, Android and the
browser
• Pragmatic, safe, concise, seamless Java interop
What is Kotlin
• Statically typed programming language for the JVM, Android and
the browser
• Pragmatic, safe, concise, seamless Java interop
Compiling a mixed project
*.java
kotlinc
*.kt
*.class
javac *.class
*.jar
Metadata
@Metadata(

mv = {1, 1, 1},

bv = {1, 0, 0},

k = 1,

d1 = {"u0000"nu0002u0018u0002nu0002u0018u0002nu0002bu0002nu0002u0010bnu0000n
u0002u0010u0002nu0002bu0005nu0002u0018u0002nu0002bu0002bu0017u0018u00002u00020u0001Bu0005¢
u0006u0002u0010u0002Jbu0010u0003u001au00020u0004Hu0007Jbu0010u0005u001au00020u0006Hu0007Jb
u0010u0007u001au00020u0004Hu0007Jbu0010bu001au00020u0004Hu0007J!u0010tu001au0002Hn"u0004b
u0000u0010n2fu0010u000bu001abu0012u0004u0012u0002Hn0fHu0002¢u0006u0002u0010r¨u0006u000e"},

d2 = {"Lorg/jetbrains/LambdaBenchmark;", "Lorg/jetbrains/SizedBenchmark;", "()V", "capturingLambda", "",
"init", "", "mutatingLambda", "noncapturingLambda", "runLambda", "T", "x", "Lkotlin/Function0;", "(Lkotlin/jvm/
functions/Function0;)Ljava/lang/Object;", "production sources for module kotlin-benchmarks"}

)

public class LambdaBenchmark extends SizedBenchmark
{ … }

Working with metadata
class A {

fun foo(): String? = null

fun bar(): String = ""

}



fun main(args: Array<String>) {

println(A::class.functions.filter {

it.returnType.isMarkedNullable

})

}
File-level functions
// Data.kt
fun foo() {

}

public final class DataKt {

public static void foo() {

}

}

@JvmName
@file:JvmName("FooUtils")
fun foo() {

}

public final class FooUtils {

public static final void foo() {

}

}

Primary constructors
class A(val x: Int) {

}

public final class A {

private final int x;



public final int getX() {

return this.x;

}



public A(int x) {

this.x = x;

}

}
Data classes
data class A(val x: Int) {

}

public final class A {
…





public String toString() {

return "A(x=" + this.x + ")";

}



public int hashCode() {

return this.x;

}



public boolean equals(Object o) {
…
}
}

Properties
class A {

var x: String? = null

}

public final class A {

@Nullable

private String x;



@Nullable

public final String getX() {

return this.x;

}



public final void setX(

@Nullable String x) {

this.x = x;

}

}
@JvmField
class A {

@JvmField var x: String? = null

}

public final class A {

@JvmField

@Nullable

public String x;

}
Not-null types
class A {

fun x(s: String) {

println(s)

}
private fun y(s: String) {

println(s)

}

}
public final class A {

public final void x(@NotNull String s) {

Intrinsics.

checkParameterIsNotNull(s, "s");

System.out.println(s);

}



private final void y(String s) {

System.out.println(s);

}

}
Parameter null checks
1 parameter
8 parameters
ns
0 2,25 4,5 6,75 9
Without @NotNull With @NotNull
Extension functions
class A(val i: Int)



fun A.foo(): Int {

return i

}



fun useFoo() {

A(1).foo()

}
public final class ExtFunKt {

public static final void foo(

@NotNull A $receiver) {
return $receiver.getI();

}

}
public static final void useFoo() {

foo(new A(1));

}
Interface methods
interface I {

fun foo(): Int {

return 42

}

}



class C : I {

}
public interface I {

int foo();



public static final class

DefaultImpls {

public static int foo(I $this) {

return 42;

}

}

}

public final class C implements I {

public void foo() {

I.DefaultImpls.foo(this);

}

}
Interface methods: Evolution
interface I {

fun foo(): Int {

return 42

}



fun bar(): Int {

return 239

}

}



// -- separate compilation ——————



class C : I {

}

public interface I {

int foo();
int bar();



public static final class

DefaultImpls { … }

}


// -- separate compilation ——————

public final class C implements I {

public void foo() {

I.DefaultImpls.foo(this);

}

}
Default Arguments
fun foo(x: Int = 42) {
println(x)

}



fun bar() {

foo()

}

public static final void foo(int x) {
System.out.println(x);

}



public static void foo$default(

int x, int mask, …) {


if ((mask & 1) != 0) {

x = 42;

}



foo(x);

}



public static final void bar() {

foo$default(0, 1, …);

}
Default Arguments
1 parameter
8 parameters
ns
0 3,75 7,5 11,25 15
Without default values With default values
@JvmOverloads
@JvmOverloads

fun foo(x: Int = 42) {

}

public static final void foo(int x)
{

}



public static void foo$default(

int x, int mask, …) { … }



public static void foo() {

foo$default(0, 1, …);

}
Lambdas: Functional types
fun <T> runLambda(x: () -> T): T =
x()

private static final
Object runLambda(Function0 x) {

return x.invoke();

}

package kotlin.jvm.functions



/** A function that takes 0 arguments. */

public interface Function0<out R> : Function<R> {

/** Invokes the function. */

public operator fun invoke(): R

}
Lambdas: Noncapturing
var value = 0



fun noncapLambda(): Int 

= runLambda { value }

final class LB$noncapLambda$1 

extends Lambda implements Function0 {



public static final LB$noncapLambda$1 

INSTANCE = new LB$noncapLambda$1();



public final int invoke() {

return LambdaBenchmarkKt.getValue();

}

}
public static int noncapLambda() {

return ((Number)runLambda(

LB$noncapLambda$1.INSTANCE)
).intValue();

}
Lambdas: Capturing
fun capturingLambda(v: Int): Int

= runLambda { v }
public static int

capturingLambda(int value) {

return ((Number)RL.runLambda(

new Function0(0) {

public final int invoke() {

return value;

}

})
).intValue();

Lambdas: Capture and mutate
fun mutatingLambda(): Int {

var x = 0

runLambda { x++ }

return x

}

public static int mutatingLambda()
{

final IntRef x = new IntRef();

x.element = 0;

RL.runLambda(new Function0(0) {

public final int invoke() {

int var1 = x.element++;

return var1;

}

});

return x.element;

}

public static final class IntRef {

public volatile int element;



@Override

public String toString() {

return String.valueOf(element);

}

}
Lambdas
Noncapturing
Capturing
Mutating
ns
0 45 90 135 180
Java Kotlin
Lambdas: inline
fun inlineLambda(x: Int): Int =

run { x }

public static int
inlineLambda(int x) {

return x;

}

/**

* Calls the specified function [block] and returns its result.

*/

public inline fun <R> run(block: () -> R): R = block()
Method References
private fun referenced() = 1



fun runReference() {

runLambda(::referenced)

}

final class MethodReferenceKt$runReference$1 

extends FunctionReference implements Function0
{



public static final MethodReferenceKt
$runReference$1 INSTANCE = new
MethodReferenceKt$runReference$1();



public final int invoke() {

return MethodReferenceKt.access
$referenced();

}



public final KDeclarationContainer 

getOwner() { … }



public final String getName() { … }



public final String getSignature() { … }

}
Loops: Range
fun rangeLoop() {

for (i in 1..10) {

println(i)

}

}

public static final void
rangeLoop() {

int i = 1;

byte var1 = 10;

if(i <= var1) {

while(true) {

System.out.println(i);

if(i == var1) {

break;

}



++i;

}

}

}
Loops: Array
fun arrayLoop(x: Array<String>) {

for (s in x) {

println(s)

}

}
public static void
arrayLoop(@NotNull String[] x) {

for(int var2 = 0; 

var2 < x.length; ++var2) {

String s = x[var2];

System.out.println(s);

}

}

Loops: List
fun listLoop(x: List<String>) {

for (s in x) {

println(s)

}

}
public static final void
listLoop(@NotNull List x) {


Iterator var2 = x.iterator();



while(var2.hasNext()) {

String s =

(String)var2.next();

System.out.println(s);

}

}
Loops
Range
Array
ArrayList
ns
0 27,5 55 82,5 110
Java Kotlin
When: Table lookup
fun tableWhen(x: Int): String =
when(x) {

0 -> "zero"

1 -> "one"

else -> "many"

}
public static String tableWhen(int x)
{
String var10000;

switch(x) {

case 0:

var10000 = "zero";

break;

case 1:

var10000 = "one";

break;

default:

var10000 = "many";

}



return var10000;

}
When: Constants
val ZERO = 0

val ONE = 1



fun constWhen(x: Int): String =
when(x) {

ZERO -> "zero"

ONE -> "one"

else -> "many"

}
public static String constWhen(

int x) {

return x == ZERO ? “zero"
: (x == ONE ? “one" : "many");

}

When: enum
enum class NumberValue {
ZERO, ONE, MANY
}



fun enumWhen(x: NumberValue):
String = when(x) {

NumberValue.ZERO -> "zero"

NumberValue.ONE -> "one"

else -> "many"

}
public static String
enumWhen(@NotNull NumberValue x) {

String var10000;

switch(WhenEnumKt$WhenMappings.
$EnumSwitchMapping$0[x.ordinal()]) {

case 1:

var10000 = "zero";

break;

case 2:

var10000 = "one";

break;

default:

var10000 = "many";

}

return var10000;

}
When
Lookup
Compare
Enum
ns
0 5,5 11 16,5 22
Java Kotlin
Summary
• Kotlin allows writing code which is easy to use from Java
• Performance in most scenarios is on par with Java
• Inline functions 👍
• Measure the performance of your code, not micro-examples
https://www.manning.com/books/kotlin-in-action
Q&A
yole@jetbrains.com
@intelliyole

More Related Content

What's hot

From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examplesPeter Lawrey
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best PracticesTheo Jungeblut
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programmingScott Wlaschin
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)Scott Wlaschin
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19José Paumard
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesVMware Tanzu
 
Java 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeJava 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeSimone Bordet
 
Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013Scott Wlaschin
 

What's hot (20)

From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
Clean code
Clean codeClean code
Clean code
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examples
 
Fetch API Talk
Fetch API TalkFetch API Talk
Fetch API Talk
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programming
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
 
Clean code
Clean codeClean code
Clean code
 
Second Level Cache in JPA Explained
Second Level Cache in JPA ExplainedSecond Level Cache in JPA Explained
Second Level Cache in JPA Explained
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
Java 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgradeJava 9/10/11 - What's new and why you should upgrade
Java 9/10/11 - What's new and why you should upgrade
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013
 

Similar to Kotlin Bytecode Generation and Runtime Performance

Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Codemotion
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Flink Forward
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Mohamed Nabil, MSc.
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
Nice to meet Kotlin
Nice to meet KotlinNice to meet Kotlin
Nice to meet KotlinJieyi Wu
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of LambdasEsther Lozano
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
Kotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functionsKotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functionsFranco Lombardo
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingHenri Tremblay
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Ontico
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already KnowKevlin Henney
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 

Similar to Kotlin Bytecode Generation and Runtime Performance (20)

Kotlin decompiled
Kotlin decompiledKotlin decompiled
Kotlin decompiled
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Nice to meet Kotlin
Nice to meet KotlinNice to meet Kotlin
Nice to meet Kotlin
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Kotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functionsKotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functions
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 

More from intelliyole

Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?intelliyole
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)intelliyole
 
From Renamer Plugin to Polyglot IDE
From Renamer Plugin to Polyglot IDEFrom Renamer Plugin to Polyglot IDE
From Renamer Plugin to Polyglot IDEintelliyole
 
How to Build Developer Tools on Top of IntelliJ Platform
How to Build Developer Tools on Top of IntelliJ PlatformHow to Build Developer Tools on Top of IntelliJ Platform
How to Build Developer Tools on Top of IntelliJ Platformintelliyole
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
IntelliJ IDEA: Life after Open Source
IntelliJ IDEA: Life after Open SourceIntelliJ IDEA: Life after Open Source
IntelliJ IDEA: Life after Open Sourceintelliyole
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
Implementing Refactorings in IntelliJ IDEA
Implementing Refactorings in IntelliJ IDEAImplementing Refactorings in IntelliJ IDEA
Implementing Refactorings in IntelliJ IDEAintelliyole
 
IntelliJ IDEA Architecture and Performance
IntelliJ IDEA Architecture and PerformanceIntelliJ IDEA Architecture and Performance
IntelliJ IDEA Architecture and Performanceintelliyole
 

More from intelliyole (9)

Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
 
From Renamer Plugin to Polyglot IDE
From Renamer Plugin to Polyglot IDEFrom Renamer Plugin to Polyglot IDE
From Renamer Plugin to Polyglot IDE
 
How to Build Developer Tools on Top of IntelliJ Platform
How to Build Developer Tools on Top of IntelliJ PlatformHow to Build Developer Tools on Top of IntelliJ Platform
How to Build Developer Tools on Top of IntelliJ Platform
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
IntelliJ IDEA: Life after Open Source
IntelliJ IDEA: Life after Open SourceIntelliJ IDEA: Life after Open Source
IntelliJ IDEA: Life after Open Source
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Implementing Refactorings in IntelliJ IDEA
Implementing Refactorings in IntelliJ IDEAImplementing Refactorings in IntelliJ IDEA
Implementing Refactorings in IntelliJ IDEA
 
IntelliJ IDEA Architecture and Performance
IntelliJ IDEA Architecture and PerformanceIntelliJ IDEA Architecture and Performance
IntelliJ IDEA Architecture and Performance
 

Recently uploaded

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 

Recently uploaded (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 

Kotlin Bytecode Generation and Runtime Performance

  • 1. Kotlin Bytecode Generation
 and 
 Runtime Performance Dmitry Jemerov Dmitry Jemerov, 11-13 May 2016
  • 4. What is Kotlin • Statically typed programming language for JVM, Android and the browser • Pragmatic, safe, concise, seamless Java interop
  • 5. What is Kotlin • Statically typed programming language for the JVM, Android and the browser • Pragmatic, safe, concise, seamless Java interop
  • 6. Compiling a mixed project *.java kotlinc *.kt *.class javac *.class *.jar
  • 7. Metadata @Metadata(
 mv = {1, 1, 1},
 bv = {1, 0, 0},
 k = 1,
 d1 = {"u0000"nu0002u0018u0002nu0002u0018u0002nu0002bu0002nu0002u0010bnu0000n u0002u0010u0002nu0002bu0005nu0002u0018u0002nu0002bu0002bu0017u0018u00002u00020u0001Bu0005¢ u0006u0002u0010u0002Jbu0010u0003u001au00020u0004Hu0007Jbu0010u0005u001au00020u0006Hu0007Jb u0010u0007u001au00020u0004Hu0007Jbu0010bu001au00020u0004Hu0007J!u0010tu001au0002Hn"u0004b u0000u0010n2fu0010u000bu001abu0012u0004u0012u0002Hn0fHu0002¢u0006u0002u0010r¨u0006u000e"},
 d2 = {"Lorg/jetbrains/LambdaBenchmark;", "Lorg/jetbrains/SizedBenchmark;", "()V", "capturingLambda", "", "init", "", "mutatingLambda", "noncapturingLambda", "runLambda", "T", "x", "Lkotlin/Function0;", "(Lkotlin/jvm/ functions/Function0;)Ljava/lang/Object;", "production sources for module kotlin-benchmarks"}
 )
 public class LambdaBenchmark extends SizedBenchmark { … }

  • 8. Working with metadata class A {
 fun foo(): String? = null
 fun bar(): String = ""
 }
 
 fun main(args: Array<String>) {
 println(A::class.functions.filter {
 it.returnType.isMarkedNullable
 })
 }
  • 9. File-level functions // Data.kt fun foo() {
 }
 public final class DataKt {
 public static void foo() {
 }
 }

  • 10. @JvmName @file:JvmName("FooUtils") fun foo() {
 }
 public final class FooUtils {
 public static final void foo() {
 }
 }

  • 11. Primary constructors class A(val x: Int) {
 }
 public final class A {
 private final int x;
 
 public final int getX() {
 return this.x;
 }
 
 public A(int x) {
 this.x = x;
 }
 }
  • 12. Data classes data class A(val x: Int) {
 }
 public final class A { …
 
 
 public String toString() {
 return "A(x=" + this.x + ")";
 }
 
 public int hashCode() {
 return this.x;
 }
 
 public boolean equals(Object o) { … } }

  • 13. Properties class A {
 var x: String? = null
 }
 public final class A {
 @Nullable
 private String x;
 
 @Nullable
 public final String getX() {
 return this.x;
 }
 
 public final void setX(
 @Nullable String x) {
 this.x = x;
 }
 }
  • 14. @JvmField class A {
 @JvmField var x: String? = null
 }
 public final class A {
 @JvmField
 @Nullable
 public String x;
 }
  • 15. Not-null types class A {
 fun x(s: String) {
 println(s)
 } private fun y(s: String) {
 println(s)
 }
 } public final class A {
 public final void x(@NotNull String s) {
 Intrinsics.
 checkParameterIsNotNull(s, "s");
 System.out.println(s);
 }
 
 private final void y(String s) {
 System.out.println(s);
 }
 }
  • 16. Parameter null checks 1 parameter 8 parameters ns 0 2,25 4,5 6,75 9 Without @NotNull With @NotNull
  • 17. Extension functions class A(val i: Int)
 
 fun A.foo(): Int {
 return i
 }
 
 fun useFoo() {
 A(1).foo()
 } public final class ExtFunKt {
 public static final void foo(
 @NotNull A $receiver) { return $receiver.getI();
 }
 } public static final void useFoo() {
 foo(new A(1));
 }
  • 18. Interface methods interface I {
 fun foo(): Int {
 return 42
 }
 }
 
 class C : I {
 } public interface I {
 int foo();
 
 public static final class
 DefaultImpls {
 public static int foo(I $this) {
 return 42;
 }
 }
 }
 public final class C implements I {
 public void foo() {
 I.DefaultImpls.foo(this);
 }
 }
  • 19. Interface methods: Evolution interface I {
 fun foo(): Int {
 return 42
 }
 
 fun bar(): Int {
 return 239
 }
 }
 
 // -- separate compilation ——————
 
 class C : I {
 }
 public interface I {
 int foo(); int bar();
 
 public static final class
 DefaultImpls { … }
 } 
 // -- separate compilation ——————
 public final class C implements I {
 public void foo() {
 I.DefaultImpls.foo(this);
 }
 }
  • 20. Default Arguments fun foo(x: Int = 42) { println(x)
 }
 
 fun bar() {
 foo()
 }
 public static final void foo(int x) { System.out.println(x);
 }
 
 public static void foo$default(
 int x, int mask, …) { 
 if ((mask & 1) != 0) {
 x = 42;
 }
 
 foo(x);
 }
 
 public static final void bar() {
 foo$default(0, 1, …);
 }
  • 21. Default Arguments 1 parameter 8 parameters ns 0 3,75 7,5 11,25 15 Without default values With default values
  • 22. @JvmOverloads @JvmOverloads
 fun foo(x: Int = 42) {
 }
 public static final void foo(int x) {
 }
 
 public static void foo$default(
 int x, int mask, …) { … }
 
 public static void foo() {
 foo$default(0, 1, …);
 }
  • 23. Lambdas: Functional types fun <T> runLambda(x: () -> T): T = x()
 private static final Object runLambda(Function0 x) {
 return x.invoke();
 }
 package kotlin.jvm.functions
 
 /** A function that takes 0 arguments. */
 public interface Function0<out R> : Function<R> {
 /** Invokes the function. */
 public operator fun invoke(): R
 }
  • 24. Lambdas: Noncapturing var value = 0
 
 fun noncapLambda(): Int 
 = runLambda { value }
 final class LB$noncapLambda$1 
 extends Lambda implements Function0 {
 
 public static final LB$noncapLambda$1 
 INSTANCE = new LB$noncapLambda$1();
 
 public final int invoke() {
 return LambdaBenchmarkKt.getValue();
 }
 } public static int noncapLambda() {
 return ((Number)runLambda(
 LB$noncapLambda$1.INSTANCE) ).intValue();
 }
  • 25. Lambdas: Capturing fun capturingLambda(v: Int): Int
 = runLambda { v } public static int
 capturingLambda(int value) {
 return ((Number)RL.runLambda(
 new Function0(0) {
 public final int invoke() {
 return value;
 }
 }) ).intValue();

  • 26. Lambdas: Capture and mutate fun mutatingLambda(): Int {
 var x = 0
 runLambda { x++ }
 return x
 }
 public static int mutatingLambda() {
 final IntRef x = new IntRef();
 x.element = 0;
 RL.runLambda(new Function0(0) {
 public final int invoke() {
 int var1 = x.element++;
 return var1;
 }
 });
 return x.element;
 }
 public static final class IntRef {
 public volatile int element;
 
 @Override
 public String toString() {
 return String.valueOf(element);
 }
 }
  • 28. Lambdas: inline fun inlineLambda(x: Int): Int =
 run { x }
 public static int inlineLambda(int x) {
 return x;
 }
 /**
 * Calls the specified function [block] and returns its result.
 */
 public inline fun <R> run(block: () -> R): R = block()
  • 29. Method References private fun referenced() = 1
 
 fun runReference() {
 runLambda(::referenced)
 }
 final class MethodReferenceKt$runReference$1 
 extends FunctionReference implements Function0 {
 
 public static final MethodReferenceKt $runReference$1 INSTANCE = new MethodReferenceKt$runReference$1();
 
 public final int invoke() {
 return MethodReferenceKt.access $referenced();
 }
 
 public final KDeclarationContainer 
 getOwner() { … }
 
 public final String getName() { … }
 
 public final String getSignature() { … }
 }
  • 30. Loops: Range fun rangeLoop() {
 for (i in 1..10) {
 println(i)
 }
 }
 public static final void rangeLoop() {
 int i = 1;
 byte var1 = 10;
 if(i <= var1) {
 while(true) {
 System.out.println(i);
 if(i == var1) {
 break;
 }
 
 ++i;
 }
 }
 }
  • 31. Loops: Array fun arrayLoop(x: Array<String>) {
 for (s in x) {
 println(s)
 }
 } public static void arrayLoop(@NotNull String[] x) {
 for(int var2 = 0; 
 var2 < x.length; ++var2) {
 String s = x[var2];
 System.out.println(s);
 }
 }

  • 32. Loops: List fun listLoop(x: List<String>) {
 for (s in x) {
 println(s)
 }
 } public static final void listLoop(@NotNull List x) { 
 Iterator var2 = x.iterator();
 
 while(var2.hasNext()) {
 String s =
 (String)var2.next();
 System.out.println(s);
 }
 }
  • 34. When: Table lookup fun tableWhen(x: Int): String = when(x) {
 0 -> "zero"
 1 -> "one"
 else -> "many"
 } public static String tableWhen(int x) { String var10000;
 switch(x) {
 case 0:
 var10000 = "zero";
 break;
 case 1:
 var10000 = "one";
 break;
 default:
 var10000 = "many";
 }
 
 return var10000;
 }
  • 35. When: Constants val ZERO = 0
 val ONE = 1
 
 fun constWhen(x: Int): String = when(x) {
 ZERO -> "zero"
 ONE -> "one"
 else -> "many"
 } public static String constWhen(
 int x) {
 return x == ZERO ? “zero" : (x == ONE ? “one" : "many");
 }

  • 36. When: enum enum class NumberValue { ZERO, ONE, MANY }
 
 fun enumWhen(x: NumberValue): String = when(x) {
 NumberValue.ZERO -> "zero"
 NumberValue.ONE -> "one"
 else -> "many"
 } public static String enumWhen(@NotNull NumberValue x) {
 String var10000;
 switch(WhenEnumKt$WhenMappings. $EnumSwitchMapping$0[x.ordinal()]) {
 case 1:
 var10000 = "zero";
 break;
 case 2:
 var10000 = "one";
 break;
 default:
 var10000 = "many";
 }
 return var10000;
 }
  • 38. Summary • Kotlin allows writing code which is easy to use from Java • Performance in most scenarios is on par with Java • Inline functions 👍 • Measure the performance of your code, not micro-examples