I was using the until
infix fun for my loop below
for (x in 0 until bodies.size) bodies[x]()
when profiling my code with YourKit I noticed that I had a huge amount of IntRange objects (about 2k/sec).
When I switch the loop to use the int...int
rangeTo directly it does not create any garbage.
for (x in 0..bodies.size-1) bodies[x]()
Can someone please explain the difference between these two? From what I can tell Int.until
simply returns 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_
}
In the current version v1.0.4 the compiler optimises calls for rangeTo
and downTo
functions since they are the most common in for
loops.
I think they'll optimises until
away some time soon.
Here is the relevant issue ticket: https://youtrack.jetbrains.com/issue/KT-9900. Feel free to vote it up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With