Martin G
Martin G

Reputation: 18109

Small scalar types and google protocol buffers

What is the reasoning behind not having small scalar types in google protocol buffers?

https://developers.google.com/protocol-buffers/docs/proto#scalar

More specifically for C++, do I transfer a uint16_t as two bytes in gpb? I'm looking into converting an existing message based protocol to gpb and this seems a bit strange to me.

Upvotes: 0

Views: 2081

Answers (2)

Pavel Zdenek
Pavel Zdenek

Reputation: 7278

Protobuf scalar type encoding uses variable number of bytes:

  • 1 byte if < 2**(8-1) = 128
  • 2 bytes if < 2**(16-2) = 16384
  • 3 bytes if < 2**(24-3) = 2097152
  • 4 bytes if < 2**(32-4) = 268435456 etc.

So while this may be killing you as space-savvy C coder, uint_16t will be taking at most 2 bytes only for lowest 1/4 of the range.

PB is ultimately designed for forward compatibility, and Google knows that short fixed data types will always turn out too short :-) (Y2K, IPv4, upcoming 2038 Unix Time, etc.) If you're really, terribly after compactness, use bytes as @SRLKilling recommended, at the expense of needing to write your own codec on top of it.

Upvotes: 1

SRLKilling
SRLKilling

Reputation: 42

Gbp uses variable-length encoding, meaning that the size of the transmitted integer depends on the value of the integer. Small ints will be sent using only few bytes.
Here is a link to a guide about gbp encoding

In particular, if you only have one specific case of a short int (and not many of them, in which case you'll probably want to use bytes), you should simply cast all uint16_t to uint32_t and let varints do the stuff.

Upvotes: 1

Related Questions