8339876: Move constant symbol caches to Utf8EntryImpl

Reviewed-by: redestad
This commit is contained in:
Chen Liang 2024-09-12 15:16:38 +00:00
parent 0765917dea
commit 4d65c3efca
19 changed files with 87 additions and 113 deletions

View File

@ -34,6 +34,8 @@ import jdk.internal.classfile.impl.TemporaryConstantPool;
import java.lang.constant.ClassDesc;
import java.util.List;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -78,7 +80,7 @@ public sealed interface Annotation
* {@return the annotation interface, as a symbolic descriptor}
*/
default ClassDesc classSymbol() {
return ClassDesc.ofDescriptor(className().stringValue());
return Util.fieldTypeSymbol(className());
}
/**
@ -115,7 +117,7 @@ public sealed interface Annotation
*/
static Annotation of(ClassDesc annotationClass,
List<AnnotationElement> elements) {
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass.descriptorString()), elements);
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass), elements);
}
/**
@ -125,6 +127,6 @@ public sealed interface Annotation
*/
static Annotation of(ClassDesc annotationClass,
AnnotationElement... elements) {
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass.descriptorString()), elements);
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass), elements);
}
}

View File

@ -38,6 +38,8 @@ import java.lang.constant.ClassDesc;
import java.lang.constant.Constable;
import java.util.ArrayList;
import java.util.List;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -405,7 +407,7 @@ public sealed interface AnnotationValue {
/** {@return the class descriptor} */
default ClassDesc classSymbol() {
return ClassDesc.ofDescriptor(className().stringValue());
return Util.fieldTypeSymbol(className());
}
}
@ -423,7 +425,7 @@ public sealed interface AnnotationValue {
/** {@return the enum class descriptor} */
default ClassDesc classSymbol() {
return ClassDesc.ofDescriptor(className().stringValue());
return Util.fieldTypeSymbol(className());
}
/** {@return the enum constant name} */
@ -452,7 +454,7 @@ public sealed interface AnnotationValue {
* @param constantName the name of the enum constant
*/
static OfEnum ofEnum(ClassDesc className, String constantName) {
return ofEnum(TemporaryConstantPool.INSTANCE.utf8Entry(className.descriptorString()),
return ofEnum(TemporaryConstantPool.INSTANCE.utf8Entry(className),
TemporaryConstantPool.INSTANCE.utf8Entry(constantName));
}
@ -469,7 +471,7 @@ public sealed interface AnnotationValue {
* @param className the descriptor of the class
*/
static OfClass ofClass(ClassDesc className) {
return ofClass(TemporaryConstantPool.INSTANCE.utf8Entry(className.descriptorString()));
return ofClass(TemporaryConstantPool.INSTANCE.utf8Entry(className));
}
/**

View File

@ -194,7 +194,9 @@ public sealed interface ClassBuilder
default ClassBuilder withField(String name,
ClassDesc descriptor,
int flags) {
return withField(name, descriptor, Util.buildingFlags(flags));
return withField(constantPool().utf8Entry(name),
constantPool().utf8Entry(descriptor),
flags);
}
/**

View File

@ -769,7 +769,7 @@ public sealed interface CodeBuilder
default CodeBuilder localVariable(int slot, String name, ClassDesc descriptor, Label startScope, Label endScope) {
return localVariable(slot,
constantPool().utf8Entry(name),
constantPool().utf8Entry(descriptor.descriptorString()),
constantPool().utf8Entry(descriptor),
startScope, endScope);
}

View File

@ -31,6 +31,7 @@ import java.util.Optional;
import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.classfile.impl.BufferedFieldBuilder;
import jdk.internal.classfile.impl.FieldImpl;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -59,6 +60,6 @@ public sealed interface FieldModel
/** {@return the field descriptor of this field, as a symbolic descriptor} */
default ClassDesc fieldTypeSymbol() {
return ClassDesc.ofDescriptor(fieldType().stringValue());
return Util.fieldTypeSymbol(fieldType());
}
}

View File

@ -31,6 +31,7 @@ import java.util.Optional;
import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.classfile.impl.BufferedMethodBuilder;
import jdk.internal.classfile.impl.MethodImpl;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -59,7 +60,7 @@ public sealed interface MethodModel
/** {@return the method descriptor of this method, as a symbolic descriptor} */
default MethodTypeDesc methodTypeSymbol() {
return MethodTypeDesc.ofDescriptor(methodType().stringValue());
return Util.methodTypeSymbol(methodType());
}
/** {@return the body of this method, if there is one} */

View File

@ -28,6 +28,7 @@ import java.lang.constant.ClassDesc;
import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.classfile.impl.BoundLocalVariable;
import jdk.internal.classfile.impl.UnboundAttribute;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -65,7 +66,7 @@ public sealed interface LocalVariableInfo
* {@return the field descriptor of the local variable}
*/
default ClassDesc typeSymbol() {
return ClassDesc.ofDescriptor(type().stringValue());
return Util.fieldTypeSymbol(type());
}
/**

View File

@ -33,6 +33,7 @@ import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.classfile.impl.BoundRecordComponentInfo;
import jdk.internal.classfile.impl.TemporaryConstantPool;
import jdk.internal.classfile.impl.UnboundAttribute;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -58,7 +59,7 @@ public sealed interface RecordComponentInfo
* {@return the field descriptor of this component, as a {@linkplain ClassDesc}}
*/
default ClassDesc descriptorSymbol() {
return ClassDesc.ofDescriptor(descriptor().stringValue());
return Util.fieldTypeSymbol(descriptor());
}
/**
@ -95,7 +96,7 @@ public sealed interface RecordComponentInfo
ClassDesc descriptor,
List<Attribute<?>> attributes) {
return new UnboundAttribute.UnboundRecordComponentInfo(TemporaryConstantPool.INSTANCE.utf8Entry(name),
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor.descriptorString()),
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor),
attributes);
}

View File

@ -223,9 +223,7 @@ public sealed interface ConstantPoolBuilder
* @param type the symbolic descriptor for a field type
*/
default NameAndTypeEntry nameAndTypeEntry(String name, ClassDesc type) {
var ret = (NameAndTypeEntryImpl)nameAndTypeEntry(utf8Entry(name), utf8Entry(type.descriptorString()));
ret.typeSym = type;
return ret;
return nameAndTypeEntry(utf8Entry(name), utf8Entry(type));
}
/**
@ -238,9 +236,7 @@ public sealed interface ConstantPoolBuilder
* @param type the symbolic descriptor for a method type
*/
default NameAndTypeEntry nameAndTypeEntry(String name, MethodTypeDesc type) {
var ret = (NameAndTypeEntryImpl)nameAndTypeEntry(utf8Entry(name), utf8Entry(type.descriptorString()));
ret.typeSym = type;
return ret;
return nameAndTypeEntry(utf8Entry(name), utf8Entry(type));
}
/**

View File

@ -36,6 +36,7 @@ import java.lang.constant.ClassDesc;
import jdk.internal.classfile.impl.AbstractPseudoInstruction;
import jdk.internal.classfile.impl.BoundLocalVariable;
import jdk.internal.classfile.impl.TemporaryConstantPool;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
/**
@ -70,7 +71,7 @@ public sealed interface LocalVariable extends PseudoInstruction
* {@return the local variable type, as a symbolic descriptor}
*/
default ClassDesc typeSymbol() {
return ClassDesc.ofDescriptor(type().stringValue());
return Util.fieldTypeSymbol(type());
}
/**
@ -109,7 +110,7 @@ public sealed interface LocalVariable extends PseudoInstruction
static LocalVariable of(int slot, String name, ClassDesc descriptor, Label startScope, Label endScope) {
return of(slot,
TemporaryConstantPool.INSTANCE.utf8Entry(name),
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor.descriptorString()),
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor),
startScope, endScope);
}
}

View File

@ -54,6 +54,7 @@ import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.Stable;
public abstract sealed class AbstractPoolEntry {
/*
@ -146,12 +147,14 @@ public abstract sealed class AbstractPoolEntry {
private final int offset;
private final int rawLen;
// Set in any state other than RAW
private int hash;
private int charLen;
private @Stable int hash;
private @Stable int charLen;
// Set in CHAR state
private char[] chars;
private @Stable char[] chars;
// Only set in STRING state
private String stringValue;
private @Stable String stringValue;
// The descriptor symbol, if this is a descriptor
@Stable TypeDescriptor typeSym;
Utf8EntryImpl(ConstantPool cpm, int index,
byte[] rawBytes, int offset, int rawLen) {
@ -187,6 +190,7 @@ public abstract sealed class AbstractPoolEntry {
this.charLen = u.charLen;
this.chars = u.chars;
this.stringValue = u.stringValue;
this.typeSym = u.typeSym;
}
/**
@ -419,6 +423,22 @@ public abstract sealed class AbstractPoolEntry {
pool.writeUTF(stringValue);
}
}
public ClassDesc fieldTypeSymbol() {
if (typeSym instanceof ClassDesc cd)
return cd;
var ret = ClassDesc.ofDescriptor(stringValue());
typeSym = ret;
return ret;
}
public MethodTypeDesc methodTypeSymbol() {
if (typeSym instanceof MethodTypeDesc mtd)
return mtd;
var ret = MethodTypeDesc.ofDescriptor(stringValue());
typeSym = ret;
return ret;
}
}
abstract static sealed class AbstractRefEntry<T extends PoolEntry> extends AbstractPoolEntry {
@ -585,8 +605,6 @@ public abstract sealed class AbstractPoolEntry {
public static final class NameAndTypeEntryImpl extends AbstractRefsEntry<Utf8EntryImpl, Utf8EntryImpl>
implements NameAndTypeEntry {
public TypeDescriptor typeSym = null;
NameAndTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name, Utf8EntryImpl type) {
super(cpm, ClassFile.TAG_NAMEANDTYPE, index, name, type);
}
@ -601,31 +619,12 @@ public abstract sealed class AbstractPoolEntry {
return ref2;
}
public ClassDesc fieldTypeSymbol() {
if (typeSym instanceof ClassDesc cd) {
return cd;
} else {
return (ClassDesc)(typeSym = ClassDesc.ofDescriptor(ref2.stringValue()));
}
}
public MethodTypeDesc methodTypeSymbol() {
if (typeSym instanceof MethodTypeDesc mtd) {
return mtd;
} else {
return (MethodTypeDesc)(typeSym = MethodTypeDesc.ofDescriptor(ref2.stringValue()));
}
}
@Override
public NameAndTypeEntry clone(ConstantPoolBuilder cp) {
if (cp.canWriteDirect(constantPool)) {
return this;
} else {
var ret = (NameAndTypeEntryImpl)cp.nameAndTypeEntry(ref1, ref2);
ret.typeSym = typeSym;
return ret;
}
return cp.nameAndTypeEntry(ref1, ref2);
}
@Override
@ -897,8 +896,6 @@ public abstract sealed class AbstractPoolEntry {
extends AbstractRefEntry<Utf8EntryImpl>
implements MethodTypeEntry {
public MethodTypeDesc sym = null;
MethodTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl descriptor) {
super(cpm, ClassFile.TAG_METHODTYPE, index, descriptor);
}
@ -912,20 +909,13 @@ public abstract sealed class AbstractPoolEntry {
public MethodTypeEntry clone(ConstantPoolBuilder cp) {
if (cp.canWriteDirect(constantPool)) {
return this;
} else {
var ret = (MethodTypeEntryImpl)cp.methodTypeEntry(ref1);
ret.sym = sym;
return ret;
}
return cp.methodTypeEntry(ref1);
}
@Override
public MethodTypeDesc asSymbol() {
var sym = this.sym;
if (sym != null) {
return sym;
}
return this.sym = MethodTypeDesc.ofDescriptor(descriptor().stringValue());
return ref1.methodTypeSymbol();
}
@Override

View File

@ -46,7 +46,7 @@ public final class BoundLocalVariable
@Override
public ClassDesc typeSymbol() {
return ClassDesc.ofDescriptor(type().stringValue());
return Util.fieldTypeSymbol(type());
}
@Override

View File

@ -53,7 +53,6 @@ public final class BufferedMethodBuilder
private AccessFlags flags;
private final MethodModel original;
private int[] parameterSlots;
MethodTypeDesc mDesc;
public BufferedMethodBuilder(SplitConstantPool constantPool,
ClassFileImpl context,
@ -102,14 +101,7 @@ public final class BufferedMethodBuilder
@Override
public MethodTypeDesc methodTypeSymbol() {
if (mDesc == null) {
if (original instanceof MethodInfo mi) {
mDesc = mi.methodTypeSymbol();
} else {
mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue());
}
}
return mDesc;
return Util.methodTypeSymbol(methodType());
}
@Override

View File

@ -79,15 +79,6 @@ public final class ChainedClassBuilder
return this;
}
@Override
public ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int flags, Consumer<? super MethodBuilder> handler) {
var mb = new BufferedMethodBuilder(terminal.constantPool, terminal.context,
constantPool().utf8Entry(name), constantPool().utf8Entry(descriptor), flags, null);
mb.mDesc = descriptor;
consumer.accept(mb.run(handler).toModel());
return this;
}
@Override
public ClassBuilder transformMethod(MethodModel method, MethodTransform transform) {
BufferedMethodBuilder builder = new BufferedMethodBuilder(terminal.constantPool, terminal.context,

View File

@ -90,14 +90,6 @@ public final class DirectClassBuilder
return this;
}
@Override
public ClassBuilder withField(String name,
ClassDesc descriptor,
int flags) {
return withField(new DirectFieldBuilder(constantPool, context,
constantPool.utf8Entry(name), constantPool.utf8Entry(descriptor), flags, null));
}
@Override
public ClassBuilder withField(Utf8Entry name,
Utf8Entry descriptor,
@ -130,13 +122,6 @@ public final class DirectClassBuilder
.run(handler));
}
@Override
public ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int flags, Consumer<? super MethodBuilder> handler) {
var method = new DirectMethodBuilder(constantPool, context, constantPool.utf8Entry(name), constantPool.utf8Entry(descriptor), flags, null);
method.mDesc = descriptor;
return withMethod(method.run(handler));
}
@Override
public ClassBuilder transformMethod(MethodModel method, MethodTransform transform) {
DirectMethodBuilder builder = new DirectMethodBuilder(constantPool, context, method.methodName(),

View File

@ -46,7 +46,6 @@ public final class DirectMethodBuilder
final Utf8Entry desc;
int flags;
int[] parameterSlots;
MethodTypeDesc mDesc;
public DirectMethodBuilder(SplitConstantPool constantPool,
ClassFileImpl context,
@ -87,14 +86,7 @@ public final class DirectMethodBuilder
@Override
public MethodTypeDesc methodTypeSymbol() {
if (mDesc == null) {
if (original instanceof MethodInfo mi) {
mDesc = mi.methodTypeSymbol();
} else {
mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue());
}
}
return mDesc;
return Util.methodTypeSymbol(methodType());
}
@Override

View File

@ -41,7 +41,6 @@ public final class MethodImpl
private final int startPos, endPos, attributesPos;
private List<Attribute<?>> attributes;
private int[] parameterSlots;
private MethodTypeDesc mDesc;
public MethodImpl(ClassReader reader, int startPos, int endPos, int attrStart) {
this.reader = reader;
@ -75,10 +74,7 @@ public final class MethodImpl
@Override
public MethodTypeDesc methodTypeSymbol() {
if (mDesc == null) {
mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue());
}
return mDesc;
return Util.methodTypeSymbol(methodType());
}
@Override

View File

@ -24,7 +24,7 @@
*/
package jdk.internal.classfile.impl;
import java.lang.constant.ConstantDesc;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.util.Arrays;
import java.util.List;
@ -398,6 +398,20 @@ public final class SplitConstantPool implements ConstantPoolBuilder {
return null;
}
@Override
public Utf8Entry utf8Entry(ClassDesc desc) {
var utf8 = utf8Entry(desc.descriptorString());
utf8.typeSym = desc;
return utf8;
}
@Override
public Utf8Entry utf8Entry(MethodTypeDesc desc) {
var utf8 = utf8Entry(desc.descriptorString());
utf8.typeSym = desc;
return utf8;
}
@Override
public AbstractPoolEntry.Utf8EntryImpl utf8Entry(String s) {
int hash = AbstractPoolEntry.hashString(s.hashCode());
@ -480,9 +494,7 @@ public final class SplitConstantPool implements ConstantPoolBuilder {
@Override
public MethodTypeEntry methodTypeEntry(MethodTypeDesc descriptor) {
var ret = (AbstractPoolEntry.MethodTypeEntryImpl)methodTypeEntry(utf8Entry(descriptor.descriptorString()));
ret.sym = descriptor;
return ret;
return methodTypeEntry(utf8Entry(descriptor));
}
@Override

View File

@ -30,6 +30,7 @@ import java.lang.classfile.FieldBuilder;
import java.lang.classfile.MethodBuilder;
import java.lang.classfile.PseudoInstruction;
import java.lang.classfile.constantpool.PoolEntry;
import java.lang.classfile.constantpool.Utf8Entry;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.util.AbstractList;
@ -220,12 +221,20 @@ public class Util {
return (flag.mask() & flagsMask) == flag.mask() && flag.locations().contains(location);
}
public static ClassDesc fieldTypeSymbol(Utf8Entry utf8) {
return ((AbstractPoolEntry.Utf8EntryImpl) utf8).fieldTypeSymbol();
}
public static MethodTypeDesc methodTypeSymbol(Utf8Entry utf8) {
return ((AbstractPoolEntry.Utf8EntryImpl) utf8).methodTypeSymbol();
}
public static ClassDesc fieldTypeSymbol(NameAndTypeEntry nat) {
return ((AbstractPoolEntry.NameAndTypeEntryImpl)nat).fieldTypeSymbol();
return fieldTypeSymbol(nat.type());
}
public static MethodTypeDesc methodTypeSymbol(NameAndTypeEntry nat) {
return ((AbstractPoolEntry.NameAndTypeEntryImpl)nat).methodTypeSymbol();
return methodTypeSymbol(nat.type());
}
@SuppressWarnings("unchecked")