Kotlin'直到'有趣创造IntRange垃圾

Jon*_*oin 5 kotlin

我在until下面的循环中使用了infix fun

for (x in 0 until bodies.size) bodies[x]()
Run Code Online (Sandbox Code Playgroud)

在使用YourKit分析我的代码时,我注意到我有大量的IntRange对象(大约2k/sec). 在此输入图像描述

当我切换循环以int...int直接使用rangeTo时,它不会产生任何垃圾.

for (x in 0..bodies.size-1) bodies[x]()
Run Code Online (Sandbox Code Playgroud)

有人可以解释这两者之间的区别吗?从我所能说的Int.until只是回归this .. to

public infix fun Int.until(to: Int): IntRange {
    val to_  = (to.toLong() - 1).toInt()
    if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.")
    return this .. to_
}
Run Code Online (Sandbox Code Playgroud)

vod*_*dan 5

在当前版本v1.0.4中,编译器优化了调用rangeTodownTo函数,因为它们是for循环中最常见的.

我想他们很快就会优化until一段时间.

以下是相关的发行票:https://youtrack.jetbrains.com/issue/KT-9900.随意投票.