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 Integer
s as a repeated list or something else ?
As an example writing byte[] as a Srting 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.)
See more on this question at Stackoverflow