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 HttpUtility.JavaScriptStringEncode by using SearchValues#102917

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 HttpUtility.JavaScriptStringEncode by using SearchValues for invalid JavaScript characters.

Benchmark

[MemoryDiagnoser]publicclassHttpUtilityBenchmarks{[Params(true,false)]publicboolAddQuotes{get;set;}[Benchmark(Baseline=true)]publicstringJavaScriptStringEncode_NoEscape()=>HttpUtility.JavaScriptStringEncode("No escaping needed.",AddQuotes);[Benchmark]publicstringJavaScriptStringEncode_NoEscape_PR()=>MyHttpUtility.JavaScriptStringEncode("No escaping needed.",AddQuotes);[Benchmark]publicstringJavaScriptStringEncode_Escape()=>HttpUtility.JavaScriptStringEncode("The\t and\n will need to be escaped.",AddQuotes);[Benchmark]publicstringJavaScriptStringEncode_Escape_PR()=>MyHttpUtility.JavaScriptStringEncode("The\t and\n will need to be escaped.",AddQuotes);}

Results:

MethodAddQuotesMeanErrorStdDevRatioRatioSDGen0AllocatedAlloc Ratio
JavaScriptStringEncode_NoEscapeFalse27.485 ns0.2172 ns0.2032 ns1.000.00--NA
JavaScriptStringEncode_NoEscape_PRFalse2.558 ns0.0873 ns0.0857 ns0.090.00--NA
JavaScriptStringEncode_EscapeFalse75.264 ns0.7011 ns0.5854 ns2.740.020.0315264 BNA
JavaScriptStringEncode_Escape_PRFalse36.093 ns0.5054 ns0.3945 ns1.310.020.0315264 BNA
JavaScriptStringEncode_NoEscapeTrue35.001 ns0.4262 ns0.3778 ns1.000.000.007664 B1.00
JavaScriptStringEncode_NoEscape_PRTrue8.809 ns0.2115 ns0.3100 ns0.260.010.007664 B1.00
JavaScriptStringEncode_EscapeTrue86.737 ns1.7250 ns1.6136 ns2.480.040.0440368 B5.75
JavaScriptStringEncode_Escape_PRTrue37.486 ns0.7770 ns1.4971 ns1.100.030.0315264 B4.12

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

@EgorBot -arm64 -amd

usingSystem.Web;usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;BenchmarkRunner.Run<HttpUtilityBenchmarks>(args:args);publicclassHttpUtilityBenchmarks{[Benchmark(Baseline=true)]publicstringJavaScriptStringEncode()=>HttpUtility.JavaScriptStringEncode("No escaping needed.");[Benchmark]publicstringJavaScriptStringEncode_Encode()=>HttpUtility.JavaScriptStringEncode("The\t and\n will need to be escaped.");}
EgorBot reacted with thumbs up emoji

@EgorBo
Copy link
Member

@TrayanZapryanov please avoid using-arm64 if PR is not arch specific 🙂 I host this bot on my personal subscription

TrayanZapryanov and rzikm reacted with thumbs up emoji

@MihaZupan
Copy link
Member

I see we had similar ideas@gfoidl :)

gfoidl reacted with laugh emoji

@MihaZupanMihaZupan added this to the9.0.0 milestoneMay 31, 2024
@TrayanZapryanov
Copy link
ContributorAuthor

@gfoidl,@MihaZupan thanks for good ideas.
I've applied all of them and here are results

MethodAddQuotesMeanErrorStdDevRatioRatioSDGen0AllocatedAlloc Ratio
JavaScriptStringEncode_NoEscapeFalse24.483 ns0.4342 ns0.3625 ns1.000.00--NA
JavaScriptStringEncode_NoEscape_PRFalse4.922 ns0.0432 ns0.0383 ns0.200.00--NA
JavaScriptStringEncode_EscapeFalse78.210 ns1.2223 ns0.9543 ns3.200.070.0315264 BNA
JavaScriptStringEncode_Escape_PRFalse42.045 ns0.5002 ns0.4679 ns1.720.030.0124104 BNA
JavaScriptStringEncode_NoEscapeTrue36.507 ns0.3863 ns0.3614 ns1.000.000.007664 B1.00
JavaScriptStringEncode_NoEscape_PRTrue16.740 ns0.2302 ns0.1922 ns0.460.010.007664 B1.00
JavaScriptStringEncode_EscapeTrue86.635 ns1.6189 ns1.3518 ns2.370.040.0440368 B5.75
JavaScriptStringEncode_Escape_PRTrue43.646 ns0.8023 ns0.7504 ns1.200.030.0124104 B1.62

Allocations are much better, although there is a small regression(most probably due to ValueStringBuilder). In General PR gives good perf wins even now.

@MihaZupan
Copy link
Member

MihaZupan commentedMay 31, 2024
edited
Loading

although there is a small regression(most probably due to ValueStringBuilder)

Do you see it go away if you move the fallback (everything afterreturn addDoubleQuotes ? $"\"{value}\"" : value;) to a separate helper (to move the stackalloc out of the fast path)?

E.g.string EncodeCore(ReadOnlySpan<char> value, int i)

@TrayanZapryanov
Copy link
ContributorAuthor

@MihaZupan requested changes are applied.
I've measured your suggestion for removing if(string.IsnullorEmpty(XXX)) and looks like without this check results are slightly worst. Here are results:
Without check for null(like in the PR code at the moment):

MethodAddQuotesMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
JavaScriptStringEncode_NoEscapeFalse23.716 ns0.1315 ns0.1098 ns23.710 ns1.000.00--NA
JavaScriptStringEncode_NoEscape_PRFalse3.035 ns0.0135 ns0.0120 ns3.033 ns0.130.00--NA
JavaScriptStringEncode_EscapeFalse85.928 ns1.8087 ns5.3047 ns84.196 ns3.790.210.0315264 BNA
JavaScriptStringEncode_Escape_PRFalse46.633 ns0.7838 ns0.7331 ns46.815 ns1.970.030.0124104 BNA
JavaScriptStringEncode_NoEscapeTrue37.540 ns0.6752 ns0.5639 ns37.431 ns1.000.000.007664 B1.00
JavaScriptStringEncode_NoEscape_PRTrue15.981 ns0.3474 ns0.4000 ns15.940 ns0.420.010.007664 B1.00
JavaScriptStringEncode_EscapeTrue92.901 ns1.8431 ns5.2286 ns91.220 ns2.540.130.0440368 B5.75
JavaScriptStringEncode_Escape_PRTrue46.662 ns0.9257 ns0.8659 ns46.474 ns1.240.030.0124104 B1.62

string.IsNullOrEmpty:

MethodAddQuotesMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
JavaScriptStringEncode_NoEscapeFalse26.880 ns0.3227 ns0.2695 ns26.949 ns1.000.00--NA
JavaScriptStringEncode_NoEscape_PRFalse2.495 ns0.0395 ns0.0370 ns2.501 ns0.090.00--NA
JavaScriptStringEncode_EscapeFalse81.711 ns1.1379 ns0.9502 ns81.718 ns3.040.060.0315264 BNA
JavaScriptStringEncode_Escape_PRFalse47.066 ns0.6737 ns0.5972 ns46.978 ns1.750.020.0124104 BNA
JavaScriptStringEncode_NoEscapeTrue37.651 ns0.7910 ns0.7399 ns37.339 ns1.000.000.007664 B1.00
JavaScriptStringEncode_NoEscape_PRTrue10.082 ns0.3532 ns1.0304 ns9.684 ns0.300.030.007664 B1.00
JavaScriptStringEncode_EscapeTrue90.945 ns1.7183 ns3.9480 ns90.059 ns2.490.130.0440368 B5.75
JavaScriptStringEncode_Escape_PRTrue46.321 ns0.9600 ns2.6441 ns46.369 ns1.190.060.0124104 B1.62

I don't think this is so performance critical code, so both ways shows performance increase and it is up to you to decide which one do you prefer.

MihaZupan reacted with thumbs up emoji

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.

Thanks.

Looks like this one hit a merge conflict after#102805

@TrayanZapryanovTrayanZapryanovforce-pushed theoptimize_HttpUtility_JavaScriptStringEncode branch from9ae9832 to5d498a4CompareJune 4, 2024 11:49
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.

5 participants

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

[8]ページ先頭

©2009-2025 Movatter.jp