thwd
thwd

Reputation: 24818

how to choose protocol-buffer's tags

Here's a real-life example; hand-written .proto file extract:

message StatsResponse {
  optional int64 gets = 1;
  optional int64 cache_hits = 12;
  optional int64 fills = 2;
  optional uint64 total_alloc = 3;
  optional CacheStats main_cache = 4;
  optional CacheStats hot_cache = 5;
  optional int64 server_in = 6;
  optional int64 loads = 8;
  optional int64 peer_loads = 9;
  optional int64 peer_errors = 10;
  optional int64 local_loads = 11;
}

I understand everything about it except how the programmer who wrote it chose the tag numbers he was going to use.

The official documentation just notes how these tags are shifted around and encoded to compose a wire type identifier. Yet, in the example above, several fields of the same data type have different tag numbers.

My question is; how do I choose tag numbers if I was going to write a .proto file from scratch?

Upvotes: 1

Views: 82

Answers (1)

Kenton Varda
Kenton Varda

Reputation: 45181

The number is just an alternative way to identify the field, other than its name. The encoding uses numbers rather than names because they take less space and time to encode. It doesn't matter what number you use as long as you don't change the number later (although, lower numbers take less space on the wire).

Usually, people simply assign numbers sequentially starting from 1. In your example proto, cache_hits is probably a new field that was added after all the others, which is why its number appears "out-of-order".

Upvotes: 3

Related Questions