-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loop condition i != T.Length
bounds check not eliminated
#84697
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch, @kunalspathak Issue Detailsnull
|
i != T.Length
bounds check not eliminated
cc @BruceForstall if this loop shape can be recognized as normal, monotonically increased one |
Also missed in the backward iteration case, I think there there is an existing issue though. public static int Array_Backward_GE(Byte[] src)
{
int sum = 0;
for (int i = src.Length - 1; i >= 0; i--)
sum += src[i];
return sum;
} |
This could be a common pattern in cases where people have copied things from c |
Found the existing issue: #9505. |
Backward iteration case for public static int Array_Backward_NE(Byte[] src)
{
int sum = 0;
int i = src.Length;
while (i != 0)
{
sum += src[--i];
}
return sum;
} |
This is not a particularly common pattern, but it is a missed optimization all the same.
Consider:
A bounds check is eliminated for the second method but not the first.
Applies to
T[]
,string
,Span<T>
,ReadOnlySpan<T>
, etc.https://csharp.godbolt.org/z/bKzrE9Wjq
The text was updated successfully, but these errors were encountered: