Skip to content

Commit

Permalink
Avoid having a ton of empty HashMaps in memory
Browse files Browse the repository at this point in the history
I'm working on a heap dump and Eclipse MAT complained that there was
a lot of empty HashMaps around. I had a closer look and a lot of them
were due to the Map kept in ClassInfo and this one looked like the most
plausible culprit. Now, in Quarkus, the Jandex index is just around
for building the application, but it looks like an easy win.
  • Loading branch information
gsmet authored and Ladicek committed Jun 26, 2024
1 parent 0e74811 commit 8dc2254
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jboss/jandex/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public final Map<DotName, List<AnnotationInstance>> annotationsMap() {
}

final void setAnnotations(Map<DotName, List<AnnotationInstance>> annotations) {
this.annotations = annotations;
this.annotations = Utils.minimize(annotations);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion core/src/main/java/org/jboss/jandex/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,19 @@ static <K, V> Map<K, V[]> unfold(Map<K, List<V>> map, Class<V> listElementType)
return result;
}

static <K, V> Map<K, V> minimize(Map<K, V> map) {
if (map.isEmpty()) {
return Collections.emptyMap();
} else if (map.size() == 1) {
Map.Entry<K, V> entry = map.entrySet().iterator().next();
return Collections.singletonMap(entry.getKey(), entry.getValue());
} else {
return map;
}
}

static <T> List<T> listOfCapacity(int capacity) {
return capacity > 0 ? new ArrayList<T>(capacity) : Collections.<T> emptyList();
return capacity > 0 ? new ArrayList<>(capacity) : Collections.emptyList();
}

static final class ReusableBufferedDataInputStream extends DataInputStream {
Expand Down

0 comments on commit 8dc2254

Please sign in to comment.