Skip to content

Commit

Permalink
8341664: ReferenceClassDescImpl cache internalName
Browse files Browse the repository at this point in the history
Reviewed-by: liach
  • Loading branch information
wenshao committed Oct 18, 2024
1 parent c51a086 commit 0963b9e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,38 +382,6 @@ else if ((state == State.STRING && u.state == State.STRING))
return stringValue().equals(u.stringValue());
}

/**
* Returns if this utf8 entry's content equals a substring
* of {@code s} obtained as {@code s.substring(start, end - start)}.
* This check avoids a substring allocation.
*/
public boolean equalsRegion(String s, int start, int end) {
// start and end values trusted
if (state == State.RAW)
inflate();
int len = charLen;
if (len != end - start)
return false;

var sv = stringValue;
if (sv != null) {
return sv.regionMatches(0, s, start, len);
}

var chars = this.chars;
if (chars != null) {
for (int i = 0; i < len; i++)
if (chars[i] != s.charAt(start + i))
return false;
} else {
var bytes = this.rawBytes;
for (int i = 0; i < len; i++)
if (bytes[offset + i] != s.charAt(start + i))
return false;
}
return true;
}

@Override
public boolean equalsString(String s) {
if (state == State.RAW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,24 +394,6 @@ private AbstractPoolEntry.Utf8EntryImpl tryFindUtf8(int hash, AbstractPoolEntry.
return null;
}

private AbstractPoolEntry.Utf8EntryImpl tryFindUtf8OfRegion(int hash, String target, int start, int end) {
EntryMap map = map();
while (true) {
for (int token = map.firstToken(hash); token != -1; token = map.nextToken(hash, token)) {
PoolEntry e = entryByIndex(map.getIndexByToken(token));
if (e.tag() == TAG_UTF8
&& e instanceof AbstractPoolEntry.Utf8EntryImpl ce
&& ce.equalsRegion(target, start, end))
return ce;
}
if (!doneFullScan) {
fullScan();
continue;
}
return null;
}
}

private AbstractPoolEntry.ClassEntryImpl tryFindClassOrInterface(int hash, ClassDesc cd) {
while (true) {
EntryMap map = map();
Expand All @@ -429,8 +411,7 @@ private AbstractPoolEntry.ClassEntryImpl tryFindClassOrInterface(int hash, Class
}

// no symbol available
var desc = cd.descriptorString();
if (ce.ref1.equalsRegion(desc, 1, desc.length() - 1)) {
if (ce.ref1.equalsString(Util.toInternalName(cd))) {
// definite match, propagate symbol
ce.sym = cd;
return ce;
Expand All @@ -454,10 +435,11 @@ private AbstractPoolEntry.ClassEntryImpl classEntryForClassOrInterface(ClassDesc
if (ce != null)
return ce;

var utfHash = Util.internalNameHash(desc);
var utf = tryFindUtf8OfRegion(AbstractPoolEntry.hashString(utfHash), desc, 1, desc.length() - 1);
String internalName = Util.toInternalName(cd);
var utfHash = internalName.hashCode();
var utf = tryFindUtf8(AbstractPoolEntry.hashString(utfHash), internalName);
if (utf == null)
utf = internalAdd(new AbstractPoolEntry.Utf8EntryImpl(this, size, ConstantUtils.dropFirstAndLastChar(desc), utfHash));
utf = internalAdd(new AbstractPoolEntry.Utf8EntryImpl(this, size, internalName, utfHash));

return internalAdd(new AbstractPoolEntry.ClassEntryImpl(this, size, utf, hash, cd));
}
Expand Down
18 changes: 5 additions & 13 deletions src/java.base/share/classes/jdk/internal/classfile/impl/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.function.Function;

import jdk.internal.access.SharedSecrets;
import jdk.internal.constant.ReferenceClassDescImpl;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Stable;

Expand Down Expand Up @@ -133,10 +134,10 @@ public static String toBinaryName(ClassDesc cd) {
}

public static String toInternalName(ClassDesc cd) {
var desc = cd.descriptorString();
if (desc.charAt(0) == 'L')
return desc.substring(1, desc.length() - 1);
throw new IllegalArgumentException(desc);
if (cd instanceof ReferenceClassDescImpl rcd) {
return rcd.internalName();
}
throw new IllegalArgumentException(cd.descriptorString());
}

public static ClassDesc toClassDesc(String classInternalNameOrArrayDesc) {
Expand Down Expand Up @@ -321,15 +322,6 @@ interface WritableLocalVariable {
boolean writeLocalTo(BufWriterImpl buf);
}

/**
* Returns the hash code of an internal name given the class or interface L descriptor.
*/
public static int internalNameHash(String desc) {
if (desc.length() > 0xffff)
throw new IllegalArgumentException("String too long: ".concat(Integer.toString(desc.length())));
return (desc.hashCode() - pow31(desc.length() - 1) * 'L' - ';') * INVERSE_31;
}

/**
* Returns the hash code of a class or interface L descriptor given the internal name.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.lang.constant.ClassDesc;
import java.lang.invoke.MethodHandles;

import jdk.internal.vm.annotation.Stable;
import static jdk.internal.constant.ConstantUtils.*;

/**
Expand All @@ -36,6 +37,7 @@
*/
public final class ReferenceClassDescImpl implements ClassDesc {
private final String descriptor;
private @Stable String internalName;

private ReferenceClassDescImpl(String descriptor) {
this.descriptor = descriptor;
Expand Down Expand Up @@ -76,6 +78,16 @@ public String descriptorString() {
return descriptor;
}

public String internalName() {
var internalName = this.internalName;
if (internalName == null) {
var desc = this.descriptor;
this.internalName = internalName = desc.charAt(0) == 'L' ? dropFirstAndLastChar(desc) : desc;
}

return internalName;
}

@Override
public Class<?> resolveConstantDesc(MethodHandles.Lookup lookup)
throws ReflectiveOperationException {
Expand All @@ -90,7 +102,7 @@ public Class<?> resolveConstantDesc(MethodHandles.Lookup lookup)
clazz = clazz.arrayType();
return clazz;
}
return lookup.findClass(internalToBinary(dropFirstAndLastChar(descriptor)));
return lookup.findClass(internalToBinary(internalName()));
}

/**
Expand Down
14 changes: 0 additions & 14 deletions test/jdk/jdk/classfile/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,6 @@ void testPow31() {
}
}

@ParameterizedTest
@ValueSource(classes = {
Long.class,
Object.class,
Util.class,
Test.class,
CopyOnWriteArrayList.class,
AtomicReferenceFieldUpdater.class
})
void testInternalNameHash(Class<?> type) {
var cd = type.describeConstable().orElseThrow();
assertEquals(ConstantUtils.binaryToInternal(type.getName()).hashCode(), Util.internalNameHash(cd.descriptorString()));
}

// Ensures the initialization statement of the powers array is filling in the right values
@Test
void testPowersArray() {
Expand Down

1 comment on commit 0963b9e

@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.