String field processing in getSerializedSize/writeTo looks suspicious

9 views
Skip to first unread message

Stanislav Kashirin

unread,
Feb 17, 2020, 4:00:42 PM2/17/20
to Protocol Buffers

Hey!


Can someone from protobuf team explain to me the rationale behind the Java code for string fields processing produced by protoc?


Protoc generates Java Object field for each proto string field and protobuf uses it to store either ByteString or java.util.String (dunno why).

Also each string field gets two accessor methods: getXxx for String and getXxxBytes for ByteString.


The suspicious part begins inside generated methods getSerializedSize and writeTo.


For both of these methods getXxxBytes will be called disregarding the current type of an instance stored in Object field which leads to unnecessary ByteString allocation when actual type is java.util.String (and this is the common case from my perspective).


It looks even more peculiar if we will take into account the fact that in both methods we may see a usage of dual-type-aware GeneratedMessageV3.writeString/GeneratedMessageV3.computeStringSize literally in the next line after getXxxBytes calls.


Is it a bug or an intentional decision?


Recently, I've opened an issue on github, but did not get any comments so far.

Reply all
Reply to author
Forward
0 new messages