Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8310502 : Optimization for j.l.Long.fastUUID() #14578

Closed
wants to merge 35 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1459281
8310502 : optimization for UUID#toString
wenshao Jun 21, 2023
35af153
8310502 : HEX256 initialization use constant values improved jvm startup
wenshao Jun 21, 2023
8bc49c9
8310502 : hex literal
wenshao Jun 21, 2023
a952e10
add annotation Stable
wenshao Jun 21, 2023
c73f7ed
simplify code
wenshao Jun 21, 2023
ad53343
import ByteOrder
wenshao Jun 21, 2023
e0bcf6c
move HEX256 to LongCache
wenshao Jun 22, 2023
2ec33ac
fix compile error
wenshao Jun 22, 2023
a591e31
use single holder class cache HEX256
wenshao Jun 22, 2023
ae5c2e8
fix compile error
wenshao Jun 22, 2023
728dbdd
use ByteArray for COMPACT_STRINGS disable
wenshao Jun 22, 2023
c6dca16
revert to non-ByteArray
wenshao Jun 22, 2023
2c9660a
use ByteArray & simplify code
wenshao Jun 22, 2023
5a5e7e1
use ByteArray.setInt & setLong
wenshao Jun 22, 2023
c11668c
make fastUUID codeSize less than 325 (FreqInlineSize)
wenshao Jun 22, 2023
2b35aa4
add import & rename HEX to H256
wenshao Jun 22, 2023
ff6e7e4
format code
wenshao Jun 22, 2023
27a8212
remove Arrays.fill
wenshao Jun 23, 2023
dce6296
remove unused import
wenshao Jun 23, 2023
0d34655
make Long.fastUUID codeSize less than 325 (FreqInlineSize)
wenshao Jun 23, 2023
f08444f
format code
wenshao Jun 23, 2023
17a9f16
add jdk.util.HexDigits, sharing cache array across multiple classes, …
wenshao Jun 25, 2023
efaa869
move fastUUID implementation to UUID.toString
wenshao Jun 25, 2023
4b5019f
remove JavaLangAccess#fastUUID
wenshao Jun 25, 2023
6b61a71
use ISO_8859_1.INSTANCE directly instead of StandardCharsets.ISO_8859_1
wenshao Jun 26, 2023
9fe1d70
fix UUID.java import, rename jdk.util.HexDigits to jdk.util.Hex256 an…
wenshao Jun 27, 2023
fe9efa6
Update src/java.base/share/classes/jdk/internal/util/Hex256.java
wenshao Jun 27, 2023
b81f12d
change copyright years, and rename jdk.internal.util.Hex256 to jdk.in…
wenshao Jun 27, 2023
4a4c64b
revert to HexDigits.DIGITS, keep this PR as simple as possible.
wenshao Jun 27, 2023
26f3a6a
remove unused import, and fix comment
wenshao Jun 27, 2023
0a7f2cc
revert HexDigits.DIGITS private
wenshao Jun 29, 2023
93e74b6
code format
wenshao Jun 29, 2023
d8ee124
code format & comments add endianness
wenshao Jun 29, 2023
fe144ef
Update full name
wenshao Jun 29, 2023
aea4e04
fix comments typo
wenshao Jun 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 37 additions & 51 deletions src/java.base/share/classes/java/lang/Long.java
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@

import jdk.internal.misc.CDS;
import jdk.internal.util.ByteArray;
import jdk.internal.util.HexDigits;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
@@ -449,72 +450,71 @@ private static void formatUnsignedLong0UTF16(long val, int shift, byte[] buf, in

static String fastUUID(long lsb, long msb) {
byte[] buf = new byte[36];
char[] H256 = DigitCache.HEX256;

short[] digits = HexDigits.DIGITS;
ByteArray.setLong(
buf,
0,
((long) H256[((int) (msb >> 56)) & 0xff] << 48)
| ((long) H256[((int) (msb >> 48)) & 0xff] << 32)
| ((long) H256[((int) (msb >> 40)) & 0xff] << 16)
| H256[((int) (msb >> 32)) & 0xff]);
((long) digits[((int) (msb >> 56)) & 0xff] << 48)
| ((long) digits[((int) (msb >> 48)) & 0xff] << 32)
| ((long) digits[((int) (msb >> 40)) & 0xff] << 16)
| digits[((int) (msb >> 32)) & 0xff]);
buf[8] = '-';
ByteArray.setInt(
buf,
9,
(H256[(((int) msb) >> 24) & 0xff] << 16)
| H256[(((int) msb) >> 16) & 0xff]);
(digits[(((int) msb) >> 24) & 0xff] << 16)
| digits[(((int) msb) >> 16) & 0xff]);
buf[13] = '-';
ByteArray.setInt(
buf,
14,
(H256[(((int) msb) >> 8) & 0xff] << 16)
| H256[((int) msb) & 0xff]);
(digits[(((int) msb) >> 8) & 0xff] << 16)
| digits[((int) msb) & 0xff]);
buf[18] = '-';
ByteArray.setInt(
buf,
19,
(H256[(((int) (lsb >> 56))) & 0xff] << 16)
| H256[(((int) (lsb >> 48))) & 0xff]);
(digits[(((int) (lsb >> 56))) & 0xff] << 16)
| digits[(((int) (lsb >> 48))) & 0xff]);
buf[23] = '-';
ByteArray.setLong(
buf,
24,
((long) H256[(((int) (lsb >> 40))) & 0xff] << 48)
| ((long) H256[((int) (lsb >> 32)) & 0xff] << 32)
| ((long) H256[(((int) lsb) >> 24) & 0xff] << 16)
| H256[(((int) lsb) >> 16) & 0xff]);
((long) digits[(((int) (lsb >> 40))) & 0xff] << 48)
| ((long) digits[((int) (lsb >> 32)) & 0xff] << 32)
| ((long) digits[(((int) lsb) >> 24) & 0xff] << 16)
| digits[(((int) lsb) >> 16) & 0xff]);
ByteArray.setInt(
buf,
32,
(H256[(((int) lsb) >> 8) & 0xff] << 16)
| H256[((int) lsb) & 0xff]);
(digits[(((int) lsb) >> 8) & 0xff] << 16)
| digits[((int) lsb) & 0xff]);

return new String(buf, LATIN1);
}

static String fastUUIDUTF16(long lsb, long msb) {
char[] H256 = DigitCache.HEX256;

char i0 = H256[((int) (msb >> 56)) & 0xff];
char i1 = H256[((int) (msb >> 48)) & 0xff];
char i2 = H256[((int) (msb >> 40)) & 0xff];
char i3 = H256[((int) (msb >> 32)) & 0xff];
char i4 = H256[(((int) msb) >> 24) & 0xff];
char i5 = H256[(((int) msb) >> 16) & 0xff];
char i6 = H256[(((int) msb) >> 8) & 0xff];
char i7 = H256[((int) msb) & 0xff];
char i8 = H256[(((int) (lsb >> 56))) & 0xff];
char i9 = H256[(((int) (lsb >> 48))) & 0xff];
char i10 = H256[(((int) (lsb >> 40))) & 0xff];
char i11 = H256[((int) (lsb >> 32)) & 0xff];
char i12 = H256[(((int) lsb) >> 24) & 0xff];
char i13 = H256[(((int) lsb) >> 16) & 0xff];
char i14 = H256[(((int) lsb) >> 8) & 0xff];
char i15 = H256[((int) lsb) & 0xff];
short[] digits = HexDigits.DIGITS;

short i0 = digits[((int) (msb >> 56)) & 0xff];
short i1 = digits[((int) (msb >> 48)) & 0xff];
short i2 = digits[((int) (msb >> 40)) & 0xff];
short i3 = digits[((int) (msb >> 32)) & 0xff];
short i4 = digits[(((int) msb) >> 24) & 0xff];
short i5 = digits[(((int) msb) >> 16) & 0xff];
short i6 = digits[(((int) msb) >> 8) & 0xff];
short i7 = digits[((int) msb) & 0xff];
short i8 = digits[(((int) (lsb >> 56))) & 0xff];
short i9 = digits[(((int) (lsb >> 48))) & 0xff];
short i10 = digits[(((int) (lsb >> 40))) & 0xff];
short i11 = digits[((int) (lsb >> 32)) & 0xff];
short i12 = digits[(((int) lsb) >> 24) & 0xff];
short i13 = digits[(((int) lsb) >> 16) & 0xff];
short i14 = digits[(((int) lsb) >> 8) & 0xff];
short i15 = digits[((int) lsb) & 0xff];

byte[] buf = new byte[72];
int off = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? 1 : 0;
int off = StringUTF16.isBigEndian() ? 1 : 0;

buf[0 + off] = (byte) (i0 >> 8);
buf[2 + off] = (byte) i0;
@@ -1259,20 +1259,6 @@ private LongCache() {}
}
}

static final class DigitCache {
@Stable
static final char[] HEX256;
static {
HEX256 = new char[256];
for (int i = 0; i < 256; i++) {
int hi = (i >> 4) & 15;
int lo = i & 15;
HEX256[i] = (char) (((hi < 10 ? '0' + hi : 'a' + hi - 10) << 8)
+ (lo < 10 ? '0' + lo : 'a' + lo - 10));
}
}
}

/**
* Returns a {@code Long} instance representing the specified
* {@code long} value.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/lang/StringUTF16.java
Original file line number Diff line number Diff line change
@@ -1499,7 +1499,7 @@ public static int lastIndexOfLatin1(byte[] src, int srcCount,

////////////////////////////////////////////////////////////////

private static native boolean isBigEndian();
static native boolean isBigEndian();

static final int HI_BYTE_SHIFT;
static final int LO_BYTE_SHIFT;
20 changes: 2 additions & 18 deletions src/java.base/share/classes/java/util/HexDigits.java
Original file line number Diff line number Diff line change
@@ -29,35 +29,19 @@

import jdk.internal.vm.annotation.Stable;

import static jdk.internal.util.HexDigits.DIGITS;

/**
* Digits class for hexadecimal digits.
*
* @since 21
*/
final class HexDigits implements Digits {
@Stable
private static final short[] DIGITS;

/**
* Singleton instance of HexDigits.
*/
static final Digits INSTANCE = new HexDigits();

static {
short[] digits = new short[16 * 16];

for (int i = 0; i < 16; i++) {
short hi = (short) ((i < 10 ? i + '0' : i - 10 + 'a') << 8);

for (int j = 0; j < 16; j++) {
short lo = (short) (j < 10 ? j + '0' : j - 10 + 'a');
digits[(i << 4) + j] = (short) (hi | lo);
}
}

DIGITS = digits;
}

/**
* Constructor.
*/
52 changes: 52 additions & 0 deletions src/java.base/share/classes/jdk/internal/util/HexDigits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.internal.util;

import jdk.internal.vm.annotation.Stable;

/**
* Digits class for hexadecimal digits.
*
*/
public final class HexDigits {
@Stable
public static final short[] DIGITS;

static {
short[] digits = new short[16 * 16];

for (int i = 0; i < 16; i++) {
short hi = (short) ((i < 10 ? i + '0' : i - 10 + 'a') << 8);

for (int j = 0; j < 16; j++) {
short lo = (short) (j < 10 ? j + '0' : j - 10 + 'a');
digits[(i << 4) + j] = (short) (hi | lo);
}
}

DIGITS = digits;
}
}