8311207: Cleanup for Optimization for UUID.toString

Reviewed-by: liach, rriggs
This commit is contained in:
shaojin.wensj 2023-09-13 08:25:13 +00:00 committed by Claes Redestad
parent fecd2fd8f2
commit f8df754b9a
2 changed files with 43 additions and 50 deletions

View File

@ -31,7 +31,7 @@ import java.security.*;
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ByteArray; import jdk.internal.util.ByteArrayLittleEndian;
import jdk.internal.util.HexDigits; import jdk.internal.util.HexDigits;
/** /**
@ -470,34 +470,34 @@ public final class UUID implements java.io.Serializable, Comparable<UUID> {
long lsb = leastSigBits; long lsb = leastSigBits;
long msb = mostSigBits; long msb = mostSigBits;
byte[] buf = new byte[36]; byte[] buf = new byte[36];
ByteArray.setLong( ByteArrayLittleEndian.setLong(
buf, buf,
0, 0,
HexDigits.digit((int) (msb >> 56), (int) (msb >> 48), (int) (msb >> 40), (int) (msb >> 32))); HexDigits.packDigits((int) (msb >> 56), (int) (msb >> 48), (int) (msb >> 40), (int) (msb >> 32)));
buf[8] = '-'; buf[8] = '-';
ByteArray.setInt( ByteArrayLittleEndian.setInt(
buf, buf,
9, 9,
HexDigits.digit(((int) msb) >> 24, ((int) msb) >> 16)); HexDigits.packDigits(((int) msb) >> 24, ((int) msb) >> 16));
buf[13] = '-'; buf[13] = '-';
ByteArray.setInt( ByteArrayLittleEndian.setInt(
buf, buf,
14, 14,
HexDigits.digit(((int) msb) >> 8, (int) msb)); HexDigits.packDigits(((int) msb) >> 8, (int) msb));
buf[18] = '-'; buf[18] = '-';
ByteArray.setInt( ByteArrayLittleEndian.setInt(
buf, buf,
19, 19,
HexDigits.digit((int) (lsb >> 56), (int) (lsb >> 48))); HexDigits.packDigits((int) (lsb >> 56), (int) (lsb >> 48)));
buf[23] = '-'; buf[23] = '-';
ByteArray.setLong( ByteArrayLittleEndian.setLong(
buf, buf,
24, 24,
HexDigits.digit(((int) (lsb >> 40)), (int) (lsb >> 32), ((int) lsb) >> 24, ((int) lsb) >> 16)); HexDigits.packDigits((int) (lsb >> 40), (int) (lsb >> 32), ((int) lsb) >> 24, ((int) lsb) >> 16));
ByteArray.setInt( ByteArrayLittleEndian.setInt(
buf, buf,
32, 32,
HexDigits.digit(((int) lsb) >> 8, (int) lsb)); HexDigits.packDigits(((int) lsb) >> 8, (int) lsb));
try { try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1); return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);

View File

@ -39,38 +39,27 @@ public final class HexDigits implements Digits {
* Each element of the array represents the ascii encoded * Each element of the array represents the ascii encoded
* hex relative to its index, for example:<p> * hex relative to its index, for example:<p>
* <pre> * <pre>
* 0 -> '00' -> ('0' << 8) | '0' -> 12336 * 0 -> '00' -> '0' | ('0' << 8) -> 0x3030
* 1 -> '01' -> ('0' << 8) | '1' -> 12337 * 1 -> '01' -> '0' | ('1' << 8) -> 0x3130
* 2 -> '02' -> ('0' << 8) | '2' -> 12338 * 2 -> '02' -> '0' | ('2' << 8) -> 0x3230
* *
* ... * ...
* *
* 10 -> '0a' -> ('0' << 8) | 'a' -> 12385 * 10 -> '0a' -> '0' | ('a' << 8) -> 0x6130
* 11 -> '0b' -> ('0' << 8) | 'b' -> 12386 * 11 -> '0b' -> '0' | ('b' << 8) -> 0x6230
* 12 -> '0c' -> ('0' << 8) | 'b' -> 12387 * 12 -> '0c' -> '0' | ('b' << 8) -> 0x6330
* *
* ... * ...
* *
* 26 -> '1a' -> ('1' << 8) | 'a' -> 12641 * 26 -> '1a' -> '1' | ('a' << 8) -> 0x6131
* 27 -> '1b' -> ('1' << 8) | 'b' -> 12642 * 27 -> '1b' -> '1' | ('b' << 8) -> 0x6231
* 28 -> '1c' -> ('1' << 8) | 'c' -> 12643 * 28 -> '1c' -> '1' | ('c' << 8) -> 0x6331
* *
* ... * ...
* *
* 253 -> 'fd' -> ('f' << 8) | 'd' -> 26212 * 253 -> 'fd' -> 'f' | ('d' << 8) -> 0x6466
* 254 -> 'fe' -> ('f' << 8) | 'e' -> 26213 * 254 -> 'fe' -> 'f' | ('e' << 8) -> 0x6566
* 255 -> 'ff' -> ('f' << 8) | 'f' -> 26214 * 255 -> 'ff' -> 'f' | ('f' << 8) -> 0x6666
* </pre>
* <p>use like this:
* <pre>
* int v = 254;
*
* char[] chars = new char[2];
*
* short i = DIGITS[v]; // 26213
*
* chars[0] = (char) (byte) (i >> 8); // 'f'
* chars[1] = (char) (byte) i; // 'e'
* </pre> * </pre>
*/ */
@Stable @Stable
@ -85,10 +74,10 @@ public final class HexDigits implements Digits {
short[] digits = new short[16 * 16]; short[] digits = new short[16 * 16];
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
short hi = (short) ((i < 10 ? i + '0' : i - 10 + 'a') << 8); short lo = (short) (i < 10 ? i + '0' : i - 10 + 'a');
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
short lo = (short) (j < 10 ? j + '0' : j - 10 + 'a'); short hi = (short) ((j < 10 ? j + '0' : j - 10 + 'a') << 8);
digits[(i << 4) + j] = (short) (hi | lo); digits[(i << 4) + j] = (short) (hi | lo);
} }
} }
@ -103,20 +92,24 @@ public final class HexDigits implements Digits {
} }
/** /**
* Combine two hex shorts into one int based on big endian * Return a little-endian packed integer for the 4 ASCII bytes for an input unsigned 2-byte integer.
* {@code b0} is the most significant byte and {@code b1} is the least significant byte.
* The integer is passed byte-wise to allow reordering of execution.
*/ */
public static int digit(int b0, int b1) { public static int packDigits(int b0, int b1) {
return (DIGITS[b0 & 0xff] << 16) | DIGITS[b1 & 0xff]; return DIGITS[b0 & 0xff] | (DIGITS[b1 & 0xff] << 16);
} }
/** /**
* Combine four hex shorts into one long based on big endian * Return a little-endian packed long for the 8 ASCII bytes for an input unsigned 4-byte integer.
* {@code b0} is the most significant byte and {@code b3} is the least significant byte.
* The integer is passed byte-wise to allow reordering of execution.
*/ */
public static long digit(int b0, int b1, int b2, int b3) { public static long packDigits(int b0, int b1, int b2, int b3) {
return (((long) DIGITS[b0 & 0xff]) << 48) return DIGITS[b0 & 0xff]
| (((long) DIGITS[b1 & 0xff]) << 32) | (DIGITS[b1 & 0xff] << 16)
| (DIGITS[b2 & 0xff] << 16) | (((long) DIGITS[b2 & 0xff]) << 32)
| DIGITS[b3 & 0xff]; | (((long) DIGITS[b3 & 0xff]) << 48);
} }
@Override @Override
@ -125,15 +118,15 @@ public final class HexDigits implements Digits {
while ((value & ~0xFF) != 0) { while ((value & ~0xFF) != 0) {
int digits = DIGITS[(int) (value & 0xFF)]; int digits = DIGITS[(int) (value & 0xFF)];
value >>>= 8; value >>>= 8;
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
putCharMH.invokeExact(buffer, --index, digits >> 8); putCharMH.invokeExact(buffer, --index, digits >> 8);
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
} }
int digits = DIGITS[(int) (value & 0xFF)]; int digits = DIGITS[(int) (value & 0xFF)];
putCharMH.invokeExact(buffer, --index, digits & 0xFF); putCharMH.invokeExact(buffer, --index, digits >> 8);
if (0xF < value) { if (0xF < value) {
putCharMH.invokeExact(buffer, --index, digits >> 8); putCharMH.invokeExact(buffer, --index, digits & 0xFF);
} }
return index; return index;