https://kotlinlang.org logo
Title
l

Lulu

11/20/2019, 5:36 AM
Why the std-lib doesn't have a map implementation written in Kotlin, and instead uses platform-specific ones with expect/actual? Is performance the reason?
b

Burkhard

11/20/2019, 6:14 AM
I guess it has a historic reason. On kotlin jvm only there is no reason to reinvent the wheel, so why not use the java stdlib classes when possible. I’m not sure what implementation backs kotlin js map, but kotlin native uses a custom map implementation. It’s here: https://github.com/JetBrains/kotlin-native/blob/master/runtime/src/main/kotlin/kotlin/collections/HashMap.kt
l

Lulu

11/20/2019, 6:26 AM
I get that, but customizing maps would be nice so that could be one reason to have a common implementation
e

E.Kisaragi

11/20/2019, 7:52 AM
I think for native is ok since Java does jit-compile, so it avoids split impls and wont be jit-compiled. also js has already Map (since 2015, but most browser - including IE11 partially - support it)
e

eekboom

11/20/2019, 8:39 AM
If I am not mistaken Kotlin has to use the Java implementation for kotlin jvm for compatibility reason with existing Java code.
👍 3
m

mikehearn

11/20/2019, 9:21 AM
It's a performance optimisation. Java code tends to be more performance sensitive than JavaScript and the performance characteristics of the JVM are better known than V8. On the JVM calling a map function with an argument will often cause allocations, and Stream.map() goes megamorphic quite fast which means the indirections are slow. Historically C2 wasn't that great at fixing this situation (Graal is much better), so Kotlin has this inlined function trick. With JS I guess it's just assumed V8 will figure it out and if it doesn't, oh well, no big deal. If you care a lot about performance you aren't using browsers or NodeJS anyway <shrug>
j

Joffrey

11/20/2019, 1:16 PM
@mikehearn I think the OP was talking about the
Map
interface, and e.g. Java's
HashMap
implementation. Not the
map()
function on collections.