Skip to content

Commit 7fa1a51

Browse files
committed
[GR-71114] Make AnnotationValueSupport.declaredAnnotations an LRU cache
PullRequest: graal/22519
2 parents 49abe5c + a76d0c1 commit 7fa1a51

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/annotation/AnnotationValueSupport.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
import jdk.graal.compiler.core.common.LibGraalSupport;
4141
import jdk.graal.compiler.debug.GraalError;
42-
import jdk.graal.compiler.util.EconomicHashMap;
4342
import jdk.vm.ci.meta.ResolvedJavaMethod;
4443
import jdk.vm.ci.meta.ResolvedJavaType;
4544
import jdk.vm.ci.meta.UnresolvedJavaType;
@@ -153,11 +152,24 @@ public static AnnotationValue getAnnotationValue(Annotated annotated, Class<? ex
153152

154153
/**
155154
* Cache for {@link #getAnnotationValue}. Building libgraal-ee shows that this cache grows to
156-
* about 3K entries and there are about 30K accesses so no need to optimize further with an LRU
157-
* cache.
155+
* about 3K entries so the LRU cache is sized just above that (4096). This cache must not grow
156+
* too large as there are Native Image tests that build numerous images in the one JVM process.
158157
*/
159158
@LibGraalSupport.HostedOnly //
160-
private static final Map<Annotated, Map<ResolvedJavaType, AnnotationValue>> declaredAnnotations = LibGraalSupport.INSTANCE == null ? Collections.synchronizedMap(new EconomicHashMap<>()) : null;
159+
private static final Map<Annotated, Map<ResolvedJavaType, AnnotationValue>> declaredAnnotations;
160+
static {
161+
final int cacheMaxSize = 4096;
162+
if (LibGraalSupport.INSTANCE == null) {
163+
declaredAnnotations = Collections.synchronizedMap(new java.util.LinkedHashMap<>(cacheMaxSize, 0.75f, true) {
164+
@Override
165+
protected boolean removeEldestEntry(Map.Entry<Annotated, Map<ResolvedJavaType, AnnotationValue>> eldest) {
166+
return size() > cacheMaxSize;
167+
}
168+
});
169+
} else {
170+
declaredAnnotations = null;
171+
}
172+
}
161173

162174
@LibGraalSupport.HostedOnly
163175
private static AnnotationValue getAnnotationValue0(Annotated annotated, Class<? extends Annotation> annotationType, boolean inherited) {

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/AnnotatedObjectAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ private static Annotated unwrap(Annotated element, List<AnnotationValue> injecte
522522
while (cur instanceof AnnotatedWrapper wrapper) {
523523
if (injectedAnnotationsCollector != null) {
524524
List<AnnotationValue> injectedAnnotations = wrapper.getInjectedAnnotations();
525-
if (injectedAnnotations != null) {
525+
if (!injectedAnnotations.isEmpty()) {
526526
injectedAnnotationsCollector.addAll(injectedAnnotations);
527527
}
528528
}

0 commit comments

Comments
 (0)