Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 inchars[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