Skip to content

Commit

Permalink
Avoid empty array allocations in AnnotationTypeMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
kilink authored and sdeleuze committed Sep 11, 2024
1 parent 2b9b581 commit 7827188
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7827188

Please sign in to comment.