chaokunyang commented on code in PR #1892:
URL: https://github.com/apache/fury/pull/1892#discussion_r1811024280
##########
java/fury-core/src/main/java/org/apache/fury/serializer/EnumSerializer.java:
##########
@@ -40,20 +50,54 @@ public EnumSerializer(Fury fury, Class<Enum> cls) {
Preconditions.checkArgument(enclosingClass.isEnum());
enumConstants = enclosingClass.getEnumConstants();
}
+
+ if (fury.getConfig().serializeEnumByName()) {
+ // as we know the size of enum is fixed, initialize the size of map with
that value
+ int initialCapacity = (int) Math.ceil(enumConstants.length / 0.99f);
+
+ metaStringtoEnumRepresentation = new HashMap<>(initialCapacity, 0.99f);
+
+ for (Enum enumConstant : enumConstants) {
+ if (enumConstant != null) {
+ MetaString ms =
+ Encoders.GENERIC_ENCODER.encode(enumConstant.name(),
MetaString.Encoding.UTF_8);
+ MetaStringBytes msb =
metaStringResolver.getOrCreateMetaStringBytes(ms);
+ metaStringtoEnumRepresentation.put(msb, enumConstant);
+ }
+ }
+ } else {
+ metaStringtoEnumRepresentation = null;
+ }
}
@Override
public void write(MemoryBuffer buffer, Enum value) {
- buffer.writeVarUint32Small7(value.ordinal());
+ if (fury.getConfig().serializeEnumByName()) {
+ MetaString enumMetaString =
Review Comment:
metaStringResolver may return a different object, we could use fury
ObjectMap instead. fury ObjectMap is a linear probe based map, which is faster
than hashmap for smaller map size
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]