Kotlin 'before' fun IntRange garbage creation

I used the until lesson for my loop below

 for (x in 0 until bodies.size) bodies[x]() 

when profiling my code with MyKit, I noticed that I had a huge number of IntRange objects (about 2 fps). enter image description here

When I switch the loop to use int...int rangeTo, it does not create any garbage.

 for (x in 0..bodies.size-1) bodies[x]() 

Can someone explain the difference between the two? From what I can say Int.until just 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_ } 
+5
source share
1 answer

In the current version v1.0.4, the compiler optimizes rangeTo and downTo function calls, as they are the most common in for loops.

I think they will optimize until soon.

Here is the relevant question: https://youtrack.jetbrains.com/issue/KT-9900 . Feel free to vote.

+5
source

All Articles