Remove some branches in OptimizedInboxTextEncoder.GetIndexOfFirstCharToEncode#130378
Merged
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-encoding |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR simplifies OptimizedInboxTextEncoder.GetIndexOfFirstCharToEncode(ReadOnlySpan<char>) by removing a redundant range-check / secondary “remaining” span and instead slicing data in-place once idx is proven in-range, aiming to improve generated code quality without changing behavior.
Changes:
- Replace the
(uint)idx < (uint)data.Lengthguard +remainingspan with in-placedata = data.Slice(idx)and direct iteration overdata. - Keep the 8x-unrolled scan and fall back to a
foreachfor the tail. - Adjust the return flow to
return -1on full consumption andreturn idxon first disallowed char.
EgorBo
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to review feedback on #129625 (comment).
Since
idxis always in range (either0when the SIMD search is skipped, ora value already validated against
data.Length), the(uint)idx < (uint)data.Lengthguard and the separate
remainingspan are unnecessary. We now slicedatainplace inside the proven-in-range branch and run the loops directly over it.
No behavior change. Codegen for the method improves:
r14)The redundant range check is gone and the extra length register is no longer
needed. Hot-loop bounds checks remain eliminated, and the
data.Slice(idx)compiles to plain pointer arithmetic with no
ThrowArgumentOutOfRange.Validated:
System.Text.Encodings.Web.Tests(610 tests) pass.Note
This PR was authored with assistance from GitHub Copilot.