DagrA guide to binary formats, and to Dagrdraft

6 Chapter six

Packed, next to Protocol Buffers

Protocol Buffers made one idea ordinary: put a small label in front of each value, and a reader can skip what it does not know. Dagr's packed layout is the same idea. This chapter puts the two side by side, because the places where they differ are the places where a format's designers had to choose.

The same message, twice

A sensor sends a reading: where it is, a counter, how the counter changed, a temperature and a few raw samples. Here it is as a Protocol Buffers schema and as a Dagr one.

message Reading {
  string sensor = 1;
  uint32 count = 2;
  int32 delta = 3;
  float temp = 4;
  repeated uint32 samples = 5;
}
Node("Reading", fields=[
    "sensor"  >> t.utf8 >> required,
    "count"   >> t.u32 >> required,
    "delta"   >> t.i32 >> required,
    "temp"    >> t.f32 >> required,
    "samples" >> t.u16.array >> required,
], packed=True)

Both formats write this as a run of pairs, a label and then a value, one field after another. Chapter 3 called that "by label". Change the values below and compare the two results. The colours are the usual ones.

The two results look alike, as you would expect from the same idea: a label, a value, a label, a value. Where they differ, a decision differs.

None of this makes one format smaller than the other in general. With everyday values and delta declared sint32, Protocol Buffers is the smaller one, by the two bytes at the front. What Dagr's choices do is make the size depend less on guesses made when the schema was written.

What a label is

The larger difference is in the label itself. In Protocol Buffers the label is a number that a person chose and wrote into the schema. In Dagr it is the field's position in the schema: the first field is 0, the next is 1. That sounds like a detail, and almost everything else follows from it.

Below, the same reading again. Choose how the Protocol Buffers fields are numbered and in which order they were written, and then ask for one field.

field numbers:
order in the message:
a reader wants only:

Numbers that people choose

Protocol Buffers was built inside Google for messages that many teams extend at once. Chosen numbers suit that well. Each team can be given a range, nobody has to coordinate, a deleted field leaves a hole that is never reused, and a field keeps its number for life wherever its line moves in the file. Choose "a range for each team" to see the price: labels above 15 no longer fit in one byte, and above 2047 they take three.

A position needs none of that bookkeeping and stays small: Dagr's first 64 fields have one-byte tags. But a position is a weaker identity than a number, and the next section is about what that costs.

Any order, or one order

Because its labels are free-standing numbers, Protocol Buffers lets the fields of a message appear in any order. Writers usually emit them by number, but a reader may not rely on that. A field may even appear twice, and then the last one counts. Choose "another valid order": the message is as correct as before.

Dagr's packed fields are always in schema order, with tags that only go up. Now ask for a field in the panel and compare what each reader has to look at.

What position costs

This is where Dagr's packed layout is the weaker of the two, and you should know it before you choose it.

Reading without decoding first

A Protocol Buffers message is normally turned into objects before a program reads anything from it. That is a pass over all of the bytes and an allocation for every string, list and inner message, whether or not the program wanted them.

Dagr also generates accessors that read from the bytes as they are. Asking a packed node for a field walks its tags up to that field and returns the value, or a view of it, and builds nothing. That is what the monotonic order in the last panel is for.

The cost is the walk. A packed node has no table, so every request for a field starts again from the front, and asking a packed list for element i walks i elements. Reading a list by index in a loop is therefore slow in a way that is easy to miss. The generated code offers iterators for that, which go through a list once. And for a node with many fields that you read at random, the table of the regular layout is the better choice, which is the subject of chapter 7.

How much any of this matters in practice depends on the data and the language. The measurements, with the code that produces them, are in the repository's benchmarks, and chapter 18 says what they show.

Side by side

Protocol BuffersDagr, packed
a label isa number someone chosethe field's position
order of fieldsany, repeats allowedschema order, once each
how a number is writtenfixed by the schemathe shorter way, for each value
new fieldsany free numberat the end only
readable without the schemathe structure, yesno
one field out of manyread to the endread up to it
a message knows its own lengthnoyes

One row is missing because it is not about the packed layout. A Protocol Buffers message is a tree, with everything that chapter 4 said about trees. A Dagr packed node is a tree too, but it can sit inside a graph: a regular node can hold packed ones, share them, and be reached by pointer. Chapter 5 showed that mix.

What these panels are.

Try it

  1. Find the smallest count for which Dagr writes four plain bytes. What does Protocol Buffers write for the same number?
  2. Set the samples to 1, 2, 300, then to 1, 2, 3, then to 300, 400, 500. Dagr's list changes shape each time. What are the three shapes?
  3. Find a temperature that Dagr writes in one byte, in two, in three and in four.
  4. In the second panel, give each team a range and ask for sensor. How many bytes does each reader look at? Now put the fields in another order. What changed, and for which reader?
  5. You want to delete delta from the schema. Describe what you would do in each format, and what an old reader sees afterwards.

What comes next

Chapter 7 does the same for the regular layout and its relatives, FlatBuffers and Cap'n Proto: formats that spend bytes on tables and pointers so that a reader can go straight to what it wants.