A row of bytes
A byte is a number from 0 to 255. A file is a row of them. So is a message on a network, and so is a block of memory. Whatever you store or send ends up as such a row, and somebody has to decide which bytes mean what.
The most familiar decision is to write everything as text, the way a person would type it. JSON does that. The other is to write values the way the machine already holds them. That is what "binary format" means, and it is no more mysterious than that.
Type a whole number and see it both ways.
- As text, a number is its digits. Each digit is a character, and each character is a byte:
1is byte 49,8is byte 56. A reader has to turn them back into a number, digit by digit, multiplying by ten as it goes. - In binary, the bytes are the number. A byte holds 256 different values where a digit holds ten, so fewer are needed, and a processor can use them as they are.
- The difference grows with the number. Try
4000000000: ten characters, four bytes.
That is the whole advantage in miniature: smaller, and less work to read. The rest of this guide is about what it takes to keep that advantage for data more interesting than one number.
What text is good at
A guide to binary formats should say this early: text formats are good, and for most data they are the right choice. The reason is one word, convenience, and it is convenience for people more than for programs.
- You can read them. Open the file and there is your data. When something goes wrong, the first tool you need is a text editor, and you already have one.
- You can write them by hand. A test case, a configuration, a request typed into a terminal to see what comes back.
- Everything understands them. Every language reads JSON. Every editor opens it, every search tool searches it, every version control system can show what changed.
- They are forgiving to change. Add a field and old programs mostly carry on.
This is a large part of why the web's own protocols spread as they did. A request to a web server was a few lines of text, and anyone could type one and read the answer. Nothing had to be installed to understand what was going on.
And then it has to be fast
Once a text format is everywhere, its cost is everywhere too, and a great deal of work goes into reducing it afterwards.
- Parsers of remarkable ingenuity. There are JSON parsers that examine sixty-four characters at a time with the processor's vector instructions, to find the quotes and brackets faster.
- Compression on nearly every connection, to take out again the repetition that writing every name in every record put in. Chapter 17 is about what that costs.
- Binary underneath. The newer versions of the web's protocol are binary. The text is still what people see, because the tools show it that way.
That last point is the one this guide builds on. What makes text convenient is that the tools for it already exist. A binary format with good tools can be opened, searched, compared and merged as well, and chapter 20 shows the ones Dagr has. Speed added to a text format afterwards has limits that a format designed for it does not.
Binary formats pay when one of these starts to hurt:
- Size, when there is a great deal of data, or a slow or costly connection.
- Time, when turning text into values and back is a visible share of the work.
- Reading a little of a lot, when you want three fields out of a large file and do not want to parse the rest to find them.
- Exactness, for raw bytes such as an image or a key, and for numbers that do not survive a trip through decimal digits unchanged.
If none of these hurts, use JSON and close this guide with a clear conscience.
The first hard question: where does it end?
One number was easy. Put two things in a row and a question appears that will come back in every chapter: where does the first one stop?
There are three answers, and every format uses some mix of them. Here are a few names stored each way. Change them, and ask for one of them.
- A mark between. Text formats do this: a comma, a quote, a line end. It costs one byte each, and the reader must look at every byte to find the marks. It also raises a new problem: what if the data contains the mark?
- A length in front. The reader knows how far to go and can step over what it does not want. Most binary formats do this for anything whose size varies.
- The same size for all. No marks and no lengths: the third name is simply at three times the size. Nothing is faster to find. It wastes room on short values and cannot hold a long one.
Small, flexible, quick to find: you get to favour two. Most of what distinguishes one binary format from another is where it makes this trade, and for which parts of the data.
Four questions
Every format, text or binary, has to answer four questions. The guide takes them in order.
- How is one value written? A number, a piece of text. Chapter 2.
- Where does a value end? You have just seen the three answers.
- Which value is this? A record has fields, and the reader must tell the name from the age. Chapter 3.
- How does one thing refer to another? Real data is not a list of records. Things contain and point at other things. Chapter 4.
Three kinds of data
Those four questions are asked of one message. Data in real systems comes in more than one kind, and the three most common are usually served by three unrelated technologies.
- A value that is built and then read: a document, a request, a configuration, a tree or graph of records. This is what serialization formats such as JSON and Protocol Buffers are for.
- Records that keep arriving: a log, a stream of events, a journal that is only ever added to. This is usually a file of lines, or a format's messages wrapped in a convention the project invents.
- Memory that programs share: one region that several programs or threads read and change at the same time. This is usually a struct in C and a great deal of care.
Dagr's premise is that these are one subject. It has a container for each, a DataGraph, a DataSink and a SharedBuffer, and all three are described in the same schema language, generated by the same tool for the same languages, and watched by the same check when a schema changes. Graphs and sinks also write their bytes by the same rules and are opened by the same tools, so a type defined for a graph can be a record in a stream. A SharedBuffer has to lay out its bytes differently, for reasons chapter 15 gives.
So Dagr is best understood as a model for these three kinds of data together, with the tools that make each of them practical, and not as a more compact replacement for one existing format. Most of what it does for a single message, some other format does as well or better, and the chapters say where. The combination is what the others do not have.
After those four chapters the guide goes on to Dagr itself: the four layouts Dagr offers for a record and why there are four (chapters 5 to 7), choices and lists (8 and 9), data with no fixed shape (10), data that must be trusted (11), the sink, for data that never ends (12) and can show that its past was not changed (13), data for processors that are particular about addresses (14), the shared buffer, for memory that two programs share (15), and what happens when a schema has to change (16). The last four chapters step back: what compression adds and takes away (17), where speed comes from and where Dagr stands (18), how Dagr was built with an AI and tests that compare bytes (19), and the tools that make a binary format as convenient as text (20).
Along the way Dagr is set next to the formats you may already know: JSON, CBOR, Protocol Buffers, FlatBuffers, Cap'n Proto. The aim is to show what each one chose, and where each is the better choice.
How the guide works.
- Every chapter has panels like the two above. Most compute real bytes in your browser, and for the floats in chapter 2 and everything from chapter 3 on, those bytes are checked against the formats' own tools, so what you see is what the real thing would write. A few are drawings, and the later chapters display output recorded from Dagr's own tools. Each chapter says which.
- The second panel here is a picture of three ideas, not of any particular format.
- Colours mean the same thing everywhere: your own values in green and violet, and what a format adds around them in the other colours.
Try it
- What is the largest number that takes one byte in binary? How many characters does it take as text?
- Type
4294967295, then add one. The binary form jumps from four bytes to eight. Why not five? (Chapter 2 is about not having to jump.) - In the second panel, type the single name
Lovelace, Ada. The panel itself is fooled. Which of the three forms could hold that name without trouble? - Ask for the last name in each form and compare how many bytes the reader looks at.
- Add a very long name. Which form refuses to hold it, and what did that form give you in exchange?
What comes next
Chapter 2 takes the first question seriously: the ways to write a whole number so that small ones are short, and how text becomes bytes.
