DagrA guide to binary formats, and to Dagrdraft

7 Chapter seven

Regular, next to FlatBuffers and Cap'n Proto

Some formats are meant to be read where they lie. No decoding, no objects: the program asks for a field and the answer comes straight out of the bytes it received. FlatBuffers and Cap'n Proto made that practical. Dagr's regular layout belongs to the same family, and disagrees with both about what the bytes should cost.

The bargain

Chapter 6 was about formats that are small and are walked from the front. This family makes the opposite trade. It spends bytes, on tables, pointers and padding, so that a reader can go directly to the one thing it wants and never touch the rest. That pays when the data is large and the reader wants little of it: one record out of a file of a million, a few fields of a message that is being passed along.

Here is the person from chapter 3 again, in all three. Change the values, leave a field out, and choose a format.

a reader wants only:

Eight bytes of data, and three quite different amounts of everything else. Each format has its reasons.

FlatBuffers: a table, and everything in its place

Select FlatBuffers. The record has a table with one entry per field saying where that field is, and the record points at its table. Text lives outside the record, behind a pointer. Every pointer is four bytes and every table entry two, whatever they hold. Every value sits at an address that is a multiple of its own size, and padding fills the gaps.

Cap'n Proto: no table at all

Select Cap'n Proto. There is no table. A record is a block of numbers followed by a block of pointers, and which bytes hold which field is fixed by the schema, as in the bare struct of chapter 3. Everything is measured in words of eight bytes: the numbers are padded to a whole word, a pointer is a whole word, the text is padded to whole words.

Untick score. Nothing gets smaller. A field has its place whether it is set or not, and an unset field is zeros. Cap'n Proto's answer to all those zeros is a simple compression of its own, called packing, which the panel shows as a second size. It has nothing to do with Dagr's packed layout: it squeezes the zeros out of bytes that are already written. The packed form is for storing and sending. It has to be unpacked again before anything can be read from it in place.

Dagr, regular: the table, without the fixed widths

Select Dagr. The idea is FlatBuffers': a table that says where each field is. The difference is in the table and the pointers: neither has a fixed width, and each is as small as it can be. Numbers keep the width of their type.

What fixed widths buy

It would be easy to read that panel as "Dagr is smaller, so Dagr is better". The other two are not wasting those bytes. They are buying things with them, and Dagr's regular layout does without.

Dagr keeps one thing from this list where it matters most. A list of numbers in a regular node is a plain block of fixed-width values, and a schema can ask for such a block to be aligned, so that it can be handed to code that needs it that way. Chapter 14 is about that.

Many records

One record is a poor measure, because a table is paid for once and used many times. Here is a list of people, each with a name, an age and a score, in each format. The sizes come from the formats' own tools.

people:
names, ages and scores themselvesadded by the format

Where a pointer can go

All three use pointers, and they differ on where a pointer is allowed to lead. After chapter 4 you know why that matters.

Growing the schema

Side by side

FlatBuffersCap'n ProtoDagr, regular
finding a fieldtablefixed positiontable
widthsfixedfixed, in 8-byte wordsnumbers fixed; table and pointers as small as they can be
paddingyesyesonly where a schema asks
an absent field costsa table entryits full widtha table entry
shared recordsyesnoyes
cyclesnonoyes
change a value in placenumbersyesno

If you want memory you can treat as structs and edit, the first two are built for it. If you want data you can read in place that is also compact and may be a graph, that is the corner the regular layout is in. Reading speed is not in the table: the measurements, and the code behind them, are in the repository's benchmarks, and chapter 18 says what they show.

What these panels are.

Try it

  1. In the first panel, find a name length at which Cap'n Proto grows by eight bytes at once. Why eight?
  2. Ask for score in each format. Cap'n Proto's reader looks at no table. What does it know in advance that the other two have to look up?
  3. In Dagr's bytes, point at the table entries and then at the byte where the record begins. How does the record find its table?
  4. In the second panel, compare 1 person with 1000. Which format's cost for each person changes most, and why?
  5. You need to store a family tree in which cousins marry. Which of the three can hold it without help from your program?

What comes next

The records so far had fields that were numbers and text. Two more things belong in a schema before the guide moves on: a field that is one of several choices, and a field that is a list. Chapter 8 is about the first, enums and unions, and chapter 9 about the second.