RFR: JDK-8310502 : Optimization for j.l.Long.fastUUID() [v14]

ExE Boss duke at openjdk.org
Thu Jun 22 12:22:09 UTC 2023


On Thu, 22 Jun 2023 11:07:09 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:
> 
>   use ByteArray.setInt & setLong

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

> 525:         buf[70 + off] = (byte) i15;
> 526: 
> 527:         return new String(buf, UTF16);

Maybe restore the `else` block?
Suggestion:

        } else {
            byte[] buf = new byte[72];
            int off = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? 1 : 0;

            buf[0 + off] = (byte) (i0 >> 8);
            buf[2 + off] = (byte) i0;
            buf[4 + off] = (byte) (i1 >> 8);
            buf[6 + off] = (byte) i1;
            buf[8 + off] = (byte) (i2 >> 8);
            buf[10 + off] = (byte) i2;
            buf[12 + off] = (byte) (i3 >> 8);
            buf[14 + off] = (byte) i3;
            buf[16 + off] = '-';
            buf[18 + off] = (byte) (i4 >> 8);
            buf[20 + off] = (byte) i4;
            buf[22 + off] = (byte) (i5 >> 8);
            buf[24 + off] = (byte) i5;
            buf[26 + off] = '-';
            buf[28 + off] = (byte) (i6 >> 8);
            buf[30 + off] = (byte) i6;
            buf[32 + off] = (byte) (i7 >> 8);
            buf[34 + off] = (byte) i7;
            buf[36 + off] = '-';
            buf[38 + off] = (byte) (i8 >> 8);
            buf[40 + off] = (byte) i8;
            buf[42 + off] = (byte) (i9 >> 8);
            buf[44 + off] = (byte) i9;
            buf[46 + off] = '-';
            buf[48 + off] = (byte) (i10 >> 8);
            buf[50 + off] = (byte) i10;
            buf[52 + off] = (byte) (i11 >> 8);
            buf[54 + off] = (byte) i11;
            buf[56 + off] = (byte) (i12 >> 8);
            buf[58 + off] = (byte) i12;
            buf[60 + off] = (byte) (i13 >> 8);
            buf[62 + off] = (byte) i13;
            buf[64 + off] = (byte) (i14 >> 8);
            buf[66 + off] = (byte) i14;
            buf[68 + off] = (byte) (i15 >> 8);
            buf[70 + off] = (byte) i15;

            return new String(buf, UTF16);
        }

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

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


More information about the core-libs-dev mailing list