Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 1dc7929

Browse files
committedJun 10, 2020
8247269: JFR: Reduce allocation when using AnnotationElement
Reviewed-by: mgronlun
1 parent 19be497 commit 1dc7929

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed
 

‎src/jdk.jfr/share/classes/jdk/jfr/AnnotationElement.java

+17-31
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@
6868
public final class AnnotationElement {
6969
private final Type type;
7070
private final List<Object> annotationValues;
71-
private final List<String> annotationNames;
7271
private final boolean inBootClassLoader;
7372

7473
// package private
7574
AnnotationElement(Type type, List<Object> objects, boolean boot) {
7675
Objects.requireNonNull(type);
7776
Objects.requireNonNull(objects);
7877
this.type = type;
79-
if (objects.size() != type.getFields().size()) {
78+
List<ValueDescriptor> fields = type.getFields();
79+
int fieldCount = fields.size();
80+
if (objects.size() != fieldCount) {
8081
StringJoiner descriptors = new StringJoiner(",", "[", "]");
8182
for (ValueDescriptor v : type.getFields()) {
8283
descriptors.add(v.getName());
@@ -88,25 +89,18 @@ public final class AnnotationElement {
8889
throw new IllegalArgumentException("Annotation " + descriptors + " for " + type.getName() + " doesn't match number of values " + values);
8990
}
9091

91-
List<String> n = new ArrayList<>();
92-
List<Object> v = new ArrayList<>();
93-
int index = 0;
94-
for (ValueDescriptor valueDescriptor : type.getFields()) {
92+
for (int index = 0; index < fieldCount; index++) {
9593
Object object = objects.get(index);
9694
if (object == null) {
9795
throw new IllegalArgumentException("Annotation value can't be null");
9896
}
9997
Class<?> valueType = object.getClass();
100-
if (valueDescriptor.isArray()) {
98+
if (fields.get(index).isArray()) {
10199
valueType = valueType.getComponentType();
102100
}
103101
checkType(Utils.unboxType(valueType));
104-
n.add(valueDescriptor.getName());
105-
v.add(object);
106-
index++;
107102
}
108-
this.annotationValues = Utils.smallUnmodifiable(v);
109-
this.annotationNames = Utils.smallUnmodifiable(n);
103+
this.annotationValues = Utils.smallUnmodifiable(objects);
110104
this.inBootClassLoader = boot;
111105
}
112106

@@ -162,9 +156,8 @@ public AnnotationElement(Class<? extends Annotation> annotationType, Map<String,
162156
if (methods.length != map.size()) {
163157
throw new IllegalArgumentException("Number of declared methods must match size of value map");
164158
}
165-
List<String> n = new ArrayList<>();
166-
List<Object> v = new ArrayList<>();
167-
Set<String> nameSet = new HashSet<>();
159+
List<Object> v = new ArrayList<>(methods.length);
160+
Set<String> nameSet = methods.length > 1 ? new HashSet<String>() : null;
168161
for (Method method : methods) {
169162
String fieldName = method.getName();
170163
Object object = map.get(fieldName);
@@ -198,18 +191,19 @@ public AnnotationElement(Class<? extends Annotation> annotationType, Map<String,
198191
fieldType = Utils.unboxType(object.getClass());
199192
checkType(fieldType);
200193
}
201-
if (nameSet.contains(fieldName)) {
202-
throw new IllegalArgumentException("Value with name '" + fieldName + "' already exists");
194+
if (nameSet!= null) {
195+
if (nameSet.contains(fieldName)) {
196+
throw new IllegalArgumentException("Value with name '" + fieldName + "' already exists");
197+
}
198+
nameSet.add(fieldName);
203199
}
204200
if (isKnownJFRAnnotation(annotationType)) {
205201
ValueDescriptor vd = new ValueDescriptor(fieldType, fieldName, Collections.emptyList(), true);
206202
type.add(vd);
207203
}
208-
n.add(fieldName);
209204
v.add(object);
210205
}
211206
this.annotationValues = Utils.smallUnmodifiable(v);
212-
this.annotationNames = Utils.smallUnmodifiable(n);
213207
this.inBootClassLoader = annotationType.getClassLoader() == null;
214208
}
215209

@@ -314,12 +308,9 @@ public String getTypeName() {
314308
*/
315309
public Object getValue(String name) {
316310
Objects.requireNonNull(name);
317-
int index = 0;
318-
for (String n : annotationNames) {
319-
if (name.equals(n)) {
320-
return annotationValues.get(index);
321-
}
322-
index++;
311+
int index = type.indexOf(name);
312+
if (index != -1) {
313+
return annotationValues.get(index);
323314
}
324315
StringJoiner valueNames = new StringJoiner(",", "[", "]");
325316
for (ValueDescriptor v : type.getFields()) {
@@ -339,12 +330,7 @@ public Object getValue(String name) {
339330
*/
340331
public boolean hasValue(String name) {
341332
Objects.requireNonNull(name);
342-
for (String n : annotationNames) {
343-
if (name.equals(n)) {
344-
return true;
345-
}
346-
}
347-
return false;
333+
return type.indexOf(name) != -1;
348334
}
349335

350336
/**

‎src/jdk.jfr/share/classes/jdk/jfr/internal/Type.java

+9
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ public void add(ValueDescriptor valueDescriptor) {
223223
fields.add(valueDescriptor);
224224
}
225225

226+
public int indexOf(String name) {
227+
for (int i = 0; i < fields.size(); i++) {
228+
if (name.equals(fields.get(i).getName())) {
229+
return i;
230+
}
231+
}
232+
return -1;
233+
}
234+
226235
void trimFields() {
227236
getFields();
228237
}

0 commit comments

Comments
 (0)
This repository has been archived.