Getting enum value from object is slow

51 views
Skip to first unread message

Andrei Solodin

unread,
Jan 15, 2023, 7:56:07 PM1/15/23
to Protocol Buffers
Greetings , in our Java application we use the protobuf object model quite a bit. While profiling, I have noticed that simply getting an enum value from an object is quite slow. It appears to be caused by the fact that the object stores internally an int which must be converted to a java enum on ever call to the getter. If the object just stored the enum value directly, this would not be necessary. The required translation between int<->enum could just be done once on (de)serialization. Wondering if this would qualify for a performance request.GetImageAttachment.png

Thanks

Jerry Berg

unread,
Jan 27, 2023, 12:28:32 PM1/27/23
to Protocol Buffers
Hi Andrei,

This doesn't work for proto3 where we need to support "open enums" so that enum values added to an enum definition but not yet compiled into the receiver can still be stored and accessed as a raw int. This can't be done with Java's Enums which are "closed enums". Our long term plan is that proto enums will default to open as in proto3, so we are unlikely to change this behavior. If enum access is unacceptably slow, consider switching to int32 values -- it is a wire compatible change though certainly not quite as convenient or self-documenting.

-Jerry

Reply all
Reply to author
Forward
0 new messages