kotlin performance readability · my kotlin journey about 9 months long rushed project, looking for...

42

Upload: others

Post on 23-May-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance
Page 2: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

From Sweden, Rock climber, Dev on the JSW integrations team

ABOUT ME

Page 3: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Page 4: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

C H R I S T I A N R O L F | S E N I O R D E V E L O P E R | @ C C R O L F

The Price of Readability

Page 5: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Performant code is better

Readable code is better

READABILITY PERFORMANCE

Page 6: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Testability

PerformanceReadability

Page 7: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

IS KOTLIN INTERESTING OR IS IT AWESOME?

Page 8: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Source (https://www.tofugu.com/japan/chindogu-japanese-inventions/)

Page 9: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Source (https://commons.wikimedia.org/wiki/File:Too_Busy_To_Improve_-_Performance_Management_-_Square_Wheels.png)

Page 10: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

The performance problem

Page 11: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

How many people know GraphQL?

Page 12: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

REST GRAPHQL

By mik Krakow - Pracownie 2012 - etiudy teatralne (21.04.2012), CC BY 2.0, https://commons.wikimedia.org/w/index.php?curid=31816249 By Erich Ferdinand from germany - (not my) toolbox, CC BY 2.0, https://commons.wikimedia.org/w/index.php?curid=34359541

Page 13: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

{

“foo” : 1,

“bar” : null,

“baz” : -1

}

Trivial problem: Find min

Page 14: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

fun finMin(json: Map<String, Int?>) =

json.filter { it.value != null }

.minWith(comparingInt{ it.value!! })!!

.value!!

Page 15: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

int findMin(Map<String, Integer> json) {

return json.values().stream()

.filter(Objects::nonNull)

.min(comparingInt(it -> it))

.get();

}

Page 16: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

780 ± 170 ops/ms

320 ± 8 ops/ms

KOTLIN JAVA

Page 17: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

I guess Jira’s written in Kotlin then…

Page 18: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Kotlin collection operators are eager, streams are lazy

PROBLEM

Page 19: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

fun finMin(json: Map<String, Int?>) =

json.filter { it.value != null }

.minWith(comparingInt{ it.value!! })!!

.value!!

Page 20: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

fun finMin(json: Map<String, Int?>) =

json.values

.minWith(comparingInt {

it ?: Int.MAX_VALUE

})!!

Page 21: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

780 ± 170 ops/ms

1830 ± 91 ops/ms

SMART KOTLIN DUMB JAVA

Page 22: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

int findMin(Map<String, Integer> json) {

return json.values().stream()

.min(comparingInt(it ->

it == null ? MAX_VALUE: it

))

.get();

}

Page 23: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

1040 ± 100 ops/ms

1830 ± 91 ops/ms

SMART KOTLIN SMART JAVA

Page 24: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Performance doesn’t matter until it’s the only thing that matters

JED WESLEY-SMITH

Page 25: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

When you care, care a lot!

Page 26: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

fun finMin(json: Map<String, Int?>) =

var min: Int = Int.MAX_VALUE

for (it in json.values) {

it?.let { min = min(min, it) }

}

return min

Page 27: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

int findMin(Map<String, Integer> json) {

int min = Integer.MAX_VALUE;

for (Integer it : json.values()) {

min = it == null ? min : min(min, it);

}

return min;

}

Page 28: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

2740 ± 140 ops/ms

2790 ± 210 ops/ms

OPTIMIZED KOTLIN OPTIMIZED JAVA

Page 29: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

But the test-case is just a loop!

EVERYONE, HERE, NOW

Page 30: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

What about large, nested data structures?

Page 31: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

{

“foo” : [

{

“a” : 1,

“b” : null

}

]

“bar”: [ { “c” : null } , { “d” : -1 } ]

}

GraphQL problem: find min

Page 32: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

fun findMin(json:

Map<String, List<Map<String, Int?>>>) =

return json

.flatMap { it.value }

.flatMap { it.values }

.filter { it != null }

.minWith(comparingInt { it!! })!!

}

Page 33: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

int findMin(Map<String,

List<Map<String, Integer>>> json){

return json.values().stream()

.flatMap(Collection::stream)

.flatMap(it ->

it.values().stream())

.filter(Objects::nonNull)

.min(comparingInt(it -> it))

.get();

}

Page 34: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

0.74 ± 0.05 ops/ms

0.68 ± 0.08 ops/ms

DUMB KOTLIN DUMB JAVA

Page 35: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

9xperformance improvement we got before from optimization

Page 36: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

fun findMin(json:

Map<String, List<Map<String, Int?>>>) =

var min: Int = Int.MAX_VALUE

for (v in json.values) {

for (value in v) {

for (it in value.values) {

it?.let { min = min(min, it) }

}

}

}

return min

}

Page 37: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

1.0 ± 0.22 ops/ms

0.94 ± 0.24 ops/ms

OPTIMIZED KOTLIN OPTIMIZED JAVA

Page 38: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

🤔

Page 39: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

WHEN YOU CAN’T GET SPEED OPTIMIZE FOR READABILITY

Page 40: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

WHEN YOU CAN GET SPEED AVOID CHAINING EAGER OPERATIONS

Page 41: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

C H R I S T I A N R O L F | S E N I O R D E V E L O P E R | @ C C R O L F

Keep Calm and Kotlin on

Page 42: kotlin performance readability · My Kotlin journey About 9 months long Rushed project, looking for speed Saw Kotlin’s collections API Didn’t stop to look at the performance

Questions?