Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
}
};

private final Map<String, T> nameToConstant = new HashMap<>();
private final Map<String, T> stringToConstant = new HashMap<>();
private final Map<T, String> constantToName = new HashMap<>();
private final Map<String, T> nameToConstant;
private final Map<String, T> stringToConstant;
private final Map<T, String> constantToName;

private EnumTypeAdapter(Class<T> classOfT) {
try {
Expand All @@ -69,7 +69,12 @@ private EnumTypeAdapter(Class<T> classOfT) {

// Trim the array to the new length. Every enum type can be expected to have at least
// one declared field which is not an enum constant, namely the implicit $VALUES array
fields = Arrays.copyOf(fields, constantCount);
fields = (constantCount == fields.length) ? fields : Arrays.copyOf(fields, constantCount);

nameToConstant = new HashMap<>(constantCount, 1F);
stringToConstant = new HashMap<>(constantCount, 1F);
// Don't use `EnumMap` here; that can break when using ProGuard or R8
constantToName = new HashMap<>(constantCount, 1F);
Comment on lines +74 to +77
Copy link
Contributor

@Marcono1234 Marcono1234 Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if 'load factor' 1F here could decrease performance (and if that would be noticeable)? See HashMap documentation:

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put).

If you want feel free to keep it like this for now and wait for the feedback from the Gson project members.


AccessibleObject.setAccessible(fields, true);

Expand Down