Is there a way to perform a not-null assertion (`!...
# multiplatform
e
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
On JS there’s
unsafeCast
which does what you want. On native there’s
reinterpret
but that’s restricted to pointer types.
c
Have you measured a performance penalty? Checking if a reference is
null
is one of the most basic computer operations
e
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
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
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
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
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.