Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin uses runtime assertions for null checking - Performance overhead?

I'm considering Kotlin for a simulation framework and noticed that the compiler interweaves static checkParameterIsNotNull calls after for each method which can be accessed from java. This checks wether the method parameter is not null by accessing (and dumping?) the call stack every time this method is run. Now that in a typical simulation framework the count of runs might be in the millions, I'm wondering about the performance impact of such a feature.

If there an option to turn it off?

like image 208
Jack Shade Avatar asked Jul 30 '15 11:07

Jack Shade


1 Answers

First of all, the call stack is not accessed and dumped every time the method is run. During normal execution, only a single null check is performed, which does not have any noticeable overhead. The call stack is accessed only when the parameter is null and an exception needs to be thrown.

The assertions are not generated for private methods (because they cannot be called from Java code, and the Kotlin compiler ensures that the Kotlin code calling the method does not pass any null values). Therefore, the best way to avoid the overhead entirely is to write the inner loop of your simulation code using private methods.

You can disable assertion generation using the -Xno-param-assertions and -Xno-call-assertions options for the command line compiler. Note that those options are unsupported and could be removed in the future.

like image 77
yole Avatar answered Oct 04 '22 09:10

yole