RFR: 8333833: UUID toString removes the use of ByteArrayLittleEndian [v3]

Shaojin Wen duke at openjdk.org
Sun Jun 9 01:19:13 UTC 2024


On Sat, 8 Jun 2024 23:30:38 GMT, Shaojin Wen <duke at openjdk.org> wrote:

>> After PR #16245, C2 optimizes stores into primitive arrays by combining values ​​into larger stores. In the UUID.toString method, ByteArrayLittleEndian can be removed, making the code more elegant and faster.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   change method name, putHex -> putHex4, and fix comments

> I think we don't need to change them back everywhere, but only need to rewrite `ByteArrayLittleEndian` and `ByteArray` so that they no longer use `VarHandle`.
> 
> Maybe I should rewrite #14636 without using `Unsafe`, so more people might agree with it.

You are right, ByteArray and ByteArrayLittleEndian have good performance after removing Unsafe. This is similar to the previous version of java.io.Bits


class ByteArrayLittleEndian {
    public static void setInt(byte[] array, int offset, int value) {
        array[offset    ] = (byte)  value;
        array[offset + 1] = (byte) (value >> 8);
        array[offset + 2] = (byte) (value >> 16);
        array[offset + 3] = (byte) (value >> 24);
    }
}

class HexDigits {
    public static void putHex4(byte[] array, int offset, int value) {
        // Prepare an int value so C2 generates a 4-byte write instead of two 2-byte writes
        ByteArrayLittleEndian.setInt(
                array,
                offset,
                (DIGITS[value & 0xff] << 16) | DIGITS[(value >> 8) & 0xff]);
    }
}

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

PR Comment: https://git.openjdk.org/jdk/pull/19610#issuecomment-2156255748


More information about the core-libs-dev mailing list