Performance: Intrinsics.checkNotNull called for every parameter

As far as I can tell, every non-nullable reference-type parameter of every non-private method is null-checked at run time (with a call to the static method kotlin.jvm.internal.Intrinsics.checkNotNull). In particular, the two-parameter method is called. I’m not at the computer right now, and I’m going on memory, so please correct me if I’m wrong.

If I’m right, does anyone know whether this could entail performance problems on Android? The following document, for example, is quite strict about what Android programmers should and shouldn’t do. From reading this, I get the impression that method calls might not be so tiny on Android.

https://developer.android.com/training/articles/perf-tips.html

2 Likes

Check the meaning of “Intrinsic”: Intrinsic function - Wikipedia

It’s handed specially by the compiler (basically, inlined), so if I’m not mistaken it should just result in a null check.

On the Oracle JVM, the JIT will further optimize this check away when I can determine it isn’t needed. Android doesn’t use a VM, but I suppose this means the compiler performs much more extensive optimizations, so it might be able to optimize that in certain cases as well.

As always, only worry about performance if it’s measurable (although since this is spread accross all methods, it might be hard to do a direct comparison).

if you’re worried about performance, just proguard the intrinsics

1 Like