Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Optimize allocations in HttpUtility.UrlDecodeToBytes for smaller inputs.#102753

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@TrayanZapryanov
Copy link
Contributor

Optimize byte[] allocation when:

  1. input string is less than 256 chars(might change after review)
  2. maximum encoded bytes according Encoding is less than 512(might change after review)

If a "happy" path is taken, we will have allocations equals to the returned byte[].
Drawback: method will be a bit slower as we need to calculate Encoding.GetMaxByteCount(xxx)

@ghostghost added the area-System.Net labelMay 28, 2024
@dotnet-policy-servicedotnet-policy-servicebot added the community-contributionIndicates that the PR has been added by a community member labelMay 28, 2024
@dotnet-policy-service
Copy link
Contributor

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

@TrayanZapryanov
Copy link
ContributorAuthor

Benchmark:

[MemoryDiagnoser]publicclassHttpUtilityBenchmarks{[Benchmark(Baseline=true)]publicbyte[]UrlDecodeToBytes()=>HttpUtility.UrlDecodeToBytes("http://127.0.0.1:8080/app%%Dir/page.aspx?foo=b%%r");[Benchmark]publicbyte[]UrlDecodeToBytes_PR()=>MyHttpUtility.UrlDecodeToBytes("http://127.0.0.1:8080/app%%Dir/page.aspx?foo=b%%r");}

Results:

MethodMeanErrorStdDevRatioGen0AllocatedAlloc Ratio
UrlDecodeToBytes90.45 ns1.436 ns1.343 ns1.000.0191160 B1.00
UrlDecodeToBytes_PR80.87 ns0.632 ns0.560 ns0.890.009580 B0.50

Copy link
Member

@MihaZupanMihaZupan left a comment

Choose a reason for hiding this comment

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

Thank you for looking into making improvements here.
Is the existing test coverage here sufficient to catch any potential regressions?

@EgorBo
Copy link
Member

@EgorBot -intel -arm64 -profiler

usingBenchmarkDotNet.Attributes;usingSystem.Web;usingBenchmarkDotNet.Running;BenchmarkRunner.Run<HttpUtilityBenchmarks>(args:args);publicclassHttpUtilityBenchmarks{[Benchmark]publicbyte[]UrlDecodeToBytes()=>HttpUtility.UrlDecodeToBytes("http://127.0.0.1:8080/app%%Dir/page.aspx?foo=b%%r");}

@EgorBot
Copy link

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 16 logical and 8 physical cores  Job-PTRDNU : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI  Job-JRGPMR : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
MethodToolchainMeanErrorRatio
UrlDecodeToBytesMain128.22 ns1.150 ns1.00
UrlDecodeToBytesPR89.45 ns0.144 ns0.70
More data

SeeBDN_Artifacts.zip for details.

Flame graphs:Main vsPR 🔥
Hot asm:Main vsPR
Hot functions:Main vsPR

For cleanperf results, make sure you have just one[Benchmark] in your app.

@EgorBot
Copy link

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Unknown processor  Job-BUHYYP : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD  Job-KRDBSO : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
MethodToolchainMeanErrorRatio
UrlDecodeToBytesMain176.4 ns0.21 ns1.00
UrlDecodeToBytesPR170.5 ns0.15 ns0.97
More data

SeeBDN_Artifacts.zip for details.

Flame graphs:Main vsPR 🔥
Hot asm:Main vsPR
Hot functions:Main vsPR

For cleanperf results, make sure you have just one[Benchmark] in your app.


[return:NotNullIfNotNull(nameof(bytes))]
publicstaticbyte[]?UrlDecodeToBytes(byte[]?bytes)=>bytes==null?null:UrlDecodeToBytes(bytes,0,bytes.Length);
publicstaticbyte[]?UrlDecodeToBytes(byte[]?bytes)=>bytes==null?null:HttpEncoder.UrlDecode(bytes.AsSpan(0,bytes.Length));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
publicstaticbyte[]?UrlDecodeToBytes(byte[]?bytes)=>bytes==null?null:HttpEncoder.UrlDecode(bytes.AsSpan(0,bytes.Length));
publicstaticbyte[]?UrlDecodeToBytes(byte[]?bytes)=>bytes==null?null:HttpEncoder.UrlDecode(bytes.AsSpan());

For future reference (not worth restarting CI on this PR)

@MihaZupanMihaZupan added this to the9.0.0 milestoneMay 29, 2024
@MihaZupanMihaZupan merged commit3ff4051 intodotnet:mainMay 29, 2024
@TrayanZapryanovTrayanZapryanov deleted the optimize_httputility_urldecodetobytes branchMay 29, 2024 19:12
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull requestMay 30, 2024
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJun 29, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@MihaZupanMihaZupanMihaZupan approved these changes

+1 more reviewer

@gfoidlgfoidlgfoidl left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

area-System.Netcommunity-contributionIndicates that the PR has been added by a community member

Projects

None yet

Milestone

9.0.0

Development

Successfully merging this pull request may close these issues.

6 participants

@TrayanZapryanov@EgorBo@EgorBot@gfoidl@MihaZupan@Trayan-Zapryanov

[8]ページ先頭

©2009-2025 Movatter.jp