RFR: 8314774: Optimize URLEncoder [v12]

Claes Redestad redestad at openjdk.org
Tue Sep 19 09:29:54 UTC 2023


On Mon, 18 Sep 2023 22:39:13 GMT, Glavo <duke at openjdk.org> wrote:

>> I mainly made these optimizations:
>> 
>> * Avoid allocating `StringBuilder` when there are no characters in the URL that need to be encoded;
>> * ~Implement a fast path for UTF-8.~ (Has been removed from this PR)
>> 
>> In addition to improving performance, these optimizations also reduce temporary objects:
>> 
>> * It no longer allocates any object when there are no characters in the URL that need to be encoded;
>> * The initial size of StringBuilder is larger to avoid expansion as much as possible;
>> * ~For UTF-8, the temporary `CharArrayWriter`, strings and byte arrays are no longer needed.~ (Has been removed from this PR)
>> 
>> I also updated the tests to add more test cases.
>
> Glavo has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Update encodeByte

src/java.base/share/classes/java/net/URLEncoder.java line 233:

> 231:         for (i = 0; i < s.length(); i++) {
> 232:             char c = s.charAt(i);
> 233:             if (c >= 128 || !DONT_NEED_ENCODING.test(c) || c == ' ') {

Suggestion:

            if (!DONT_NEED_ENCODING.test(c) || c == ' ') {


I missed this, which is redundant now. Removing this check seems beneficial for mixed or unchanged (linux-x64 workstation):

Name                           (unchanged) Cnt  Base   Error   Test   Error  Unit   Diff%
URLEncodeDecode.testEncodeUTF8           0  15 3.575 ± 0.131  3.566 ± 0.158 ms/op    0.2% (p = 0.869 )
URLEncodeDecode.testEncodeUTF8          75  15 1.453 ± 0.051  1.391 ± 0.007 ms/op    4.3% (p = 0.000*)
URLEncodeDecode.testEncodeUTF8         100  15 0.621 ± 0.000  0.542 ± 0.000 ms/op   12.8% (p = 0.000*)
  * = significant

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15354#discussion_r1329836191


More information about the net-dev mailing list