Question

We need our protobuf messages to contain as little data as possible. So what are the best practices we can follow in order to gain the maximum out of it. As an example writing byte[] as a String or ByteString ? What makes the difference? And adding a list of Integers as a repeated list or something else ?

Was it helpful?

Solution

As an example writing byte[] as a String or ByteString ?

If you want to write binary data, use a bytes fields (so ByteString). A string field is UTF-8-encoded text, so can't be used for all possible byte sequences.

And adding a list of integers as a repeated list or something else ?

Yes, use a repeated list - but with the [packed=true] option.

Basically, look over the whole encoding documentation and work out what's most appropriate for you. In particular, choose carefully between the various numeric representations, based on what your actual data will be. (If you're writing 32-bit values which are typically very large, consider using the fixed32 format instead of just int32 for example.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top