I was playing with JProfiler evaluation version, and was a bit surprised to see that after my expected CPU-heavy functions, third place “hotspot” were calls to
15.1% - 35,276 ms - 464,945,919 hot spot inv. kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull
That is… way bigger than I thought it would be. Granted, I’ve got a tight loop that is checking the distance^2 between 2D points over and over, and I haven’t done any sort of sane distance-bucketing to speed it up yet, but still, 15% seemed like a lot.
Cost of doing business? Is this something that could be optimized out? Is there some annotation I could add that indicates “Check if this is null at the beginning for the first few thousand calls, then relax a bit and don’t check every single time.”
As far as I know kotlin adds a call to Intrinsics.checkParameterIsNotNull for each not null parameter of a public functions. Still if you take a look at the kotlin source this function just performs a simple null check.
There are a few ways you could try to reduce calls to this. I don’t know of any internal annotation which would allow you to skip this call but you can change your function.
One way would be to make the critical functions private. That way the compiler knows that it does not have to generate the checks. Not sure if this is possible in your case. You might also try inlining some functions as the intrinsic checks are not inlined (at least not in my simple test).
There is a compiler option to disable the generation of these checks. More importantly though, you should be very careful with false measurements. In particular the expectation is that this will mostly be optimized out by hotspot. Try to have a version with and a version without and compare their actual performance (without distorting it through a profiler). Similarly what kind of profiling are you using?