DagrA guide to binary formats, and to Dagrdraft

1 Chapter one

Why bytes

Inside a running program, data is a web of objects: a list here, a record there, one pointing at another. None of that can be saved to a file or sent to another machine as it is. First it has to become one flat row of bytes. The rules for doing that, and for getting the data back, are a format.

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.

a charactera byte of the number itself

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.

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.

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:

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 reader wants:
texta mark betweena lengthunused roomfaded: not looked at

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.

  1. How is one value written? A number, a piece of text. Chapter 2.
  2. Where does a value end? You have just seen the three answers.
  3. Which value is this? A record has fields, and the reader must tell the name from the age. Chapter 3.
  4. 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.

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.

Try it

  1. What is the largest number that takes one byte in binary? How many characters does it take as text?
  2. 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.)
  3. 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?
  4. Ask for the last name in each form and compare how many bytes the reader looks at.
  5. 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.