backend: Don't create a Progression objects in 'for' loops
This patch optimizes the following pattern:
for (i in first..last step st) { ... }
In this case we need to create a Progression object and then call its
iterator() method causing at least 2 allocation per loop. This change
replaces such loops with the following constuction:
var inductionVar = first
checkProgressionStep(step) // check if step > 0
last = getProgressionLastElement(first, last, step)
if (first <= last) {
do {
i = inductionVar
inductionVar += step
...
} while(i != last)
}
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fun main(args: Array<String>) {
|
||||
for (i in Int.MAX_VALUE - 1 .. Int.MAX_VALUE) { print(i); print(' ') }; println()
|
||||
for (i in Int.MAX_VALUE - 1 until Int.MAX_VALUE) { print(i); print(' ') }; println()
|
||||
for (i in Int.MIN_VALUE + 1 downTo Int.MIN_VALUE) { print(i); print(' ') }; println()
|
||||
|
||||
val M = Int.MAX_VALUE / 2
|
||||
for (i in M + 4..M + 10 step M) { print(i); print(' ') }; println()
|
||||
}
|
||||
Reference in New Issue
Block a user