![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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/1.jpg)
![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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/13.jpg)
{
“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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/31.jpg)
{
“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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/38.jpg)
🤔
![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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/39.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/40.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/41.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022042300/5eca82db6fb10865241b8743/html5/thumbnails/42.jpg)
Questions?