Skip to content

Commit 4d65c3e

Browse files
committedSep 12, 2024
8339876: Move constant symbol caches to Utf8EntryImpl
Reviewed-by: redestad
1 parent 0765917 commit 4d65c3e

19 files changed

+87
-113
lines changed
 

‎src/java.base/share/classes/java/lang/classfile/Annotation.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import java.lang.constant.ClassDesc;
3636
import java.util.List;
37+
38+
import jdk.internal.classfile.impl.Util;
3739
import jdk.internal.javac.PreviewFeature;
3840

3941
/**
@@ -78,7 +80,7 @@ public sealed interface Annotation
7880
* {@return the annotation interface, as a symbolic descriptor}
7981
*/
8082
default ClassDesc classSymbol() {
81-
return ClassDesc.ofDescriptor(className().stringValue());
83+
return Util.fieldTypeSymbol(className());
8284
}
8385

8486
/**
@@ -115,7 +117,7 @@ static Annotation of(Utf8Entry annotationClass,
115117
*/
116118
static Annotation of(ClassDesc annotationClass,
117119
List<AnnotationElement> elements) {
118-
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass.descriptorString()), elements);
120+
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass), elements);
119121
}
120122

121123
/**
@@ -125,6 +127,6 @@ static Annotation of(ClassDesc annotationClass,
125127
*/
126128
static Annotation of(ClassDesc annotationClass,
127129
AnnotationElement... elements) {
128-
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass.descriptorString()), elements);
130+
return of(TemporaryConstantPool.INSTANCE.utf8Entry(annotationClass), elements);
129131
}
130132
}

‎src/java.base/share/classes/java/lang/classfile/AnnotationValue.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.lang.constant.Constable;
3939
import java.util.ArrayList;
4040
import java.util.List;
41+
42+
import jdk.internal.classfile.impl.Util;
4143
import jdk.internal.javac.PreviewFeature;
4244

4345
/**
@@ -405,7 +407,7 @@ sealed interface OfClass extends AnnotationValue
405407

406408
/** {@return the class descriptor} */
407409
default ClassDesc classSymbol() {
408-
return ClassDesc.ofDescriptor(className().stringValue());
410+
return Util.fieldTypeSymbol(className());
409411
}
410412
}
411413

@@ -423,7 +425,7 @@ sealed interface OfEnum extends AnnotationValue
423425

424426
/** {@return the enum class descriptor} */
425427
default ClassDesc classSymbol() {
426-
return ClassDesc.ofDescriptor(className().stringValue());
428+
return Util.fieldTypeSymbol(className());
427429
}
428430

429431
/** {@return the enum constant name} */
@@ -452,7 +454,7 @@ static OfEnum ofEnum(Utf8Entry className,
452454
* @param constantName the name of the enum constant
453455
*/
454456
static OfEnum ofEnum(ClassDesc className, String constantName) {
455-
return ofEnum(TemporaryConstantPool.INSTANCE.utf8Entry(className.descriptorString()),
457+
return ofEnum(TemporaryConstantPool.INSTANCE.utf8Entry(className),
456458
TemporaryConstantPool.INSTANCE.utf8Entry(constantName));
457459
}
458460

@@ -469,7 +471,7 @@ static OfClass ofClass(Utf8Entry className) {
469471
* @param className the descriptor of the class
470472
*/
471473
static OfClass ofClass(ClassDesc className) {
472-
return ofClass(TemporaryConstantPool.INSTANCE.utf8Entry(className.descriptorString()));
474+
return ofClass(TemporaryConstantPool.INSTANCE.utf8Entry(className));
473475
}
474476

475477
/**

‎src/java.base/share/classes/java/lang/classfile/ClassBuilder.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ default ClassBuilder withField(String name,
194194
default ClassBuilder withField(String name,
195195
ClassDesc descriptor,
196196
int flags) {
197-
return withField(name, descriptor, Util.buildingFlags(flags));
197+
return withField(constantPool().utf8Entry(name),
198+
constantPool().utf8Entry(descriptor),
199+
flags);
198200
}
199201

200202
/**

‎src/java.base/share/classes/java/lang/classfile/CodeBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ default CodeBuilder localVariable(int slot, Utf8Entry nameEntry, Utf8Entry descr
769769
default CodeBuilder localVariable(int slot, String name, ClassDesc descriptor, Label startScope, Label endScope) {
770770
return localVariable(slot,
771771
constantPool().utf8Entry(name),
772-
constantPool().utf8Entry(descriptor.descriptorString()),
772+
constantPool().utf8Entry(descriptor),
773773
startScope, endScope);
774774
}
775775

‎src/java.base/share/classes/java/lang/classfile/FieldModel.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.lang.classfile.constantpool.Utf8Entry;
3232
import jdk.internal.classfile.impl.BufferedFieldBuilder;
3333
import jdk.internal.classfile.impl.FieldImpl;
34+
import jdk.internal.classfile.impl.Util;
3435
import jdk.internal.javac.PreviewFeature;
3536

3637
/**
@@ -59,6 +60,6 @@ public sealed interface FieldModel
5960

6061
/** {@return the field descriptor of this field, as a symbolic descriptor} */
6162
default ClassDesc fieldTypeSymbol() {
62-
return ClassDesc.ofDescriptor(fieldType().stringValue());
63+
return Util.fieldTypeSymbol(fieldType());
6364
}
6465
}

‎src/java.base/share/classes/java/lang/classfile/MethodModel.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.lang.classfile.constantpool.Utf8Entry;
3232
import jdk.internal.classfile.impl.BufferedMethodBuilder;
3333
import jdk.internal.classfile.impl.MethodImpl;
34+
import jdk.internal.classfile.impl.Util;
3435
import jdk.internal.javac.PreviewFeature;
3536

3637
/**
@@ -59,7 +60,7 @@ public sealed interface MethodModel
5960

6061
/** {@return the method descriptor of this method, as a symbolic descriptor} */
6162
default MethodTypeDesc methodTypeSymbol() {
62-
return MethodTypeDesc.ofDescriptor(methodType().stringValue());
63+
return Util.methodTypeSymbol(methodType());
6364
}
6465

6566
/** {@return the body of this method, if there is one} */

‎src/java.base/share/classes/java/lang/classfile/attribute/LocalVariableInfo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.lang.classfile.constantpool.Utf8Entry;
2929
import jdk.internal.classfile.impl.BoundLocalVariable;
3030
import jdk.internal.classfile.impl.UnboundAttribute;
31+
import jdk.internal.classfile.impl.Util;
3132
import jdk.internal.javac.PreviewFeature;
3233

3334
/**
@@ -65,7 +66,7 @@ public sealed interface LocalVariableInfo
6566
* {@return the field descriptor of the local variable}
6667
*/
6768
default ClassDesc typeSymbol() {
68-
return ClassDesc.ofDescriptor(type().stringValue());
69+
return Util.fieldTypeSymbol(type());
6970
}
7071

7172
/**

‎src/java.base/share/classes/java/lang/classfile/attribute/RecordComponentInfo.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import jdk.internal.classfile.impl.BoundRecordComponentInfo;
3434
import jdk.internal.classfile.impl.TemporaryConstantPool;
3535
import jdk.internal.classfile.impl.UnboundAttribute;
36+
import jdk.internal.classfile.impl.Util;
3637
import jdk.internal.javac.PreviewFeature;
3738

3839
/**
@@ -58,7 +59,7 @@ public sealed interface RecordComponentInfo
5859
* {@return the field descriptor of this component, as a {@linkplain ClassDesc}}
5960
*/
6061
default ClassDesc descriptorSymbol() {
61-
return ClassDesc.ofDescriptor(descriptor().stringValue());
62+
return Util.fieldTypeSymbol(descriptor());
6263
}
6364

6465
/**
@@ -95,7 +96,7 @@ static RecordComponentInfo of(String name,
9596
ClassDesc descriptor,
9697
List<Attribute<?>> attributes) {
9798
return new UnboundAttribute.UnboundRecordComponentInfo(TemporaryConstantPool.INSTANCE.utf8Entry(name),
98-
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor.descriptorString()),
99+
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor),
99100
attributes);
100101
}
101102

‎src/java.base/share/classes/java/lang/classfile/constantpool/ConstantPoolBuilder.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ default ModuleEntry moduleEntry(ModuleDesc moduleDesc) {
223223
* @param type the symbolic descriptor for a field type
224224
*/
225225
default NameAndTypeEntry nameAndTypeEntry(String name, ClassDesc type) {
226-
var ret = (NameAndTypeEntryImpl)nameAndTypeEntry(utf8Entry(name), utf8Entry(type.descriptorString()));
227-
ret.typeSym = type;
228-
return ret;
226+
return nameAndTypeEntry(utf8Entry(name), utf8Entry(type));
229227
}
230228

231229
/**
@@ -238,9 +236,7 @@ default NameAndTypeEntry nameAndTypeEntry(String name, ClassDesc type) {
238236
* @param type the symbolic descriptor for a method type
239237
*/
240238
default NameAndTypeEntry nameAndTypeEntry(String name, MethodTypeDesc type) {
241-
var ret = (NameAndTypeEntryImpl)nameAndTypeEntry(utf8Entry(name), utf8Entry(type.descriptorString()));
242-
ret.typeSym = type;
243-
return ret;
239+
return nameAndTypeEntry(utf8Entry(name), utf8Entry(type));
244240
}
245241

246242
/**

‎src/java.base/share/classes/java/lang/classfile/instruction/LocalVariable.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import jdk.internal.classfile.impl.AbstractPseudoInstruction;
3737
import jdk.internal.classfile.impl.BoundLocalVariable;
3838
import jdk.internal.classfile.impl.TemporaryConstantPool;
39+
import jdk.internal.classfile.impl.Util;
3940
import jdk.internal.javac.PreviewFeature;
4041

4142
/**
@@ -70,7 +71,7 @@ public sealed interface LocalVariable extends PseudoInstruction
7071
* {@return the local variable type, as a symbolic descriptor}
7172
*/
7273
default ClassDesc typeSymbol() {
73-
return ClassDesc.ofDescriptor(type().stringValue());
74+
return Util.fieldTypeSymbol(type());
7475
}
7576

7677
/**
@@ -109,7 +110,7 @@ static LocalVariable of(int slot, Utf8Entry nameEntry, Utf8Entry descriptorEntry
109110
static LocalVariable of(int slot, String name, ClassDesc descriptor, Label startScope, Label endScope) {
110111
return of(slot,
111112
TemporaryConstantPool.INSTANCE.utf8Entry(name),
112-
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor.descriptorString()),
113+
TemporaryConstantPool.INSTANCE.utf8Entry(descriptor),
113114
startScope, endScope);
114115
}
115116
}

‎src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java

+27-37
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import jdk.internal.access.JavaLangAccess;
5555
import jdk.internal.access.SharedSecrets;
5656
import jdk.internal.util.ArraysSupport;
57+
import jdk.internal.vm.annotation.Stable;
5758

5859
public abstract sealed class AbstractPoolEntry {
5960
/*
@@ -146,12 +147,14 @@ enum State { RAW, BYTE, CHAR, STRING }
146147
private final int offset;
147148
private final int rawLen;
148149
// Set in any state other than RAW
149-
private int hash;
150-
private int charLen;
150+
private @Stable int hash;
151+
private @Stable int charLen;
151152
// Set in CHAR state
152-
private char[] chars;
153+
private @Stable char[] chars;
153154
// Only set in STRING state
154-
private String stringValue;
155+
private @Stable String stringValue;
156+
// The descriptor symbol, if this is a descriptor
157+
@Stable TypeDescriptor typeSym;
155158

156159
Utf8EntryImpl(ConstantPool cpm, int index,
157160
byte[] rawBytes, int offset, int rawLen) {
@@ -187,6 +190,7 @@ enum State { RAW, BYTE, CHAR, STRING }
187190
this.charLen = u.charLen;
188191
this.chars = u.chars;
189192
this.stringValue = u.stringValue;
193+
this.typeSym = u.typeSym;
190194
}
191195

192196
/**
@@ -419,6 +423,22 @@ void writeTo(BufWriterImpl pool) {
419423
pool.writeUTF(stringValue);
420424
}
421425
}
426+
427+
public ClassDesc fieldTypeSymbol() {
428+
if (typeSym instanceof ClassDesc cd)
429+
return cd;
430+
var ret = ClassDesc.ofDescriptor(stringValue());
431+
typeSym = ret;
432+
return ret;
433+
}
434+
435+
public MethodTypeDesc methodTypeSymbol() {
436+
if (typeSym instanceof MethodTypeDesc mtd)
437+
return mtd;
438+
var ret = MethodTypeDesc.ofDescriptor(stringValue());
439+
typeSym = ret;
440+
return ret;
441+
}
422442
}
423443

424444
abstract static sealed class AbstractRefEntry<T extends PoolEntry> extends AbstractPoolEntry {
@@ -585,8 +605,6 @@ public boolean equals(Object o) {
585605
public static final class NameAndTypeEntryImpl extends AbstractRefsEntry<Utf8EntryImpl, Utf8EntryImpl>
586606
implements NameAndTypeEntry {
587607

588-
public TypeDescriptor typeSym = null;
589-
590608
NameAndTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name, Utf8EntryImpl type) {
591609
super(cpm, ClassFile.TAG_NAMEANDTYPE, index, name, type);
592610
}
@@ -601,31 +619,12 @@ public Utf8Entry type() {
601619
return ref2;
602620
}
603621

604-
public ClassDesc fieldTypeSymbol() {
605-
if (typeSym instanceof ClassDesc cd) {
606-
return cd;
607-
} else {
608-
return (ClassDesc)(typeSym = ClassDesc.ofDescriptor(ref2.stringValue()));
609-
}
610-
}
611-
612-
public MethodTypeDesc methodTypeSymbol() {
613-
if (typeSym instanceof MethodTypeDesc mtd) {
614-
return mtd;
615-
} else {
616-
return (MethodTypeDesc)(typeSym = MethodTypeDesc.ofDescriptor(ref2.stringValue()));
617-
}
618-
}
619-
620622
@Override
621623
public NameAndTypeEntry clone(ConstantPoolBuilder cp) {
622624
if (cp.canWriteDirect(constantPool)) {
623625
return this;
624-
} else {
625-
var ret = (NameAndTypeEntryImpl)cp.nameAndTypeEntry(ref1, ref2);
626-
ret.typeSym = typeSym;
627-
return ret;
628626
}
627+
return cp.nameAndTypeEntry(ref1, ref2);
629628
}
630629

631630
@Override
@@ -897,8 +896,6 @@ public static final class MethodTypeEntryImpl
897896
extends AbstractRefEntry<Utf8EntryImpl>
898897
implements MethodTypeEntry {
899898

900-
public MethodTypeDesc sym = null;
901-
902899
MethodTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl descriptor) {
903900
super(cpm, ClassFile.TAG_METHODTYPE, index, descriptor);
904901
}
@@ -912,20 +909,13 @@ public Utf8Entry descriptor() {
912909
public MethodTypeEntry clone(ConstantPoolBuilder cp) {
913910
if (cp.canWriteDirect(constantPool)) {
914911
return this;
915-
} else {
916-
var ret = (MethodTypeEntryImpl)cp.methodTypeEntry(ref1);
917-
ret.sym = sym;
918-
return ret;
919912
}
913+
return cp.methodTypeEntry(ref1);
920914
}
921915

922916
@Override
923917
public MethodTypeDesc asSymbol() {
924-
var sym = this.sym;
925-
if (sym != null) {
926-
return sym;
927-
}
928-
return this.sym = MethodTypeDesc.ofDescriptor(descriptor().stringValue());
918+
return ref1.methodTypeSymbol();
929919
}
930920

931921
@Override

‎src/java.base/share/classes/jdk/internal/classfile/impl/BoundLocalVariable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Utf8Entry type() {
4646

4747
@Override
4848
public ClassDesc typeSymbol() {
49-
return ClassDesc.ofDescriptor(type().stringValue());
49+
return Util.fieldTypeSymbol(type());
5050
}
5151

5252
@Override

‎src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public final class BufferedMethodBuilder
5353
private AccessFlags flags;
5454
private final MethodModel original;
5555
private int[] parameterSlots;
56-
MethodTypeDesc mDesc;
5756

5857
public BufferedMethodBuilder(SplitConstantPool constantPool,
5958
ClassFileImpl context,
@@ -102,14 +101,7 @@ public Utf8Entry methodType() {
102101

103102
@Override
104103
public MethodTypeDesc methodTypeSymbol() {
105-
if (mDesc == null) {
106-
if (original instanceof MethodInfo mi) {
107-
mDesc = mi.methodTypeSymbol();
108-
} else {
109-
mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue());
110-
}
111-
}
112-
return mDesc;
104+
return Util.methodTypeSymbol(methodType());
113105
}
114106

115107
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.