跳到主要内容
uencoder

Protocol Buffers Encoding Reference

Protobuf wire 格式化: varint, 长度-delimited, 固定-宽度, 和字段 tag 编码.

Wire Types

Protocol Buffers encode each field as a tag followed by a value, where the tag contains the field number and wire type. The wire type determines how the value is read: varint, 64-bit fixed, length-delimited, 32-bit fixed, or deprecated group. This compact binary format avoids field names on the wire, relying on field numbers for identification. Unknown fields can be skipped using the wire type without a schema.

varint
Variable-length integer, wire type 0
int32, int64, bool
fixed64
8-byte little-endian, wire type 1
fixed64, double
length-delimited
Length prefix then bytes, wire type 2
string, bytes, message
fixed32
4-byte little-endian, wire type 5
fixed32, float

Varint Encoding

Varints encode integers in groups of 7 bits with the high bit indicating whether more bytes follow. Small values use a single byte, while large 64-bit values use up to 10 bytes. This makes protobuf efficient for typical small integer values. Signed integers use zigzag encoding to avoid the 10-byte penalty for negative numbers, mapping signed to unsigned before varint encoding.

Schema Evolution

Protobuf supports forward and backward compatibility when fields are added or removed. New optional fields are ignored by old parsers as unknown fields, and old fields can be reserved to prevent reuse. Changing a field type or number breaks compatibility. Repeated fields can be packed to save space when the elements are scalar. Following the compatibility rules lets services evolve their schemas without coordinated deployments.

相关工具

参考链接