Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Precondition to eliminate mutability check inside loop #1802

Closed
soywiz opened this issue Jul 13, 2018 · 1 comment
Closed

Precondition to eliminate mutability check inside loop #1802

soywiz opened this issue Jul 13, 2018 · 1 comment

Comments

@soywiz
Copy link
Contributor

soywiz commented Jul 13, 2018

Right now, inside a loop that mutates an array, for each iteration, there is a mutability check in each iteration. With the bound check elimination, this is the remaining additional overhead inside loops for arrays, in addition to make arrays content is 16-byte aligned, that would prevent advanced optimizations like vectorization.

So my proposal would be to use a precondition before the loop that the user can place. For example:

val array = IntArray(64)
array.ensureNotFrozen()
for (n in 0 until array.size) {
    array[n] = n
}

It is possible to remove in codegen the array set mutability check, if it can be proven that the array is not mutated somewhere else. For example if only pure functions (not sure if the IR have information about pureness) are called in all the predecessors CFG nodes before the set and after the check. The precondition could be automatically added inside the if, before the do..while loop generated for fors as an advanced optimization.

Maybe not trivial to implement, but worth trying at least after 1.0 is released.

Another option would be to add an annotation for methods like @UnsafeNotCheckMutability for making hot paths as much optimized as possible.

@sbogolepov
Copy link
Contributor

We are moving to YouTrack (#4079), so I transferred your issue there :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants