0
votes

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.

2
Maybe because they use variable-length encoding? But there are fixed types too with 32/64 bit... - MB-F

2 Answers

1
votes

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.

1
votes

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.