Skip to content

Commit 003e5b0

Browse files
committed
Minor performance tweaking for enum/string resolution for serialization
1 parent fad4726 commit 003e5b0

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/main/java/com/fasterxml/jackson/databind/util/EnumValues.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
public final class EnumValues
1414
{
1515
private final Class<Enum<?>> _enumClass;
16-
17-
/**
18-
* Use a more optimized String value here, to possibly speed up
19-
* serialization.
20-
*/
21-
private final EnumMap<?,SerializableString> _values;
2216

23-
@SuppressWarnings({ "unchecked", "rawtypes" })
24-
private EnumValues(Class<Enum<?>> enumClass, Map<Enum<?>,SerializableString> v) {
17+
private final Enum<?>[] _values;
18+
private final SerializableString[] _textual;
19+
20+
private transient EnumMap<?,SerializableString> _asMap;
21+
22+
private EnumValues(Class<Enum<?>> enumClass, SerializableString[] textual)
23+
{
2524
_enumClass = enumClass;
26-
_values = new EnumMap(v);
25+
_values = enumClass.getEnumConstants();
26+
_textual = textual;
2727
}
2828

2929
public static EnumValues construct(SerializationConfig config, Class<Enum<?>> enumClass) {
@@ -41,13 +41,12 @@ public static EnumValues constructFromName(MapperConfig<?> config, Class<Enum<?>
4141
Class<? extends Enum<?>> cls = ClassUtil.findEnumType(enumClass);
4242
Enum<?>[] values = cls.getEnumConstants();
4343
if (values != null) {
44-
// Type juggling... unfortunate
45-
Map<Enum<?>,SerializableString> map = new HashMap<Enum<?>,SerializableString>();
44+
SerializableString[] textual = new SerializableString[values.length];
4645
for (Enum<?> en : values) {
4746
String value = config.getAnnotationIntrospector().findEnumValue(en);
48-
map.put(en, config.compileString(value));
47+
textual[en.ordinal()] = config.compileString(value);
4948
}
50-
return new EnumValues(enumClass, map);
49+
return new EnumValues(enumClass, textual);
5150
}
5251
throw new IllegalArgumentException("Can not determine enum constants for Class "+enumClass.getName());
5352
}
@@ -57,23 +56,39 @@ public static EnumValues constructFromToString(MapperConfig<?> config, Class<Enu
5756
Class<? extends Enum<?>> cls = ClassUtil.findEnumType(enumClass);
5857
Enum<?>[] values = cls.getEnumConstants();
5958
if (values != null) {
60-
// Type juggling... unfortunate
61-
Map<Enum<?>,SerializableString> map = new HashMap<Enum<?>,SerializableString>();
59+
SerializableString[] textual = new SerializableString[values.length];
6260
for (Enum<?> en : values) {
63-
map.put(en, config.compileString(en.toString()));
61+
textual[en.ordinal()] = config.compileString(en.toString());
6462
}
65-
return new EnumValues(enumClass, map);
63+
return new EnumValues(enumClass, textual);
6664
}
6765
throw new IllegalArgumentException("Can not determine enum constants for Class "+enumClass.getName());
6866
}
6967

70-
public SerializableString serializedValueFor(Enum<?> key) { return _values.get(key); }
71-
public Collection<SerializableString> values() { return _values.values(); }
68+
public SerializableString serializedValueFor(Enum<?> key) {
69+
return _textual[key.ordinal()];
70+
}
71+
72+
public Collection<SerializableString> values() {
73+
return Arrays.asList(_textual);
74+
}
7275

7376
/**
7477
* Method used for serialization and introspection by core Jackson code.
7578
*/
76-
public EnumMap<?,SerializableString> internalMap() { return _values; }
79+
@SuppressWarnings({ "unchecked", "rawtypes" })
80+
public EnumMap<?,SerializableString> internalMap() {
81+
EnumMap<?,SerializableString> result = _asMap;
82+
if (result == null) {
83+
// Alas, need to create it in a round-about way, due to typing constraints...
84+
Map<Enum<?>,SerializableString> map = new LinkedHashMap<Enum<?>,SerializableString>();
85+
for (Enum<?> en : _values) {
86+
map.put(en, _textual[en.ordinal()]);
87+
}
88+
result = new EnumMap(map);
89+
}
90+
return result;
91+
}
7792

7893
/**
7994
* @since 2.2

0 commit comments

Comments
 (0)