Skip to content

Commit

Permalink
8310502: Optimization for j.l.Long.fastUUID()
Browse files Browse the repository at this point in the history
Reviewed-by: liach, alanb
  • Loading branch information
wenshao authored and DamonFool committed Jun 29, 2023
1 parent 07734f6 commit 20f7d05
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 43 deletions.
33 changes: 0 additions & 33 deletions src/java.base/share/classes/java/lang/Long.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,39 +445,6 @@ private static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, in
} 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
Expand Down
3 changes: 0 additions & 3 deletions src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -2395,9 +2395,6 @@ public Class<?> findBootstrapClassOrNull(String name) {
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);
Expand Down
55 changes: 55 additions & 0 deletions src/java.base/share/classes/java/util/HexDigits.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@
* @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;

Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 41 additions & 2 deletions src/java.base/share/classes/java/util/UUID.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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).
Expand Down Expand Up @@ -463,7 +466,43 @@ public long node() {
*/
@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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

1 comment on commit 20f7d05

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.