Currently the JITter is smart enough to elide bounds checks when forward-iterating over arrays or spans, as in the below sample.
byte[] array;
for (int i = 0; i < array.Length; i++) {
// consume array[i]
}
It would help certain tight loops if the JITter could also recognize the reverse-iteration pattern and elide the bounds checks in that scenario as well.
byte[] array;
for (int i = array.Length - 1; i >= 0 /* or any non-negative const */; i--) {
// consume array[i]
}
This optimization would save a cmp and jae instruction in these tight loops, and in certain cases it would also allow omitting the stack frame setup that normally accompanies methods that might throw exceptions.
category:cq
theme:bounds-checks
skill-level:expert
cost:medium
impact:small
Currently the JITter is smart enough to elide bounds checks when forward-iterating over arrays or spans, as in the below sample.
It would help certain tight loops if the JITter could also recognize the reverse-iteration pattern and elide the bounds checks in that scenario as well.
This optimization would save a
cmpandjaeinstruction in these tight loops, and in certain cases it would also allow omitting the stack frame setup that normally accompanies methods that might throw exceptions.category:cq
theme:bounds-checks
skill-level:expert
cost:medium
impact:small