https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
e

Edoardo Luppi

12/05/2023, 2:53 PM
Is there a way to perform a not-null assertion (
!!
) without having the compiler generate checks? I have A LOT of them in some generated code unfortunately, and I don't want the performance penalty, especially in non-JVM platforms.
k

kevin.cianfarini

12/05/2023, 3:01 PM
On JS there’s
unsafeCast
which does what you want. On native there’s
reinterpret
but that’s restricted to pointer types.
c

CLOVIS

12/05/2023, 3:04 PM
Have you measured a performance penalty? Checking if a reference is
null
is one of the most basic computer operations
e

Edoardo Luppi

12/05/2023, 3:05 PM
Not really, but it's a parser (and lexer) code, from which I'd like to get the best possible performance outcome. I'll try to benchmark tho.
Tested the compiled result and the Kotlin compiler is actually smart enough to automatically get rid of redundant checks.
So the actual checks I end up with are minimal.
z

zsmb

12/05/2023, 3:40 PM
There's a long discussion on this here https://discuss.kotlinlang.org/t/run-time-null-checks-and-performance/2086/20 Includes the compiler flags you can set to remove (some of?) these assertions.
e

Edoardo Luppi

12/05/2023, 3:42 PM
Thanks @zsmb! Do you happen to know if those compiler arguments are applicable to non-JVM targets? To me it looks like they're really just for the JVM.
z

zsmb

12/05/2023, 3:44 PM
It does seem like they're JVM-specific, but I don't know the details of them very well, just remembered that they exist. If you do see specific performance problems on non-JVM platforms, filing a YouTrack issue on the KT project is likely the best course of action.
e

Edoardo Luppi

12/05/2023, 3:45 PM
Sounds good, no worries. I'll ask on the JavaScript channel.
Answer is there is nothing comparable in JS, for example. Will create an issue.
2 Views