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.
- Who decides how a number is written. In Protocol Buffers the schema decides, once, for every value that field will ever hold:
uint32means LEB128,fixed32means four bytes. Choose "large values": a count of four billion takes five bytes as LEB128, one more than the number is wide. In Dagr the writer decides for each value. It writes whichever of the two is shorter, and one bit of the tag says which. - Negative numbers. With everyday values
deltais-3, and Protocol Buffers'int32writes ten bytes for it, the problem from chapter 2. Tick the box to declare the fieldsint32and it folds the number first. That is a fine fix if you knew, when you wrote the schema, that the field would go negative, and you cannot change your mind later. Dagr folds every signed number. - Floats. Protocol Buffers writes a
floatas its four bytes, always. Dagr looks at the value: zero is a single byte, a whole number such as21is written as a whole number, and a value that a 16-bit float holds exactly, such as21.5, is written as one. Try21.37, which is none of those: four bytes, as in Protocol Buffers. Chapter 2 has the whole list of forms. - Lists. Both write a list of numbers as one value: a length, then the elements. Dagr adds the number of elements, so a reader can make room before it reads them, and writes each element the shorter way. If some elements are short and some are not, that costs a bit per element to say which. Chapter 9 is about lists of every kind.
- Two bytes at the front. Dagr's version begins with a framing byte, which says what kind of buffer follows (chapter 11 puts it to use), and the length of the node. A Protocol Buffers message has neither. It does not say where it ends, so whatever carries it has to. Choose "nearly empty" to see how much those two bytes weigh when there is little else.
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.
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.
- The Dagr reader stops when it arrives. Ask for
count: it steps oversensor, readscount, and is done. Hadcountbeen absent, it would know as soon as it met a higher tag. - The Protocol Buffers reader goes to the end, whatever you ask for and however the message is ordered. The field could still be coming, or coming again.
- Any order has its uses. Two Protocol Buffers messages glued together are a valid message, the merge of the two, and a writer can emit fields as it learns them. Dagr gives both up.
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.
- Fields can only be added at the end. Insert one in the middle and every later field has a new position, which is a new label. Removing or reordering does the same. A field you no longer want stays in the schema, marked as deprecated, to keep its place. Dagr's
checkcommand compares a schema with the last one you built from and reports a change that would break old data, but the rule is still yours to live with. - The bytes mean nothing without the schema. A Protocol Buffers label carries three bits that say what kind of value follows, so any tool can split any message into its fields without knowing what they are. Dagr's tag carries one bit. Whether the bytes after it are a four-byte number or a length is something only the schema says.
- So unknown fields are kept as a lump. An old reader that meets a tag beyond its schema cannot step over that one field, since it cannot tell how long it is. It does not have to: tags only go up and the node says how long it is, so everything from that tag to the end of the node is newer than the reader. It treats the rest as one piece, and can write it back unchanged.
- Everything around the format. Protocol Buffers has two decades of tools, an RPC system, and an implementation in any language you will meet. Dagr is young.
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 Buffers | Dagr, packed | |
|---|---|---|
| a label is | a number someone chose | the field's position |
| order of fields | any, repeats allowed | schema order, once each |
| how a number is written | fixed by the schema | the shorter way, for each value |
| new fields | any free number | at the end only |
| readable without the schema | the structure, yes | no |
| one field out of many | read to the end | read up to it |
| a message knows its own length | no | yes |
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.
- The bytes are computed in your browser. The Dagr side is a port of the packed part of Dagr's reference writer, for exactly these five field types. A check that ships with this guide compares it with the reference writer, and the Protocol Buffers side with
protoc, over a few hundred messages: the bytes are identical. - The Protocol Buffers fields are declared
optional, so that a zero is written and not left out. Without it, proto3 omits any field that holds its default. - The faded bytes are a count of what must be looked at, not a measurement of speed.
Try it
- Find the smallest
countfor which Dagr writes four plain bytes. What does Protocol Buffers write for the same number? - Set the samples to
1, 2, 300, then to1, 2, 3, then to300, 400, 500. Dagr's list changes shape each time. What are the three shapes? - Find a temperature that Dagr writes in one byte, in two, in three and in four.
- 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? - You want to delete
deltafrom 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.
