Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance penalty of getSerializedSize() in Protocol Buffers

Is there a performance penalty for calling getSerializedSize() on a GPB message before serializing the message with writeTo(OutputStream)?

I need to be able to know the size of a message before writing it to an output stream.

I'm using GPB on Java.

like image 522
Elliot Vargas Avatar asked Dec 14 '09 21:12

Elliot Vargas


People also ask

Is Protobuf faster than JSON?

It is essential to be aware that protobuf is not necessarily the fastest option. If your data is mainly string, then JSON format might be a good choice.

What are Protobuf Protocol Buffers useful for?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

Does Protobuf handle endianness?

Protocol buffers messages always use little-endian encoding. Implementations running on big-endian architectures should be doing the conversions automatically. If you are receiving data in wrong order, I would suggest using protoc --decode_raw to see whether the error occurs on the transmission or reception side.

What is protocol buffer in gRPC?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.


1 Answers

There is no performance penalty. From the second message of this thread on the Protobuf mailing list:

You can call getSerializedSize() to find out the message size ahead of time. Note that calling this doesn't actually waste any time since the result is cached, and it would have to be called during serialization anyway.

like image 151
ire_and_curses Avatar answered Oct 05 '22 02:10

ire_and_curses