8148936: Adapt UUID.toString() to Compact Strings

Reviewed-by: igerasim, redestad
This commit is contained in:
Aleksey Shipilev 2016-02-04 16:00:48 +03:00
parent f8fa6e063d
commit 24816037ab
5 changed files with 41 additions and 36 deletions

View File

@ -401,18 +401,6 @@ public final class Long extends Number implements Comparable<Long> {
* @param offset the offset in the destination buffer to start at * @param offset the offset in the destination buffer to start at
* @param len the number of characters to write * @param len the number of characters to write
*/ */
static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
// assert shift > 0 && shift <=5 : "Illegal shift value";
// assert offset >= 0 && offset < buf.length : "illegal offset";
// assert len > 0 && (offset + len) <= buf.length : "illegal length";
int charPos = offset + len;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = Integer.digits[((int) val) & mask];
val >>>= shift;
} while (charPos > offset);
}
/** byte[]/LATIN1 version */ /** byte[]/LATIN1 version */
static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) { static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {
@ -436,6 +424,39 @@ public final class Long extends Number implements Comparable<Long> {
} while (charPos > offset); } while (charPos > offset);
} }
static String fastUUID(long lsb, long msb) {
if (COMPACT_STRINGS) {
byte[] buf = new byte[36];
formatUnsignedLong0(lsb, 4, buf, 24, 12);
formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4);
formatUnsignedLong0(msb, 4, buf, 14, 4);
formatUnsignedLong0(msb >>> 16, 4, buf, 9, 4);
formatUnsignedLong0(msb >>> 32, 4, buf, 0, 8);
buf[23] = '-';
buf[18] = '-';
buf[13] = '-';
buf[8] = '-';
return new String(buf, LATIN1);
} else {
byte[] buf = new byte[72];
formatUnsignedLong0UTF16(lsb, 4, buf, 24, 12);
formatUnsignedLong0UTF16(lsb >>> 48, 4, buf, 19, 4);
formatUnsignedLong0UTF16(msb, 4, buf, 14, 4);
formatUnsignedLong0UTF16(msb >>> 16, 4, buf, 9, 4);
formatUnsignedLong0UTF16(msb >>> 32, 4, buf, 0, 8);
StringUTF16.putChar(buf, 23, '-');
StringUTF16.putChar(buf, 18, '-');
StringUTF16.putChar(buf, 13, '-');
StringUTF16.putChar(buf, 8, '-');
return new String(buf, UTF16);
}
}
/** /**
* Returns a {@code String} object representing the specified * Returns a {@code String} object representing the specified
* {@code long}. The argument is converted to signed decimal * {@code long}. The argument is converted to signed decimal

View File

@ -1911,11 +1911,8 @@ public final class System {
public void invokeFinalize(Object o) throws Throwable { public void invokeFinalize(Object o) throws Throwable {
o.finalize(); o.finalize();
} }
public void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { public String fastUUID(long lsb, long msb) {
Long.formatUnsignedLong(val, shift, buf, offset, len); return Long.fastUUID(lsb, msb);
}
public void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
Integer.formatUnsignedInt(val, shift, buf, offset, len);
} }
}); });
} }

View File

@ -392,17 +392,7 @@ public final class UUID implements java.io.Serializable, Comparable<UUID> {
* @return A string representation of this {@code UUID} * @return A string representation of this {@code UUID}
*/ */
public String toString() { public String toString() {
char[] chars = new char[36]; return jla.fastUUID(leastSigBits, mostSigBits);
jla.formatUnsignedLong(mostSigBits >> 32, 4, chars, 0, 8);
chars[8] = '-';
jla.formatUnsignedLong(mostSigBits >> 16, 4, chars, 9, 4);
chars[13] = '-';
jla.formatUnsignedLong(mostSigBits, 4, chars, 14, 4);
chars[18] = '-';
jla.formatUnsignedLong(leastSigBits >> 48, 4, chars, 19, 4);
chars[23] = '-';
jla.formatUnsignedLong(leastSigBits, 4, chars, 24, 12);
return jla.newStringUnsafe(chars);
} }
/** /**

View File

@ -124,12 +124,7 @@ public interface JavaLangAccess {
void invokeFinalize(Object o) throws Throwable; void invokeFinalize(Object o) throws Throwable;
/** /**
* Invokes Long.formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) * Invokes Long.fastUUID
*/ */
void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len); String fastUUID(long lsb, long msb);
/**
* Invokes Integer.formatUnsignedInt(long val, int shift, char[] buf, int offset, int len)
*/
void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len);
} }

View File

@ -22,9 +22,11 @@
*/ */
/* @test /* @test
* @bug 4173528 5068772 * @bug 4173528 5068772 8148936
* @summary Unit tests for java.util.UUID * @summary Unit tests for java.util.UUID
* @key randomness * @key randomness
* @run main/othervm -XX:+CompactStrings UUIDTest
* @run main/othervm -XX:-CompactStrings UUIDTest
*/ */
import java.util.*; import java.util.*;