Protobuf Packed Is the One Option the Wire Honors (and the Parser Lets You Put It Anywhere)

Every field option I have measured so far is a label the wire ignores. deprecated is inert metadata, json_name is stored and never applied, and editions is a number in the descriptor. Packed is the one option in the core spec that changes the bytes the encoder writes, and that makes its behavior worth pinning down exactly. I ran it through the parser that powers the converter, protobufjs 7.4.0, and then through protoc 25.3 for the compiler side, because the two sides disagree about where the option is allowed to live.

The option flips a default, and the default is set by the syntax line. Proto3 packs repeated scalars by default, so repeated int32 a = 1; with the values 1, 2, 3 encodes as 0a03010203, one tag, one length, three raw bytes. Add [packed=false] and the same data encodes as 080108020803, one tag per value, nine bytes. Proto2 goes the other way: bare repeated int32 encodes unpacked, and [packed=true] produces the packed form. Both directions measured, the encoder honors the option either way it is set. One structural quirk: the flag does not sit in the options bag where deprecated and json_name live. A parsed field with only a packed option has field.packed set to true or false and an options bag that is undefined. The parser reads it and hands it up, because the generated encoder consults it.

The decoder ignores it entirely, and that is the part most people get wrong. I built four schemas for the same field, proto3 default, proto3 with packed=false, proto2 default, proto2 with packed=true, and decoded both byte forms under each one. All eight combinations return [1, 2, 3], value for value. A schema that says packed=false reads packed bytes without a comment, and a packed schema reads the unpacked form just as easily. The option is a writing-time choice, not a reading-time rule, which is also why two services that disagree about packing can still read each other's streams, and why a file that mixes the two forms mid-message decodes clean. Nothing on the read path asks what the schema said.

Where the parser and the compiler disagree is where it gets interesting, and the direction is the reverse of the traps this parser has before. A bare group line and an import alias die in the JS tool with illegal token while protoc compiles them. Packed goes the other way. Write [packed=true] on a non-repeated field, int32 a = 1 [packed=true];, and the parser accepts it, stores packed = true, and the field still encodes as the plain two-byte 0807. It also accepts the option on a repeated string, where the encoder always writes one tag per value anyway, and on a repeated message, where packing is not a thing. All three silently, no warning. protoc 25.3 rejects all three with the same line: [packed = true] can only be specified for repeated primitive fields. So a file that fails the real compiler parses clean in the tool and looks like it did something, because it stored a flag nothing downstream reads.

The numbers worth keeping. A packed int64 list that crosses 2^53 encodes as 0a09 with a nine-byte varint inside, 8180808080808010, and round trips as the exact decimal string, the length-delimited wrapper is what keeps a variable-width list intact in one field. Packed bools are 0a03010001, one byte per value under one tag. In the descriptor, the option does land where you would expect, as a four-byte FieldOptions submessage on the field, B 02 10 01 for true, which is why a diff tool that walks descriptors sees it change even though the decoder never does. And the JSON mapping has no packed concept at all: packed and unpacked input produce byte-identical JSON, the array is the array.

The tool never says the word. I grepped the shipped page and packed appears zero times, because nothing the tabs do changes when you see it: the decode tab reads both forms under any schema, the encode tab follows whatever the schema says or its syntax default, and the raw decode tab shows you the form on the wire either way, one tag or several, which is the check that actually answers the question of which form a production stream is using. The wire format underneath is in the wire format post, the syntax-side split this option overrides is in the proto3 vs proto2 post, and the converter with the parser this post measured is at proto.datajutsu.com.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe