Split non-ASCII Uri normalization out of ParseRemaining and avoid temporary allocations#122038
Merged
MihaZupan merged 5 commits intodotnet:mainfrom Jan 12, 2026
Merged
Split non-ASCII Uri normalization out of ParseRemaining and avoid temporary allocations#122038MihaZupan merged 5 commits intodotnet:mainfrom
MihaZupan merged 5 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/ncl |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the URI normalization logic to improve code maintainability and reduce memory allocations for URIs containing non-ASCII characters. The key change splits the string rebuilding logic out of ParseRemaining into a dedicated helper method ParseRemaining_RecreateNormalizedString, eliminating approximately 5 temporary string allocations per URI construction.
Key changes:
- Refactored
ParseRemainingto separate Unicode normalization logic intoParseRemaining_RecreateNormalizedString - Changed
IriHelper.EscapeUnescapeIrifrom returning a string to accepting aref ValueStringBuilderparameter, enabling allocation reuse - Simplified
UriSyntaxhelper methods by inliningIsFullMatch
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Private.Uri/tests/UnitTests/IriEscapeUnescapeTest.cs | Updated test helper to use new EscapeUnescapeIri API with ValueStringBuilder |
| src/libraries/System.Private.Uri/src/System/UriSyntax.cs | Simplified flag checking methods by inlining logic |
| src/libraries/System.Private.Uri/src/System/UriExt.cs | Removed obsolete EscapeUnescapeIri wrapper method |
| src/libraries/System.Private.Uri/src/System/Uri.cs | Split normalization logic into separate helper; updated to use new IriHelper API; minor BOM addition |
| src/libraries/System.Private.Uri/src/System/IriHelper.cs | Changed API from returning string to void with ref ValueStringBuilder parameter; removed unnecessary parentheses |
ec9f941 to
2535929
Compare
2535929 to
1623329
Compare
This was referenced Jan 5, 2026
liveans
approved these changes
Jan 12, 2026
Member
liveans
left a comment
There was a problem hiding this comment.
LGTM with a question and nits
3 tasks
This was referenced Jan 13, 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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Review without whitespace changes
When we rebuild the
_stringfor inputs with non-ASCII chars, we do so in two places:PrivateParseMinimalrecreates the string up to the path (scheme, user info, host, port)ParseRemainingrecreates the path, query and fragmentParseRemainingis currently written along the lines ofThis makes it much harder to reason about because we're juggling two offsets into two different strings, while also changing one of them.
This PR splits the rebuilding logic into a separate helper we call first that normalizes the path/query/fragment.
Since all the transformations now happen in a single place, we can also easily avoid the ~5 temporary string allocations.
We're still allocating the host separately, but that can be improved later.
Benchmark