RFR: 8314774: Optimize URLEncoder [v10]
温绍锦
duke at openjdk.org
Sun Sep 17 09:22:43 UTC 2023
On Tue, 5 Sep 2023 23:43:31 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.
>>
>> 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.
>>
>> The results of the `URLEncodeDecode` benchmark:
>>
>>
>> Before:
>> Benchmark (count) (maxLength) (mySeed) Mode Cnt Score Error Units
>> URLEncodeDecode.testEncodeUTF8 1024 1024 3 avgt 15 5.587 ? 0.010 ms/op
>>
>> After:
>> Benchmark (count) (maxLength) (mySeed) Mode Cnt Score Error Units
>> URLEncodeDecode.testEncodeUTF8 1024 1024 3 avgt 15 3.582 ? 0.054 ms/op
>>
>>
>> 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:
>
> Remove unused import
src/java.base/share/classes/java/net/URLEncoder.java line 155:
> 153: } else {
> 154: out.append((char) ('A' - 10 + n1));
> 155: }
I prefer writing like this, the bytecode will be much simpler
int n0 = (b >> 4) & 0xf;
int n1 = b & 0xf;
out.append((char) (n0 + (n0 < 10 ? '0' : 'A' - 10)));
out.append((char) (n1 + (n1 < 10 ? '0' : 'A' - 10)));
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15354#discussion_r1328067895
More information about the net-dev
mailing list