-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Remove duplicate range check in VSB.Append(char) #82264
Conversation
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsDiffs from CoreLib
Example improvement: https://www.diffchecker.com/m1G2G2z2
|
Span<char> chars = _chars; | ||
if ((uint)pos < (uint)chars.Length) | ||
{ | ||
_chars[pos] = c; | ||
chars[pos] = c; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both methods are indexing on a Span<char>
, correct? Why does this new method avoid a range check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If both the index and span are locals, and you index into those same locals, the JIT avoids emitting an extra range check and helper for the throw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I might have misunderstood. My original assumption was that the if ((uint)pos < (uint)chars.Length)
allows the JIT to skip the range check in chars[pos] = c;
, is that not the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that is the case after this change.
But that optimization only works on locals. Before, we were reading the field twice (once in the length check and once in the assignment), and by the time we got to the assignment, the JIT couldn't tell that it already checked the length, so it emitted another one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, that makes sense, thanks for the explanation.
Out of curiosity, do you know what prevents the JIT from optimizing the original case? Is it the potential for the global _chars
to be accessed by another thread after the if statement? Or is optimizing globals just out of scope for the JIT for some simpler reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a general case where you have a field on a struct/class, a different thread could swap out the value from under you between the two reads, so you can't make the assumption that avoiding the second length check is safe.
In this case, this is a field on a ref struct, so it may be a valid assumption? I'll leave that up to smarter people like @EgorBo though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's #72004
Diffs from CoreLib
@MihuBot
Example improvement: https://www.diffchecker.com/m1G2G2z2