From 8aa35cacfcc94d261de102b628eb954c71eae98e Mon Sep 17 00:00:00 2001 From: Shaojin Wen Date: Mon, 10 Jun 2024 08:18:27 +0000 Subject: [PATCH] 8333833: Remove the use of ByteArrayLittleEndian from UUID::toString Reviewed-by: liach, redestad --- .../share/classes/java/util/UUID.java | 51 +++++++------------ .../classes/jdk/internal/util/HexDigits.java | 31 +++++------ 2 files changed, 31 insertions(+), 51 deletions(-) diff --git a/src/java.base/share/classes/java/util/UUID.java b/src/java.base/share/classes/java/util/UUID.java index a82f523a66f..e334f7263e4 100644 --- a/src/java.base/share/classes/java/util/UUID.java +++ b/src/java.base/share/classes/java/util/UUID.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,7 +31,6 @@ import java.security.*; import jdk.internal.access.JavaLangAccess; import jdk.internal.access.SharedSecrets; -import jdk.internal.util.ByteArrayLittleEndian; import jdk.internal.util.HexDigits; /** @@ -467,38 +466,24 @@ public final class UUID implements java.io.Serializable, Comparable { */ @Override public String toString() { - long lsb = leastSigBits; - long msb = mostSigBits; - byte[] buf = new byte[36]; - ByteArrayLittleEndian.setLong( - buf, - 0, - HexDigits.packDigits((int) (msb >> 56), (int) (msb >> 48), (int) (msb >> 40), (int) (msb >> 32))); - buf[8] = '-'; - ByteArrayLittleEndian.setInt( - buf, - 9, - HexDigits.packDigits(((int) msb) >> 24, ((int) msb) >> 16)); - buf[13] = '-'; - ByteArrayLittleEndian.setInt( - buf, - 14, - HexDigits.packDigits(((int) msb) >> 8, (int) msb)); - buf[18] = '-'; - ByteArrayLittleEndian.setInt( - buf, - 19, - HexDigits.packDigits((int) (lsb >> 56), (int) (lsb >> 48))); - buf[23] = '-'; - ByteArrayLittleEndian.setLong( - buf, - 24, - HexDigits.packDigits((int) (lsb >> 40), (int) (lsb >> 32), ((int) lsb) >> 24, ((int) lsb) >> 16)); - ByteArrayLittleEndian.setInt( - buf, - 32, - HexDigits.packDigits(((int) lsb) >> 8, (int) lsb)); + int i0 = (int) (mostSigBits >> 32); + int i1 = (int) mostSigBits; + int i2 = (int) (leastSigBits >> 32); + int i3 = (int) leastSigBits; + byte[] buf = new byte[36]; + HexDigits.put4(buf, 0, i0 >> 16); + HexDigits.put4(buf, 4, i0); + buf[8] = '-'; + HexDigits.put4(buf, 9, i1 >> 16); + buf[13] = '-'; + HexDigits.put4(buf, 14, i1); + buf[18] = '-'; + HexDigits.put4(buf, 19, i2 >> 16); + buf[23] = '-'; + HexDigits.put4(buf, 24, i2); + HexDigits.put4(buf, 28, i3 >> 16); + HexDigits.put4(buf, 32, i3); try { return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1); } catch (CharacterCodingException cce) { diff --git a/src/java.base/share/classes/jdk/internal/util/HexDigits.java b/src/java.base/share/classes/jdk/internal/util/HexDigits.java index 075443571e1..c08db4f5b48 100644 --- a/src/java.base/share/classes/jdk/internal/util/HexDigits.java +++ b/src/java.base/share/classes/jdk/internal/util/HexDigits.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -114,24 +114,19 @@ public final class HexDigits { } /** - * 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. + * Insert the unsigned 2-byte integer into the buffer as 4 hexadecimal digit ASCII bytes, + * only least significant 16 bits of {@code value} are used. + * @param buffer byte buffer to copy into + * @param index insert point + * @param value to convert */ - public static int packDigits(int b0, int b1) { - return DIGITS[b0 & 0xff] | (DIGITS[b1 & 0xff] << 16); - } - - /** - * 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 packDigits(int b0, int b1, int b2, int b3) { - return DIGITS[b0 & 0xff] - | (DIGITS[b1 & 0xff] << 16) - | (((long) DIGITS[b2 & 0xff]) << 32) - | (((long) DIGITS[b3 & 0xff]) << 48); + public static void put4(byte[] buffer, int index, int value) { + // Prepare an int value so C2 generates a 4-byte write instead of two 2-byte writes + int v = (DIGITS[value & 0xff] << 16) | DIGITS[(value >> 8) & 0xff]; + buffer[index] = (byte) v; + buffer[index + 1] = (byte) (v >> 8); + buffer[index + 2] = (byte) (v >> 16); + buffer[index + 3] = (byte) (v >> 24); } /**