Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid having a ton of empty HashMaps in memory #404

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading