๐Protobuf: calculating field size
Tag
Each field is prepended VarInt-encoded value: (field_number << 3) | wire_type
Max fieldnumber is . 19,000 through 19,999 are reserved.
bytes | bits available | max field number |
---|---|---|
1 | 4 | 15 |
2 | 11 | 2047 |
3 | 18 | 262143 |
4 | 25 | 33554431 |
5 | 29 | 536870911 |
Values
VarInt (wire type 0)
Protobuf extensively uses base128 encoding (VarInt).
bytes | bits available | max unsigned value | signed min | signed max |
---|---|---|---|---|
1 | 7 | 127 | -64 | 63 |
2 | 14 | 16383 | -8192 | 8191 |
3 | 21 | 2097151 | -1048576 | 1048575 |
4 | 28 | 268435455 | -134217728 | 134217727 |
5 | 35 | 34359738367 | -17179869184 | 17179869183 |
6 | 42 | 4398046511103 | -2199023255552 | 2199023255551 |
7 | 49 | 562949953421311 | -281474976710656 | 281474976710655 |
8 | 56 | 72057594037927935 | -36028797018963968 | 36028797018963967 |
9 | 63 | 9223372036854775807 | -4611686018427387904 | 4611686018427387903 |
10 | 64 | 18446744073709551615 | -9223372036854775808 | 9223372036854775807 |
VarInt encoding (int32, int64, uint32, uint64, sint32, sint64, bool, enum):
base128 (7-bits per byte)
zigzag encoding is only used for explicitly-signed types (sint32, sint64)
Length-delimited (wire type 2)
string, bytes, embedded messages, packed repeated fields
VarInt-encoded length + bytes
64-bit (wire type 1)
fixed64, sfixed64, double
8 bytes
32-bit (wire type 5)
fixed32, sfixed32, float
4 bytes
Packed repeated fields
Only VarInt, 32-bit, and 64-bit types can be packed.
Packed fields are encoded as:
wire type 2 (length delimited)
VarInt length
Next length bytes values go one after another without additional tags or separators
Backlinks
- ๐ Protobuf: no inheritance support
- ๐ ยง Protobuf