Tom Gullen
Tom Gullen

Reputation: 61727

Protobuff identifiers performance

In the protobuf-net documentation it states that identifiers on properties:

lower numbers take less space - don't start at 100,000,000

Why is this, and are there are any other performance issues with using larger numbers?

Upvotes: 2

Views: 43

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062855

Field headers are encoded as varints; this aspect is because of how "varint encoding" works. Kinda similar to UTF-8, where we anticipate that "abc" (3 characters) takes 3 bytes, but "Åℬℂ" (also 3 characters) takes 9 bytes.

Essentially, "varint" is a 7-bit encoding where the 8th bit indicates continuation; 100,000,000 is (in binary) 101111101011110000100000000 - 27 bits; note that the "wire type" is also 3 bits on top of this, so in total we're looking at 30 bits, which means that with a 7-bit-plus-continuation encoding it will take 5 bytes (4×7=28 which is too small; 5×7=35).

As for other issues: there's also a reserved range that protobuf-net does not enforce, but which should be avoided: 19000 through 19999

Not directly related to field numbers, but general protobuf guidance: avoid negative varint number values unless you're using "zig-zag" encoding (DataFormat.Signed in protobuf-net terms).

Upvotes: 1

Related Questions