Skip to content

Commit 3ea0b8b

Browse files
committed
8300489: Use ArraysSupport.vectorizedHashCode in j.l.CharacterName
Reviewed-by: alanb, naoto
1 parent fcbf9d0 commit 3ea0b8b

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/java.base/share/classes/java/lang/CharacterName.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,12 @@
2525

2626
package java.lang;
2727

28+
import jdk.internal.util.ArraysSupport;
29+
2830
import java.io.DataInputStream;
2931
import java.io.InputStream;
3032
import java.lang.ref.SoftReference;
3133
import java.util.Arrays;
32-
import java.util.Locale;
3334
import java.util.zip.InflaterInputStream;
3435
import java.security.AccessController;
3536
import java.security.PrivilegedAction;
@@ -59,7 +60,7 @@ public InputStream run() {
5960
int bkNum = dis.readInt();
6061
int cpNum = dis.readInt();
6162
int cpEnd = dis.readInt();
62-
byte ba[] = new byte[cpEnd];
63+
byte[] ba = new byte[cpEnd];
6364
lookup = new int[bkNum * 256];
6465
bkIndices = new int[(Character.MAX_CODE_POINT + 1) >> 8];
6566
strPool = new byte[total - cpEnd];
@@ -76,9 +77,9 @@ public InputStream run() {
7677
int bk = -1;
7778
int prevBk = -1; // prev bkNo;
7879
int idx = 0;
79-
int next = -1;
80-
int hash = 0;
81-
int hsh = 0;
80+
int next;
81+
int hash;
82+
int hsh;
8283
do {
8384
int len = ba[cpOff++] & 0xff;
8485
if (len == 0) {
@@ -111,12 +112,8 @@ public InputStream run() {
111112
}
112113
}
113114

114-
private static final int hashN(byte[] a, int off, int len) {
115-
int h = 1;
116-
while (len-- > 0) {
117-
h = 31 * h + a[off++];
118-
}
119-
return h;
115+
private static int hashN(byte[] a, int off, int len) {
116+
return ArraysSupport.vectorizedHashCode(a, off, len, 1, ArraysSupport.T_BYTE);
120117
}
121118

122119
private int addCp(int idx, int hash, int next, int cp) {
@@ -132,7 +129,7 @@ private int addCp(int idx, int hash, int next, int cp) {
132129

133130
public static CharacterName getInstance() {
134131
SoftReference<CharacterName> ref = refCharName;
135-
CharacterName cname = null;
132+
CharacterName cname;
136133
if (ref == null || (cname = ref.get()) == null) {
137134
cname = new CharacterName();
138135
refCharName = new SoftReference<>(cname);
@@ -141,7 +138,7 @@ public static CharacterName getInstance() {
141138
}
142139

143140
public String getName(int cp) {
144-
int off = 0;
141+
int off;
145142
int bk = bkIndices[cp >> 8];
146143
if (bk == -1 || (off = lookup[(bk << 8) + (cp & 0xff)]) == 0)
147144
return null;
@@ -157,7 +154,7 @@ public int getCodePoint(String name) {
157154
while (idx != -1) {
158155
if (getCpHash(idx) == hsh) {
159156
int cp = getCp(idx);
160-
int off = -1;
157+
int off;
161158
int bk = bkIndices[cp >> 8];
162159
if (bk != -1 && (off = lookup[(bk << 8) + (cp & 0xff)]) != 0) {
163160
int len = off & 0xff;

test/micro/org/openjdk/bench/java/lang/Characters.java

+13
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,17 @@ public boolean isUpperCase() {
6767
public boolean isWhitespace() {
6868
return Character.isWhitespace(codePoint);
6969
}
70+
71+
@BenchmarkMode(Mode.AverageTime)
72+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
73+
@Warmup(iterations = 5, time = 1)
74+
@Measurement(iterations = 5, time = 1)
75+
@Fork(3)
76+
public static class CodePoints {
77+
@Benchmark
78+
public void codePointOf() {
79+
Character.codePointOf("Latin Capital Letter B with hook");
80+
}
81+
82+
}
7083
}

0 commit comments

Comments
 (0)