KMM compiles the code to bytecode for Android and ...
# multiplatform
r
KMM compiles the code to bytecode for Android and native for iOS, and the KMM declares that there is not much performance loss. But In my situation, the performance is not on the same level compared to the native platform. I wrote a multiplatform calculation library using kotlin. The library contains high precision calculations and the kotlin standard library doesn't have corresponding tools. So, I use platform-APIs to achieve that. In js, I use bignumber.js; In iOS I use NSDecimalNumner in Foundation. But after a performance test on JS and iOS platforms, the former has 4 to 6 times lower performance, the latter has 7 to 8 times lower performance. Is there anyone who can tell me why is the performance degraded so much and how can I solve it?
a
How did you run your benchmarks?? Also, how are you comparing js and ios?
r
How did you run your benchmarks??
Build my KMP library for js and iOS. Write two calculation functions, one uses the native object(bignumber.js in JS, NSDecimalNumber in iOS), and another one uses my library. Then run them on their respective platforms.
Also, how are you comparing js and ios?
Not compare JS and iOS. The first picture shows the result on JS, using bignumber.js directly vs using my KMP library. The second picture shows the result on iOS, using NSDecimal directly vs using my KMP library.
k
In my experience the bridging between Kotlin and native ObjC objects is quite expensive, it’s usually better to avoid crossing the Kotlin-ObjC boundary when performance is key.
a
Re both iOS and JS: do you benchmark release builds? Re JS: do you use the IR mode? Re iOS: you might be hitting https://youtrack.jetbrains.com/issue/KT-39160/Slow-allocations-in-KotlinNative-LinuxX64-compared-to-JVM
You can also try the latest Kotlin 1.7.0 beta version.
r
Yes, on the release builds.
Yes, use IR mode
a
You can try Kotlin 1.7.0 beta. And perhaps file tickets with your benchmarks on YouTrack.
r
Ok
a
For iOS it is also worth trying the new memory model. For JS just in case - the
is
and
as
operators may be slow. There is
unsafeCast
for this.
r
Thanks
You can try Kotlin 1.7.0 beta. And perhaps file tickets with your benchmarks on YouTrack.
You mean I create a new ticket?
In my experience the bridging between Kotlin and native ObjC objects is quite expensive, it’s usually better to avoid crossing the Kotlin-ObjC boundary when performance is key.
Is there any best practice for that?
a
r
OK
Should I be concerned about the warning?
Copy code
Mimalloc allocator isn't supported on target ios_simulator_arm64. Used standard mode
a
Looks like
ios_simulator_arm64
mapping is missing - https://github.com/JetBrains/kotlin/blob/f9226841698132df5fe2644873f7984f308885ef/[…]lin/org/jetbrains/kotlin/konan/target/KonanTargetExtenstions.kt You can try another target (e.g. X64) and check if it makes any difference. And perhaps file a ticket in the repository?
r
My macOS is not Apple M1 Chip, is it relevant for this problem?
a
Should be fine I guess, unless I'm missing something