why 2^29-1 is the biggest key in protocol buffers

2019-09-20 12:14发布

问题:

2^29 -1 ? Because "Field numbers 1-15 have a 1-byte tag."
32-byte don't have the tag? I know the 3-byte,but why don`t have the tag?

Is that varint? I can`t understanding the protobuf\descriptor.cc source code.

回答1:

All fields have a tag, but for field numbers 16 and higher the tag takes multiple bytes. So field numbers 1 to 15 should be used for the most common fields. And yes, the encoding is varint.

The largest key is 2^29 because the lowest 3 bits are used to store the field datatype. So 29 + 3 = 32 fits neatly into uint32_t.

You will find more information here: https://developers.google.com/protocol-buffers/docs/encoding#structure