Skip to content
Packed Repeated Fields
step 1/5

Reading — step 1 of 5

Read

~1 min readWire Format

Packed Repeated Fields

For repeated scalar fields, proto3 uses packed encoding by default:

Schema: repeated int32 ids = 4;
Value: [1, 2, 3]

Encoded:
  tag (4, wire_type 2): (4 << 3) | 2 = 0x22
  length: 3 (each int = 1 byte varint)
  values: 01 02 03

Total: 22 03 01 02 03

vs unpacked (proto2 default):

22 01 → 22 02 → 22 03   (3 separate fields, each tag + value)
Total: 6 bytes

Packed saves: per-element tag overhead.

Worth more for many small ints. Less for few or large.

Implementation:

python

Mixed packed/unpacked: a proto3 client may send packed; but parsers must accept both.

For repeated MESSAGES:

  • NOT packed (each is length-delimited already).
  • Multiple tag-length-value sets.

For repeated STRINGS / BYTES:

  • Same as messages (already length-delimited).

For repeated FIXED:

  • Packed: just concat bytes. No per-element header.

Packed efficiency:

  • Saves N-1 tag bytes per repeated field.
  • For 1000 small ints with field number 4: ~1000 byte savings.
  • For 10 large numbers: minimal savings.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…