It seems that for loops over an `IntRange` aren't ...
# compiler
y
It seems that for loops over an
IntRange
aren't optimized in JS to be just classical for-loops, instead they use an iterator and everything. Is there a reason why that optimization doesn't exist for JS but only for JVM?
j
The optimization broke when
..<
was introduced
Here's a similar bug about it: https://youtrack.jetbrains.com/issue/KT-62652
y
What I'm observing is even weirder because the iterator actually gets created in the generated JS. This is happening seemingly because there's a
suspend
call in the loop. Seems like probably some optimization kicks in in your example that doesn't in mine
e
Would be nice to have those little optimizations in JS... See also https://youtrack.jetbrains.com/issue/KT-22342 https://youtrack.jetbrains.com/issue/KT-65831
j
The range loop elimination already exists, it just no longer matches the IR
e
Oh ok! Well in that case, would be nice to have it working again ahaha
p
Actually, I think it's a bit more compilcated. You can try reading it here. https://youtrack.jetbrains.com/issue/KT-67695/ForLoopsLowering-fails-to-handle-a-loop-over-an-imprecise-typed-iterable Long story short, it never worked in jvm inside inline functions (i.e. if it's inline function over iterable, but you passes range to it). But by accident it worked on native and js. There was a bug in IR inlining, which causes incorrect work in some corner cases, making unchecked casts throwing exceptions more often than they should. The bug was fixed in 1.9.20, which also make this work same as in jvm, i.e. erasing type to iterable, as it should be by inline function with non-reified type semantics. In 2.0.20 there are some fixes, which should reenable it back for native/js in simple cases. Upto this problem with inlines, code of for loops optimization is shared between backends, so probably everything, which works for jvm, should also work for native/js. Note: I didn't check anything on specific examples, all of above are just some intuition, so it can be unrelated problem in fact.
✔️ 1