Skip to content

Commit

Permalink
Improve WebUtility.HtmlEncode / UrlEncode performance (#103737)
Browse files Browse the repository at this point in the history
* 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.cs

Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>

---------

Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
  • Loading branch information
stephentoub and MihaZupan authored Jun 21, 2024
1 parent 65ef110 commit b1bb871
Showing 1 changed file with 137 additions and 176 deletions.
Loading

0 comments on commit b1bb871

Please sign in to comment.