8310502: Optimization for j.l.Long.fastUUID()

Reviewed-by: liach, alanb
This commit is contained in:
shaojin.wensj 2023-06-29 13:37:32 +00:00 committed by Jie Fu
parent 07734f6dde
commit 20f7d05ef2
5 changed files with 96 additions and 43 deletions

View File

@ -445,39 +445,6 @@ public final class Long extends Number
} 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
* {@code long}. The argument is converted to signed decimal

View File

@ -2395,9 +2395,6 @@ public final class System {
public Package definePackage(ClassLoader cl, String name, Module module) {
return cl.definePackage(name, module);
}
public String fastUUID(long lsb, long msb) {
return Long.fastUUID(lsb, msb);
}
@SuppressWarnings("removal")
public void addNonExportedPackages(ModuleLayer layer) {
SecurityManager.addNonExportedPackages(layer);

View File

@ -35,6 +35,44 @@ import jdk.internal.vm.annotation.Stable;
* @since 21
*/
final class HexDigits implements Digits {
/**
* Each element of the array represents the ascii encoded
* hex relative to its index, for example:<p>
* <pre>
* 0 -> '00' -> ('0' << 8) | '0' -> 12336
* 1 -> '01' -> ('0' << 8) | '1' -> 12337
* 2 -> '02' -> ('0' << 8) | '2' -> 12338
*
* ...
*
* 10 -> '0a' -> ('0' << 8) | 'a' -> 12385
* 11 -> '0b' -> ('0' << 8) | 'b' -> 12386
* 12 -> '0c' -> ('0' << 8) | 'b' -> 12387
*
* ...
*
* 26 -> '1a' -> ('1' << 8) | 'a' -> 12641
* 27 -> '1b' -> ('1' << 8) | 'b' -> 12642
* 28 -> '1c' -> ('1' << 8) | 'c' -> 12643
*
* ...
*
* 253 -> 'fd' -> ('f' << 8) | 'd' -> 26212
* 254 -> 'fe' -> ('f' << 8) | 'e' -> 26213
* 255 -> 'ff' -> ('f' << 8) | 'f' -> 26214
* </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>
*/
@Stable
private static final short[] DIGITS;
@ -64,6 +102,23 @@ final class HexDigits implements Digits {
private HexDigits() {
}
/**
* Combine two hex shorts into one int based on big endian
*/
static int digit(int b0, int b1) {
return (DIGITS[b0 & 0xff] << 16) | DIGITS[b1 & 0xff];
}
/**
* Combine four hex shorts into one long based on big endian
*/
static long digit(int b0, int b1, int b2, int b3) {
return (((long) DIGITS[b0 & 0xff]) << 48)
| (((long) DIGITS[b1 & 0xff]) << 32)
| (DIGITS[b2 & 0xff] << 16)
| DIGITS[b3 & 0xff];
}
@Override
public int digits(long value, byte[] buffer, int index,
MethodHandle putCharMH) throws Throwable {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
@ -25,10 +25,13 @@
package java.util;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ByteArray;
/**
* A class that represents an immutable universally unique identifier (UUID).
@ -463,7 +466,43 @@ public final class UUID implements java.io.Serializable, Comparable<UUID> {
*/
@Override
public String toString() {
return jla.fastUUID(leastSigBits, mostSigBits);
long lsb = leastSigBits;
long msb = mostSigBits;
byte[] buf = new byte[36];
ByteArray.setLong(
buf,
0,
HexDigits.digit((int) (msb >> 56), (int) (msb >> 48), (int) (msb >> 40), (int) (msb >> 32)));
buf[8] = '-';
ByteArray.setInt(
buf,
9,
HexDigits.digit(((int) msb) >> 24, ((int) msb) >> 16));
buf[13] = '-';
ByteArray.setInt(
buf,
14,
HexDigits.digit(((int) msb) >> 8, (int) msb));
buf[18] = '-';
ByteArray.setInt(
buf,
19,
HexDigits.digit((int) (lsb >> 56), (int) (lsb >> 48)));
buf[23] = '-';
ByteArray.setLong(
buf,
24,
HexDigits.digit(((int) (lsb >> 40)), (int) (lsb >> 32), ((int) lsb) >> 24, ((int) lsb) >> 16));
ByteArray.setInt(
buf,
32,
HexDigits.digit(((int) lsb) >> 8, (int) lsb));
try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
}
/**

View File

@ -176,11 +176,6 @@ public interface JavaLangAccess {
*/
Package definePackage(ClassLoader cl, String name, Module module);
/**
* Invokes Long.fastUUID
*/
String fastUUID(long lsb, long msb);
/**
* Record the non-exported packages of the modules in the given layer
*/