Skip to content

Make Uri non-ASCII hex unescaping a bit faster#131387

Open
MihaZupan wants to merge 2 commits into
dotnet:mainfrom
MihaZupan:uri-decodeHexHelper
Open

Make Uri non-ASCII hex unescaping a bit faster#131387
MihaZupan wants to merge 2 commits into
dotnet:mainfrom
MihaZupan:uri-decodeHexHelper

Conversation

@MihaZupan

@MihaZupan MihaZupan commented Jul 26, 2026

Copy link
Copy Markdown
Member

Avoid some bounds checks and use a table lookup instead of manual hex calculations.
Closes #121413, followup to #121016

public class UriTest
{
    private char[] _buffer = new char[1024];

    [Benchmark]
    [Arguments("%E4%BD%A0%E5%A5%BD")]
    [Arguments("%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD")]
    public bool TryUnescapeDataString(string input) => Uri.TryUnescapeDataString(input, _buffer, out _);
}
Method Toolchain input Mean Error Ratio
TryUnescapeDataString \main\corerun.exe %E4%BD%A0%E5%A5%BD 43.34 ns 0.454 ns 1.00
TryUnescapeDataString \pr\corerun.exe %E4%BD%A0%E5%A5%BD 40.43 ns 0.364 ns 0.93
TryUnescapeDataString \main\corerun.exe %E4%B(...)A5%BD [72] 103.86 ns 0.151 ns 1.00
TryUnescapeDataString \pr\corerun.exe %E4%B(...)A5%BD [72] 97.55 ns 0.203 ns 0.94

@MihaZupan MihaZupan added this to the 11.0.0 milestone Jul 26, 2026
@MihaZupan MihaZupan self-assigned this Jul 26, 2026
Copilot AI review requested due to automatic review settings July 26, 2026 10:58
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@MihaZupan
MihaZupan requested a review from EgorBo July 26, 2026 10:58
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates System.Private.Uri’s percent-unescape helper to reduce overhead when decoding non-ASCII UTF-8 bytes from %xx sequences, primarily by switching to HexConverter table lookups and tightening bounds checks in the hot loop.

Changes:

  • Replaces manual hex-nibble decoding with HexConverter.FromChar lookups for %xx parsing.
  • Adjusts the bounds check around reading % + 2 hex chars to a form intended to help the JIT elide bounds checks.
  • Adds a guard intended to prevent invalid hex from slipping past the ASCII-range check (but the current guard is insufficient and introduces a correctness bug; see review comment).

Copilot AI review requested due to automatic review settings July 26, 2026 11:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

int i = totalCharsConsumed + (bytesLeftInBuffer * 3);

ReadByteFromInput:
if ((uint)(input.Length - i) <= 2 || input[i] != '%')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that a jit issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf] Linux/arm64: 1 Regression on 10/28/2025 10:01:12 PM +00:00

3 participants