RFR: 8310502 : optimization for UUID#toString [v4]

Chen Liang liach at openjdk.org
Wed Jun 21 14:39:19 UTC 2023


On Wed, 21 Jun 2023 14:13:34 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>>> > Another thing to try is moving fastUUID out of Long - moving to an array of precomputed hex values means it is not tied to Long internals anymore.
>>> 
>>> A note about `@Stable`: `Integer.digits` and `HEX256` are not, and they might see performance improvements and change the benchmark results if they are declared so.
>> 
>> use HEX256 optimize Integer.toHexString
>> https://github.com/wenshao/jdk_8310502_test/blob/main/src/main/java/com/alibaba/openjdk/HexUtils.java
>> 
>>        char[] hex256 = UUIDUtils.HEX256;
>> 
>>         int i0 = (i >> 24) & 255;
>>         int i1 = (i >> 16) & 255;
>>         int i2 = (i >> 8) & 255;
>>         int i3 = i & 255;
>> 
>>         char c0 = hex256[i0];
>>         char c1 = hex256[i1];
>>         char c2 = hex256[i2];
>>         char c3 = hex256[i3];
>> 
>>         byte[] bytes;
>>         if (COMPACT_STRINGS) {
>>             if ((i >> 4) == 0) {
>>                 bytes = new byte[1];
>>                 bytes[0] = (byte) c3;
>>             } else if ((i >> 8) == 0) {
>>                 bytes = new byte[2];
>>                 bytes[0] = (byte) (c3 >> 8);
>>                 bytes[1] = (byte) c3;
>>             } else if ((i >> 12) == 0) {
>>                 bytes = new byte[3];
>>                 bytes[0] = (byte) c2;
>>                 bytes[1] = (byte) (c3 >> 8);
>>                 bytes[2] = (byte) c3;
>>             } else if ((i >> 16) == 0) {
>>                 bytes = new byte[4];
>>                 bytes[0] = (byte) (c2 >> 8);
>>                 bytes[1] = (byte) c2;
>>                 bytes[2] = (byte) (c3 >> 8);
>>                 bytes[3] = (byte) c3;
>>             } else if ((i >> 20) == 0) {
>>                 bytes = new byte[5];
>>                 bytes[0] = (byte) c1;
>>                 bytes[1] = (byte) (c2 >> 8);
>>                 bytes[2] = (byte) c2;
>>                 bytes[3] = (byte) (c3 >> 8);
>>                 bytes[4] = (byte) c3;
>>             } else if ((i >> 24) == 0) {
>>                 bytes = new byte[6];
>>                 bytes[0] = (byte) (c1 >> 8);
>>                 bytes[1] = (byte) c1;
>>                 bytes[2] = (byte) (c2 >> 8);
>>                 bytes[3] = (byte) c2;
>>                 bytes[4] = (byte) (c3 >> 8);
>>                 bytes[5] = (byte) c3;
>>             } else if ((i >> 28) == 0) {
>>                 bytes = new byte[7];
>>                 bytes[0] = (byte) c0;
>>                 bytes[1] = (byte) (c1 >> 8);
>>                 bytes[2] = (byte) c1;
>>                 bytes[3] = (byte) (c2 >> 8);
>>         ...
>
> Not sure if this can be applied but some months ago, I optimized Bits to use VarHandles rather than explicitly shifting bits around. This gave us a significant performance increase:
> 
> https://github.com/openjdk/jdk/pull/11840/files

The key to the optimization was MethodHandles.byteArrayViewVarHandle. Don't know if StringUTF16.putChar and StringUTF16.getChar can benefit from such an update.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/14578#discussion_r1237109343


More information about the core-libs-dev mailing list