我正在考虑将Kotlin作为一个模拟框架,并注意到编译器在对每个可以从java访问的方法进行静态checkParametersNotnull调用之后交织在一起.通过访问(和转储?)判断方法参数是否为空每次运行此方法时调用堆栈.现在,在一个典型的模拟框架中,运行次数可能会达到数百万次,我想知道这样一个特性对性能的影响.
如果有关闭它的选项?
我正在考虑将Kotlin作为一个模拟框架,并注意到编译器在对每个可以从java访问的方法进行静态checkParametersNotnull调用之后交织在一起.通过访问(和转储?)判断方法参数是否为空每次运行此方法时调用堆栈.现在,在一个典型的模拟框架中,运行次数可能会达到数百万次,我想知道这样一个特性对性能的影响.
如果有关闭它的选项?
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.
可以使用命令行编译器的-Xno-param-assertions
和-Xno-call-assertions
选项禁用断言生成.请注意,这些选项不受支持,将来可能会被删除.