RFR: 8333833: UUID toString removes the use of ByteArrayLittleEndian [v2]
Shaojin Wen
duke at openjdk.org
Sat Jun 8 23:52:12 UTC 2024
On Sat, 8 Jun 2024 18:00:43 GMT, Chen Liang <liach at openjdk.org> wrote:
>> src/java.base/share/classes/jdk/internal/util/HexDigits.java line 122:
>>
>>> 120: * @param value to convert
>>> 121: */
>>> 122: public static void putHex(byte[] buffer, int off, int i) {
>>
>> Should there be 2 methods - for 2 and 4 bytes respectively?
>> Does c2 optimize 8 byte writes as well?
>
> The 4-byte unsigned int input for 8-byte write sounds plausible, I personally am fine either with or without it.
>
>> Does c2 optimize 8 byte writes as well?
>
> From the first few lines of #16245:
>
>> Merging multiple consecutive small stores (e.g. 8 byte stores) into larger stores (e.g. one long store) can lead to speedup.
8-byte writing requires converting int to long. The performance is similar to the current version, but an additional method putHex8 needs to be added. The current version has less code.
The following is the code for writing 8 bytes:
class UUID {
@Override
public String toString() {
byte[] buf = new byte[36];
HexDigits.putHex8(buf, 0, (int) (mostSigBits >> 32));
HexDigits.putHex10(buf, 8, (int) mostSigBits);
HexDigits.putHex10(buf, 18, (int) (leastSigBits >> 32));
HexDigits.putHex8(buf, 28, (int) leastSigBits);
try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
}
}
class HexDigits {
public static void putHex8(byte[] bytes, int off, int i) {
long v = (((long) DIGITS[(i >> 16) & 0xff]) << 48)
| (((long) DIGITS[(i >> 24) & 0xff]) << 32)
| ( DIGITS[ i & 0xff] << 16)
| ( DIGITS[(i >> 8 ) & 0xff]);
bytes[off] = (byte) v;
bytes[off + 1] = (byte) (v >> 8);
bytes[off + 2] = (byte) (v >> 16);
bytes[off + 3] = (byte) (v >> 24);
bytes[off + 4] = (byte) (v >> 32);
bytes[off + 5] = (byte) (v >> 40);
bytes[off + 6] = (byte) (v >> 48);
bytes[off + 7] = (byte) (v >> 56);
}
public static void putHex10(byte[] bytes, int off, int i) {
int v0 = (DIGITS[(i >> 16) & 0xff] << 16)
| DIGITS[(i >> 24) & 0xff];
int v1 = (DIGITS[i & 0xff] << 16)
| DIGITS[(i >> 8 ) & 0xff];
bytes[off] = '-';
bytes[off + 1] = (byte) v0;
bytes[off + 2] = (byte) (v0 >> 8);
bytes[off + 3] = (byte) (v0 >> 16);
bytes[off + 4] = (byte) (v0 >> 24);
bytes[off + 5] = '-';
bytes[off + 6] = (byte) v1;
bytes[off + 7] = (byte) (v1 >> 8);
bytes[off + 8] = (byte) (v1 >> 16);
bytes[off + 9] = (byte) (v1 >> 24);
}
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/19610#discussion_r1632133853
More information about the core-libs-dev
mailing list