From 66b57413ed12ed24194f6bc74ea7f23299d01cae Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Thu, 5 Sep 2024 20:52:35 -0700 Subject: [PATCH] Avoid empty array allocations in AnnotationTypeMapping Avoid empty array allocations in a few places in AnnotationTypeMapping. --- .../core/annotation/AnnotationTypeMapping.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java index e407b42fa8a1..baef1f5a4eb8 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java @@ -69,6 +69,8 @@ final class AnnotationTypeMapping { private static final MirrorSet[] EMPTY_MIRROR_SETS = new MirrorSet[0]; + private static final int[] EMPTY_INT_ARRAY = new int[0]; + @Nullable private final AnnotationTypeMapping source; @@ -606,6 +608,9 @@ boolean isSynthesizable() { private static int[] filledIntArray(int size) { + if (size == 0) { + return EMPTY_INT_ARRAY; + } int[] array = new int[size]; Arrays.fill(array, -1); return array; @@ -684,7 +689,7 @@ class MirrorSets { private final MirrorSet[] assigned; MirrorSets() { - this.assigned = new MirrorSet[attributes.size()]; + this.assigned = attributes.size() > 0 ? new MirrorSet[attributes.size()] : EMPTY_MIRROR_SETS; this.mirrorSets = EMPTY_MIRROR_SETS; } @@ -728,6 +733,9 @@ MirrorSet getAssigned(int attributeIndex) { } int[] resolve(@Nullable Object source, @Nullable Object annotation, ValueExtractor valueExtractor) { + if (attributes.size() == 0) { + return EMPTY_INT_ARRAY; + } int[] result = new int[attributes.size()]; for (int i = 0; i < result.length; i++) { result[i] = i;