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

Improve WebUtility.HtmlEncode / UrlEncode performance#103737

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

Merged
stephentoub merged 2 commits intodotnet:mainfromstephentoub:webutilperf
Jun 21, 2024

Conversation

@stephentoub
Copy link
Member

  • For HtmlEncode, vectorize IndexOfHtmlEncodingChars. Using SearchValues, we can efficiently search for the first ASCII encoding char or the first non-ASCII char, and only then fall back to a scalar loop.
  • For HtmlEncode, reduce branching by using a more efficient check to determine whether the ASCII characters need to be encoded.
  • For UrlEncode, rather than UTF8-encoding into a new byte[], %-encoding in-place in that, and then creating a string from that, we can use string.Create and just do all the encoding in that buffer.
  • For UrlEncode, use SearchValues to vectorize the search for the first non-safe char. Also move the check for ' ' to be inside the if for non-safe char.
  • For UrlEncode, use SearchValues to optimize the check for whether an individual character is part of the set (via Contains).
  • Simplify IsUrlSafeChar. Rather than multiple checks, one of which is a bitmap, just have a bitmap.
  • Remove some leading IsNullOrEmpty checks. Null/empty inputs should be rare, and they're now handled implicitly as part of the subsequent loops.
usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;usingSystem.Net;BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);[MemoryDiagnoser]publicclassTests{[Benchmark][ArgumentsSource(nameof(Inputs))]publicstringHtmlEncode(stringinput)=>WebUtility.HtmlEncode(input);[Benchmark][ArgumentsSource(nameof(Inputs))]publicstringUrlEncode(stringinput)=>WebUtility.UrlEncode(input);publicIEnumerable<object[]>Inputs()=>[["this-is-a-very-long-filename-for-an-image-that-should-not-need-encoding.jpg"],["short_name.txt"],["https://www.example.com"],["<test>hello, world</test>"],["לילה טוב"],["""         How much wood could a woodchuck chuck         If a woodchuck could chuck wood?         A woodchuck would chuck as much wood         As much wood as a woodchuck could chuck,         If a woodchuck could chuck wood.         """]];}
MethodToolchaininputMeanRatioAllocatedAlloc Ratio
HtmlEncode\main\corerun.exe<test(...)test> [25]81.576 ns1.0096 B1.00
HtmlEncode\pr\corerun.exe<test(...)test> [25]80.366 ns0.9796 B1.00
UrlEncode\main\corerun.exe<test(...)test> [25]118.515 ns1.00160 B1.00
UrlEncode\pr\corerun.exe<test(...)test> [25]103.039 ns0.8796 B0.60
HtmlEncode\main\corerun.exeHow (...)ood. [185]164.359 ns1.00-NA
HtmlEncode\pr\corerun.exeHow (...)ood. [185]10.142 ns0.06-NA
UrlEncode\main\corerun.exeHow (...)ood. [185]537.945 ns1.00664 B1.00
UrlEncode\pr\corerun.exeHow (...)ood. [185]466.832 ns0.86432 B0.65
HtmlEncode\main\corerun.exehttps(...)e.com [23]21.442 ns1.00-NA
HtmlEncode\pr\corerun.exehttps(...)e.com [23]4.732 ns0.22-NA
UrlEncode\main\corerun.exehttps(...)e.com [23]103.053 ns1.00136 B1.00
UrlEncode\pr\corerun.exehttps(...)e.com [23]76.013 ns0.7380 B0.59
HtmlEncode\main\corerun.exeshort_name.txt13.068 ns1.00-NA
HtmlEncode\pr\corerun.exeshort_name.txt4.876 ns0.38-NA
UrlEncode\main\corerun.exeshort_name.txt14.324 ns1.00-NA
UrlEncode\pr\corerun.exeshort_name.txt3.600 ns0.26-NA
HtmlEncode\main\corerun.exethis-(...)g.jpg [75]62.800 ns1.00-NA
HtmlEncode\pr\corerun.exethis-(...)g.jpg [75]6.975 ns0.11-NA
UrlEncode\main\corerun.exethis-(...)g.jpg [75]70.709 ns1.00-NA
UrlEncode\pr\corerun.exethis-(...)g.jpg [75]5.611 ns0.08-NA
HtmlEncode\main\corerun.exeלילה טוב8.436 ns1.00-NA
HtmlEncode\pr\corerun.exeלילה טוב11.427 ns1.36-NA
UrlEncode\main\corerun.exeלילה טוב100.852 ns1.00184 B1.00
UrlEncode\pr\corerun.exeלילה טוב74.544 ns0.74112 B0.61

ShreyasJejurkar, rzikm, PaulusParssinen, martincostello, MihaZupan, ilmax, GerardSmit, davepcallan, vchirikov, NielsPilgaard, and Meir017 reacted with rocket emoji
- For HtmlEncode, vectorize IndexOfHtmlEncodingChars. Using SearchValues, we can efficiently search for the first ASCII encoding char or the first non-ASCII char, and only then fall back to a scalar loop.- For HtmlEncode, reduce branching by using a more efficient check to determine whether the ASCII characters need to be encoded.- For UrlEncode, rather than UTF8-encoding into a new byte[], %-encoding in-place in that, and then creating a string from that, we can use string.Create and just do all the encoding in that buffer.- For UrlEncode, use SearchValues to vectorize the search for the first non-safe char. Also move the check for ' ' to be inside the if for non-safe char.- For UrlEncode, use SearchValues to optimize the check for whether an individual character is part of the set (via Contains).- Simplify IsUrlSafeChar. Rather than multiple checks, one of which is a bitmap, just have a bitmap.- Remove some leading IsNullOrEmpty checks. Null/empty inputs should be rare, and they're now handled implicitly as part of the subsequent loops.
@dotnet-policy-service
Copy link
Contributor

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

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.

Always love to see SearchValues ratios like these

MethodToolchaininputMeanRatioAllocatedAlloc Ratio
HtmlEncode\main\corerun.exeHow (...)ood. [185]164.359 ns1.00-NA
HtmlEncode\pr\corerun.exeHow (...)ood. [185]10.142 ns0.06-NA

rzikm and neon-sunset reacted with rocket emoji
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
@stephentoubstephentoub merged commitb1bb871 intodotnet:mainJun 21, 2024
@stephentoubstephentoub deleted the webutilperf branchJune 21, 2024 13:57
rzikm pushed a commit to rzikm/dotnet-runtime that referenced this pull requestJun 24, 2024
* Improve WebUtility.HtmlEncode / UrlEncode performance- For HtmlEncode, vectorize IndexOfHtmlEncodingChars. Using SearchValues, we can efficiently search for the first ASCII encoding char or the first non-ASCII char, and only then fall back to a scalar loop.- For HtmlEncode, reduce branching by using a more efficient check to determine whether the ASCII characters need to be encoded.- For UrlEncode, rather than UTF8-encoding into a new byte[], %-encoding in-place in that, and then creating a string from that, we can use string.Create and just do all the encoding in that buffer.- For UrlEncode, use SearchValues to vectorize the search for the first non-safe char. Also move the check for ' ' to be inside the if for non-safe char.- For UrlEncode, use SearchValues to optimize the check for whether an individual character is part of the set (via Contains).- Simplify IsUrlSafeChar. Rather than multiple checks, one of which is a bitmap, just have a bitmap.- Remove some leading IsNullOrEmpty checks. Null/empty inputs should be rare, and they're now handled implicitly as part of the subsequent loops.* Update src/libraries/System.Private.CoreLib/src/System/Net/WebUtility.csCo-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>---------Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJul 23, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@EgorBoEgorBoEgorBo left review comments

@MihaZupanMihaZupanMihaZupan approved these changes

Assignees

@stephentoubstephentoub

Projects

None yet

Milestone

9.0.0

Development

Successfully merging this pull request may close these issues.

3 participants

@stephentoub@EgorBo@MihaZupan

[8]ページ先頭

©2009-2025 Movatter.jp