-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Split non-ASCII Uri normalization out of ParseRemaining and avoid temporary allocations #122038
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
Conversation
|
Tagging subscribers to this area: @dotnet/ncl |
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.
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
liveans
left a comment
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.
LGTM with a question and nits
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