Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@TrayanZapryanov@EgorBo@MihaZupan@gfoidl@Trayan-Zapryanov
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues - #102917

Merged
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode
Jun 6, 2024
Merged

Optimize HttpUtility.JavaScriptStringEncode by using SearchValues#102917
MihaZupan merged 6 commits into
dotnet:mainfrom
TrayanZapryanov:optimize_HttpUtility_JavaScriptStringEncode

Conversation

@TrayanZapryanov

Copy link
Copy Markdown
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 label May 31, 2024
@dotnet-policy-servicedotnet-policy-serviceBot added the community-contribution Indicates that the PR has been added by a community member label May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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.");}

@EgorBo

Copy link
Copy Markdown
Member

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

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

I see we had similar ideas @gfoidl :)

@MihaZupanMihaZupan added this to the 9.0.0 milestone May 31, 2024
@TrayanZapryanov

Copy link
Copy Markdown
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

MihaZupan commented May 31, 2024

Copy link
Copy Markdown
Member

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

Do you see it go away if you move the fallback (everything after return 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)

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov

Copy link
Copy Markdown
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.

@MihaZupanMihaZupan left a comment

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.

Thanks.

Looks like this one hit a merge conflict after #102805

Comment threadsrc/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoder.cs Outdated
@TrayanZapryanov
TrayanZapryanovforce-pushed the optimize_HttpUtility_JavaScriptStringEncode branch from 9ae9832 to 5d498a4CompareJune 4, 2024 11:49
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

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