One record
Here is a person: a name, an age, a score. The name is always there. The other two may be missing.
Person:
name: text
age: a number from 0 to 255, optional
score: a number up to about four billion, optional
Chapter 2 showed how to write each value. Put the three one after another and you have bytes, but not yet a record anyone can read: nothing says where one value ends, which values are present, or which is which.
Four answers
- By name. Write the name of every field next to its value. JSON does this in text. CBOR and MessagePack do it in binary. The data explains itself, and nobody needs a schema.
- By a numbered label. Agree on a schema first, in which every field has a number. Write that number, in a byte or two, in front of each value. This is Protocol Buffers.
- By a table. Put the values in fixed-width slots, and in front of the record a small table that says where each field's slot is. This is FlatBuffers.
- By position. Write the values in the order the schema lists them and nothing else, apart from a few bits to say which optional ones are missing. This is the oldest answer, a struct copied out of memory, and the leanest.
The panel writes the same person all five ways. Change the values, leave a field out, and pick a format. The colours are the ones this guide uses throughout: your data is numbers and text, and everything else was added by the format.
What to look for:
- Names are expensive. In JSON and CBOR the word
scorecosts more than the score. In one record that is tolerable. In a million records the same three words are written a million times. - CBOR is JSON with the punctuation made efficient. Quotes, colons and commas become one head byte in front of each thing, and numbers are binary. The names stay.
- A label is a name shrunk to a number. Protocol Buffers writes
1,2,3where JSON writesname,age,score, and the schema keeps the translation. That is where most of the saving comes from. - FlatBuffers is the largest binary form here, on purpose. It spends bytes on a table, on pointers, and on padding so that every number sits where a processor likes to read it. It is buying something other than size.
- The struct has almost nothing but your data. One byte of presence bits and one length.
Finding one field
Size is one cost. The other is what a reader has to do to get a single field out. Under the list of formats, choose what the reader wants: the bytes it never has to look at fade.
- By name, the reader compares names. It reads its way through the record, checking each name against the one it wants. JSON has to read every character on the way. CBOR can at least step over a value using its head byte.
- By label, the reader walks the labels, all of them. Ask Protocol Buffers for
age: the reader does not stop when it has found it. The format allows fields in any order and allows a field to appear twice, in which case the last one counts, so the only way to be sure is to walk to the end. In practice a Protocol Buffers message is usually decoded whole, into objects, before anything is read from it. - By table, the reader makes a few jumps. Ask FlatBuffers for
score: the start of the buffer says where the record is, the record says where its table is, the table says where the score is. The same handful of bytes whether the record has three fields or three hundred. This is what the extra size bought, and it is why such a buffer can be used straight from disk or the network without a decoding step. - By position, the reader computes. It knows the order and the widths. Text and missing fields are the only things that make it look at anything else.
A year later
So far the struct looks like the winner: the smallest, and quick to read. Its weakness shows when the schema changes, which every schema eventually does.
Suppose version 2 of the program adds an email to the person. New programs write it. Old programs, which know only name, age and score, are still running and will be handed the new bytes. Below, a new writer writes Ada with her email, and an old reader reads the result. Choose where in the schema the new field was added.
- By position, a field in the middle breaks everything after it, silently. The old reader takes the first bytes of the email for the age and the next four for the score. No error is raised, because nothing in the bytes says what they are. Added at the end, it happens to work here, but only because the record is the last thing in the buffer. Inside a list of records, the leftover bytes would be taken for the start of the next one.
- A table has the same weakness one level up. The entries of the table are in schema order, so a field added in the middle moves the others to different entries, and the old reader looks in the wrong ones. Added at the end it is safe wherever the record is: the old reader never looks at the entry it does not know. That is why FlatBuffers asks you to add fields only at the end, or to number them yourself.
- A label does not care. The number is the field's identity, and it does not change when you move a line in the schema file. The old reader meets label 4, does not know it, and steps over it. Field numbers make this the most forgiving of the three, as long as nobody ever reuses a number.
- A name does not care either, for the same reason. A JSON or CBOR reader ignores a name it does not know.
What each answer pays
| by name | by label | by table | by position | |
|---|---|---|---|---|
| needs a schema | no | yes | yes | yes |
| overhead for each field | its name | a byte or two | a table entry, shared | about a bit |
| getting one field | walk, compare names | walk the labels | a few jumps | compute |
| adding a field later | anywhere | anywhere | at the end | not safely |
"Shared" in the third row matters more than this panel can show. A thousand records with the same fields present can all point at one table, so its cost is paid once. You will see that happen in chapter 5.
No column wins every row, and that is the point of this chapter. A format that picks one answer picks its weaknesses too.
Where Dagr stands
Dagr always has a schema, so it does not write names. For data whose shape is not known in advance there is another way, in chapter 10. Among the other three answers it does not pick. It offers each of them as a layout that you choose for each type:
- regular is the table,
- packed is the label, with one difference that chapter 6 explains: its labels are positions, so new fields go at the end,
- frozen, with or without packed, is position.
So a type that is finished can go by position, a message that will grow can go by label, and a large record you want to pick single fields out of can have a table, all in one buffer. Chapter 5 shows them in Dagr's own bytes.
What these panels are.
- The bytes are the real formats'. They are computed in your browser, and the same code is checked against
flatc,protocand thecbor2library: for the records the check tries, the bytes are identical. The "bare struct" is ours, made up for this chapter. - Each format is shown in its plainest use. All of them have options this chapter ignores, and Cap'n Proto, which reads by position and still lets a schema grow at the end, is left for chapter 7.
- The faded bytes are a count, not a measurement. Fewer bytes looked at is not the same as faster.
Try it
- Give Ada a score of 5, then of 4,000,000,000. Which formats change size, and by how much?
- Untick
age. What does each format write for a missing field? Which of them writes something? - Make the name twelve letters long. In FlatBuffers, watch the padding change as you type. What is each run of padding lining up?
- In the second panel, the old struct reader gets a wrong age. Work out from the bytes which byte it read, and what that byte really is.
- Protocol Buffers survives a field added anywhere. Think of a change to the schema that it does not survive.
What comes next
A record with a list of records inside it is a tree, and all four answers extend to trees without much trouble. Real data is often not a tree: two orders refer to the same customer, a person's friend has that person as a friend. Chapter 4 is about what happens then, and chapter 5 is where Dagr's layouts appear in full.
