我正在考虑将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选项禁用断言生成.请注意,这些选项不受支持,将来可能会被删除.

Kotlin相关问答推荐

在没有外部 map 的情况下转换列表项

等待下一个值时暂停Kotlin Coroutine

我如何测试一个可组合组件没有显示,但如果它不存在也接受?

用vararg替换列表的设计弃用警告

Kotlin 函数名后面的大括号是什么意思?

Kotlin Poet 的导入不明确

在 Kotlin 中将两个字节转换为 UIn16

如何在 Kotlin 中初始化 Short 数组?

Flow.state In() 未从其来源接收新值

使用事务时未调用 Kafka ConsumerInterceptor onCommit

具有泛型类型的 Kotlin 密封接口不会为其子类型推断约束

参考 Kotlin 中的 Java 接口静态字段

如何使 TextInputEditText 只读?

Kotlin:如何在活页夹中返回正在运行的服务实例?

@uncheckedVariance 在 Kotlin 中?

如何暂停kotlin coroutine直到收到通知

IllegalStateException:function = , count = 3, index = 3

从另一个列表创建一个列表

Android Jetpack Compose - 图像无法zoom 到框的宽度和高度

Kotlin Coroutines:等待多个线程完成