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

Glavo duke at openjdk.org
Wed Jun 21 19:24:05 UTC 2023


On Wed, 21 Jun 2023 14:39:18 GMT, 温绍锦 <duke at openjdk.org> wrote:

>> By optimizing the implementation of java.lang.Long#fastUUID, the performance of the java.util.UUID#toString method can be significantly improved.
>> 
>> The following are the test results of JMH: 
>> 
>> Benchmark                     Mode  Cnt      Score      Error   Units
>> UUIDUtilsBenchmark.new       thrpt    5  92676.550 ±  292.213  ops/ms
>> UUIDUtilsBenchmark.original  thrpt    5  37040.165 ± 1023.532  ops/ms
>
> 温绍锦 has updated the pull request incrementally with one additional commit since the last revision:
> 
>   add annotation Stable

src/java.base/share/classes/java/lang/Long.java line 548:

> 546:             buf[33] = (byte) i14;
> 547:             buf[34] = (byte) (i15 >> 8);
> 548:             buf[35] = (byte) i15;

You may be able to use `jdk.internal.util.ByteArray` to simplify code and improve performance:

Suggestion:

            ByteArray.setChar(buf, 0, i0);
            ByteArray.setChar(buf, 2, i1);
            ByteArray.setChar(buf, 4, i2);
            ByteArray.setChar(buf, 6, i3);
            buf[8] = '-';
            ByteArray.setChar(buf, 9, i4);
            ByteArray.setChar(buf, 11, i5);
            buf[13] = '-';
            ByteArray.setChar(buf, 14, i6);
            ByteArray.setChar(buf, 16, i7);
            buf[18] = '-';
            ByteArray.setChar(buf, 19, i8);
            ByteArray.setChar(buf, 21, i9);
            buf[23] = '-';
            ByteArray.setChar(buf, 24, i10);
            ByteArray.setChar(buf, 26, i11);
            ByteArray.setChar(buf, 28, i12);
            ByteArray.setChar(buf, 30, i13);
            ByteArray.setChar(buf, 32, i14);
            ByteArray.setChar(buf, 34, i15);


I measured the throughput of `UUID::toString()` using JMH on my linux server (CPU: R7-5800X):


  Benchmark                 Mode  Cnt      Score     Error   Units
- UUIDUtilsBenchmark.test  thrpt    5  76117.237 ± 461.679  ops/ms
+ UUIDUtilsBenchmark.test  thrpt    5  87291.496 ± 729.527  ops/ms


The result of this benchmark test is that using `ByteArray` can improve peak throughput by nearly 15%.

Note: `ByteArray` uses `VarHandle` internally, and the impact on startup time is unknown, so more testing is needed.

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

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


More information about the core-libs-dev mailing list