Protocol Buffers varint vs fixed
This article is much more a note to myself than something else but this might be interesting for people out there.
I wanted to calculate the thresholds at which it is better it is to use a fixed
rather than a varint. Now, knowing that the varint are encoded in base 128, this basically means that we are dealing with power of 128. This gives us the following table:
Threshold value | Bytes size (without tag) |
---|---|
0 | 0 |
1 | 1 |
128 | 2 |
16,384 | 3 |
2,097,152 | 4 |
268,435,456 | 5 |
34,359,738,368 | 6 |
4,398,046,511,104 | 7 |
562,949,953,421,312 | 8 |
72,057,594,037,927,936 | 9 |
In summary:
- From 268,435,456 to whatever limit you 32 bits type has, it is better to use a
fixed32
. - From 72,057,594,037,927,936 to whatever limit you 64 bits type has, it is better to use a
fixed64
.
Written on October 2, 2022