You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Elide bounds checks for partial array/span iteration #31638
However, it does not remove them when the iteration ends before the end of the array.
for(inti=0;i<span.Length-1;i++)acc+=span[i];
In a similar pattern often used when parsing data, the bounds checks are not removed when it can be proven there will be no OOB accesses. (Simple example for illustrative purposes)
@AndyAyersMS I have a quick prototype to remove bound checks for for (int i=0; i<array.Length - C; i++): EgorBo@6085b6a (allows array cloning for loops with ARR_LEN + INT_CNS conditions)
With the first 2 cases fixed by @nathan-moore I am goind to change the milestone for the third to "Future". The last case requires more changes and it probably won't be done in 5.0.
Activity
EgorBo commentedon Feb 3, 2020
#9505 partially related
BruceForstall commentedon Feb 6, 2020
@AndyAyersMS @erozenfeld
AndyAyersMS commentedon May 6, 2020
Hmm, seems like we ought to be able to handle cases like these.
Marking as 5.0 -- we should at drill in and figure out where we get confused, and see how difficult this would be to fix.
EgorBo commentedon May 8, 2020
@AndyAyersMS I have a quick prototype to remove bound checks for
for (int i=0; i<array.Length - C; i++): EgorBo@6085b6a (allows array cloning for loops withARR_LEN + INT_CNSconditions)nathan-moore commentedon May 12, 2020
I took a shot at this at #36263 (Apologies if I stepped on your toes @EgorBo, I didn't see your comment when I started on it).
It deals with the first two cases here fine. The last one is a bit trickier. We see the i - 1 and give up when we hit the check here:
runtime/src/coreclr/src/jit/rangecheck.cpp
Line 351 in 751a10e
I'd have to look at it more to see if there's an easy way of relaxing that conditional.
EgorBo commentedon May 12, 2020
You did not 🙂
sandreenko commentedon Jun 22, 2020
With the first 2 cases fixed by @nathan-moore I am goind to change the milestone for the third to "Future". The last case requires more changes and it probably won't be done in 5.0.
iSazonov commentedon Dec 1, 2022
Vote to move this to .Net 8 milestone. In real code, we often access the previous or next element of an array.
BruceForstall commentedon Dec 1, 2022
Sure, I'll put it in 8.0 for now.
EgorBo commentedon Mar 19, 2025
Seems like all code snippets elide bounds checks in Main