Skip to content

Commit

Permalink
Merge pull request #404 from gsmet/rightsize-maps
Browse files Browse the repository at this point in the history
Avoid having a ton of empty HashMaps in memory
  • Loading branch information
Ladicek authored Jun 26, 2024
2 parents 9563819 + 8dc2254 commit 1352479
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 1352479

Please sign in to comment.