RFR: 8310502 : optimization for UUID#toString

温绍锦 duke at openjdk.org
Wed Jun 21 09:52:02 UTC 2023


On Wed, 21 Jun 2023 08:58:53 GMT, Chen Liang <liach 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);
                bytes[4] = (byte) c2;
                bytes[5] = (byte) (c3 >> 8);
                bytes[6] = (byte) c3;
            } else {
                bytes = new byte[8];
                bytes[0] = (byte) (c0 >> 8);
                bytes[1] = (byte) c0;
                bytes[2] = (byte) (c1 >> 8);
                bytes[3] = (byte) c1;
                bytes[4] = (byte) (c2 >> 8);
                bytes[5] = (byte) c2;
                bytes[6] = (byte) (c3 >> 8);
                bytes[7] = (byte) c3;
            }

            return new Stringbytes, LATIN1);
      }
      // ....


* benchmark code
https://github.com/wenshao/jdk_8310502_test/blob/main/src/main/java/com/alibaba/openjdk/HexUtilsBenchmark.java

* benchmark result

Benchmark                Mode  Cnt     Score    Error   Units
HexUtilsBenchmark.fast  thrpt    5  2910.332 ± 20.051  ops/ms
HexUtilsBenchmark.jdk   thrpt    5  1966.712 ± 48.141  ops/ms

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

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


More information about the core-libs-dev mailing list