Reputation: 18109
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
Reputation: 7278
Protobuf scalar type encoding uses variable number of bytes:
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
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