以下是來自Google Protobuf Documents里的一句話:
As you can see, each field in the message definition has a unique numbered tag. These tags are used to identify your fields in the
message binary format, and should not be changed once your message type is in use. Note that tags with values in the range 1 through 15 take one byte to encode. Tags in the range 16 through 2047 take two bytes. So you should reserve the tags 1 through 15 for very frequently occurring message elements. Remember to leave some room for frequently occurring elements that might be added in the future.
這里要做一個解釋,就是為什么是1到15,以及16到2047呢?
- 1到15,僅使用1bytes。每個byte包含兩個部分,一個是field_number一個是tag,其中field-number就是protobuf中每個值后等號后的數字(在C++和Java中,如果不設置這個值,則它是隨機的,如果在Python中,不設置,它則不被處理(這個在message binary format中的Field Order一節中有提到)。那么我們可以認為這個field_number是必須的。那么一個byte用來表達這個值就是000000000,其中紅色表示是否有后續字節,如果為0表示沒有也就是這是一個字節,藍色部分表示field-number,綠色部分則是wire_type部分,表示數據類型。也就是(field_number << 3) | wire_type。其中wire_type只有3位,表示數據類型。那么能夠表示field_number的就是5位藍色的數字,5位數字能夠表達的最大范圍就是1-15(其中0是無效的)。
- 16到2047,與上面的規則其實類似,下面以2bytes為例子,那么就有10000000 00000000,其中紅色部分依然是符號位,因為每個byte的第一位都用來表示下一byte是否和自己有關,那么對于>1byte的數據,第一位一定是1,因為這里假設是2byte,那么第二個byte的第一位也是紅色,刨除這兩位,再扣掉3個wire_type位,剩下11位(2*8-2-3),能夠表達的數字范圍就是2047(211)。
參考資料:
- http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/proto.html
- http://code.google.com/apis/protocolbuffers/docs/encoding.html