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.
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.
- A table entry is one byte, as long as the record is small enough for that, and two when it is not.
- A pointer is a V62, the number from chapter 2 that is one byte when the distance is short. Most distances are short.
- There is no padding, and no zero after the text.
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.
- Values where a processor likes them. A four-byte number at an address that is a multiple of four can be read by any processor in one step, and a whole record can be treated as a struct in memory. Dagr's values sit wherever they fall. Today's common processors read such values without complaint and at little cost, but it is a cost, and on small or older hardware it can be a large one.
- Changing a value in place. A number with a fixed width and a fixed position can be overwritten in the buffer. Both FlatBuffers and Cap'n Proto allow that. In Dagr the width of a pointer or a table entry depends on what it has to reach, and in a packed node the width of a number depends on its value. So a graph is written once and not edited afterwards.
- The cheapest possible read. Cap'n Proto reads a number from a position it knew when the program was compiled. FlatBuffers and Dagr first look in a table, and Dagr then works out how wide a pointer is. Each step is small. They are still steps.
- Nothing to encode. A Cap'n Proto message is built directly in the form in which it is sent. There is no writing step at the end, only handing over the memory.
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.
- The table is shared. In FlatBuffers and in Dagr, records with the same fields present all point at one table. A thousand people cost one table, not a thousand.
- What is left is paid per record. Pointers, padding, lengths. With fixed widths that comes to more than the data itself. With Dagr's it is somewhat less than half as much again.
- Leaving a field out. Choose "every second person has no score". FlatBuffers and Dagr shrink: those people get a second table and a shorter record. Plain Cap'n Proto does not change by a byte.
- Packing closes most of the gap. Cap'n Proto with its packing comes close to Dagr's regular layout here. The difference is in what you can do with the bytes: Dagr's are read as they are, Cap'n Proto's are unpacked first.
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.
- FlatBuffers: forwards only. A pointer is an unsigned distance towards the end of the buffer. Two records may point at the same third one, so sharing is possible. Nothing can point back at something that contains it, so a cycle is not. The distance is 32 bits, which by default limits a buffer to two gigabytes.
- Cap'n Proto: a tree. Every object has one owner. Put the same object in two places and it is copied.
- Dagr: either way. A pointer to a node can lead forwards or backwards, so shared nodes and cycles are ordinary. Being a V62, it can also be as wide as eight bytes when a buffer is large enough to need it.
Growing the schema
- FlatBuffers and Dagr's regular layout grow the same way: a new field is a new entry at the end of the table. Old readers never look at it. New readers of old data find the table too short and treat the field as absent. Fields may not be inserted, removed or reordered, the same rule as for Dagr's packed layout.
- Cap'n Proto numbers its fields (
@0,@1) and lets a record's two blocks get longer. A pointer to a record says how long its blocks are, so an old reader ignores the extra and a new reader of an old, shorter record sees zeros.
Side by side
| FlatBuffers | Cap'n Proto | Dagr, regular | |
|---|---|---|---|
| finding a field | table | fixed position | table |
| widths | fixed | fixed, in 8-byte words | numbers fixed; table and pointers as small as they can be |
| padding | yes | yes | only where a schema asks |
| an absent field costs | a table entry | its full width | a table entry |
| shared records | yes | no | yes |
| cycles | no | no | yes |
| change a value in place | numbers | yes | no |
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.
- The first computes its bytes in your browser. The FlatBuffers bytes are checked against
flatc, the Cap'n Proto bytes, plain and packed, againstcapnp, and the Dagr bytes against Dagr's reference writer, over a few hundred records each: identical. - The second shows sizes measured when this page was built, with
flatc,capnpand Dagr's reference writer. They are sizes, not speeds, and they are the same on any machine. - Each format is used plainly. FlatBuffers also has structs without tables, and both it and Cap'n Proto have options this chapter does not touch.
Try it
- In the first panel, find a name length at which Cap'n Proto grows by eight bytes at once. Why eight?
- Ask for
scorein 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? - 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?
- In the second panel, compare 1 person with 1000. Which format's cost for each person changes most, and why?
- 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.
