DagrA guide to binary formats, and to Dagrdraft

18 Chapter eighteen

Performance: where speed comes from

Every format says it is fast. The claim means little until you know fast at what, compared with what, and in which language. Speed has three sources: the structure of the format, the engineering of the code that reads and writes it, and the language that code runs in. This chapter takes them one at a time, lets you measure one case in your own browser, and then shows where Dagr stands in the benchmarks that exist, including the ones it loses.

What takes the time

Reading a message is several jobs, and a program pays for each one separately.

Writing is the same list backwards. In nearly every measurement, making objects is the largest part and converting the second. So the fastest code is mostly the code that manages not to do one of these jobs at all.

The first source: structure

The format decides which of those jobs can be left out. This is decided when the format is designed, and no later cleverness changes it.

Structure also sets limits in the other direction, and Dagr has chosen some of them. A packed node (chapter 6) is small because it has no table, so reaching its tenth field means stepping over nine. Numbers of variable length are smaller and take a little longer to read than fixed ones. A format that is simply the program's memory written to disk reads faster than Dagr, and gives up size, portability and the freedom to change. Every one of these is a trade, and the right one depends on what the data is for.

The second source: engineering

Within what the structure allows, an implementation can still be several times faster or slower than another. JSON is the best example. Its structure is against it on every point above, and decades of work have made its parsers remarkably fast anyway: they examine many characters at once with special processor instructions, avoid branches, and convert numbers with carefully proved shortcuts. Such a parser gets through a gigabyte of text in about a second.

What engineering cannot do is leave the structure. The best JSON parser in the world still has to look at every character to find the last field.

It also works the other way round: a good structure can be wasted. Two examples from Dagr's own history, in Go, with not one byte of the format changed:

So a benchmark measures a format and an implementation together, and it cannot tell you which of the two you are looking at. A slow result may mean a slow format or code nobody has profiled yet. A fast one may mean a fast format or ten years of tuning.

The third source: the language

The same design, engineered with the same care, does not run equally fast everywhere. In C++, Rust, Go or Swift, the code a generator writes is compiled to machine instructions, and a view into a buffer costs almost nothing. In two very widely used languages it is different.

JavaScript and TypeScript

JSON.parse and JSON.stringify are not written in JavaScript. They are part of the engine, written in C++ by the people who wrote the engine, and they can do things no JavaScript program can: create objects in one step, skip the checks that ordinary code passes through, and use the processor instructions mentioned above. A reader for any other format, Dagr's included, is ordinary JavaScript. However good its structure, it starts behind when the task is "turn the whole message into objects".

The obvious answer is to write the reader in a compiled language too and call it from JavaScript. That is not always possible, and it helps less than one would hope. A web page cannot load a native library at all. It can load WebAssembly, but every string and object still has to be carried across the border between the two worlds, and that crossing costs about what was saved. A server can load a native module, at the price of building it for every platform.

What remains is the structure. A JavaScript reader that does not turn the whole message into objects can win, because the built-in parser has no way of doing less than everything.

The panel below measures this on your device. It makes a list of people, writes it as JSON and as Dagr, and then does four tasks both ways. The Dagr side is the TypeScript that Dagr generates, unchanged.

the browser's own JSONDagr's generated TypeScriptDagr, building the whole list firstshorter is faster

The numbers differ from one device and browser to the next. On the machines this was tried on, the pattern was the same:

The third bar of that first task shows the same read done a slower way: ask for the whole list, which makes a reader object for every person, and then take one. Until this chapter was written, that was the only way Dagr's TypeScript offered. Measuring for this page showed that a read the format makes almost free cost as much as parsing the JSON. The generator was given a way to read one entry directly, and the bar next to it is the result. Not one byte of the format changed. That is the difference between the first two sources of speed: the structure allowed the fast read all along, and it took engineering to deliver it. From the JSON side, no amount of engineering can do the same, because the text has no table to look in.

Python

Python runs its programs through an interpreter, which is tens of times slower than compiled code at work like this: looking at bytes one by one, shifting bits, following offsets. Python's fast libraries are fast because they are not Python. Its json module is written in C, and so are the popular Protocol Buffers and JSON packages.

Dagr's Python target is pure Python, and it is the slowest thing in this chapter. That is partly the interpreter and partly a decision: it is Dagr's reference implementation. It does not generate code for a schema. It reads the schema while it runs and decides, field by field, what to do, and it always turns the whole buffer into objects. That makes it short, easy to check against the specification, and the writer that all the other languages are tested against (chapter 19). It was written to be right, and nothing in it was written to be fast.

The way to get the speed back is the way every fast Python library takes: do the work in compiled code, and let Python call it. For Dagr that means the generated Rust behind a thin Python layer. In the signed-token project below, the pure Python version checks a token several times more slowly than the usual Python JWT library. The same token checked through Rust is nearly three times faster than that library. Dagr can also generate such a library itself, for reading graphs and sinks into the column tables that Python's data tools work on. A general Python interface over generated Rust, for any schema, does not exist yet.

Where Dagr stands

Below are six sets of measurements. Two are Dagr's own benchmark, in Rust and in C++ and Java. Three come from projects that put Dagr to work on something real, and one is run by somebody else. Each bar is a factor: how many times faster or smaller Dagr was than the thing it was compared with, or how many times slower or larger.

to the right: Dagr aheadto the left: Dagr behindlevela longer bar is a larger factor, and a full half is a hundredfold

Six sets of numbers from different machines cannot be added up, but they agree with each other on the following.

Reading these numbers

What follows from this

What these panels are.

Try it

  1. Measure with 200 people and with 20,000. Which bars grow a hundredfold, and which one hardly moves? What does that say about the work each does?
  2. Adding up the ages is about level, and reading one age is not. What would Dagr's TypeScript have to stop doing for the first to look more like the second?
  3. In the "add up everyone's age" task the JSON side reads every name. Is there any way to write that task, using JSON.parse, that does not?
  4. If you have a second browser, measure there too. Which results change most, and are they the JSON ones or the Dagr ones? Why might that be?
  5. In the second panel, find a task where Dagr is ahead in one language and behind in another. Which of the three sources explains it?
  6. In the telemetry set, reading one span out of 500 is much further ahead than visiting all of them. What has the reader not done in the first case that a decoder always does?
  7. A service receives a message, changes one field and sends it on. Which of the measurements here tells you most about how Dagr would do, and which tells you least?

What comes next

The guide has now described the whole format and how it performs. Two chapters remain. Chapter 19 is about how Dagr was built: specifications first, tests that compare bytes, and an AI doing most of the typing. Several of the measurements in this chapter were made the same way.