DagrA guide to binary formats, and to Dagrdraft

17 Chapter seventeen

Compression: what it buys and what it takes

Much of the data that travels is compressed on the way, so the size of a format is rarely the size that is sent. A fair comparison has to include that. So does a fair account of compression: it is paid for in time, and it takes away some of the things a binary format was chosen for. This chapter is about what a compressor does, what it costs, and how to use one with Dagr without losing what Dagr gave you.

Why data is compressed at all

Data written for convenience repeats itself. A file of JSON records writes every field name in every record. A log writes the same few words on every line. A compressor is a program that finds such repetition and writes it more briefly, and a second program that undoes this exactly.

It is a good bargain when bytes are dearer than processor time: on a slow connection, a metered one, or a disk that is filling up. It is general, too. One compressor works on any data and needs no schema.

Two ideas

Nearly every compressor in common use is built from two ideas, and neither is difficult.

Say "the same again"

The compressor remembers what it has already seen. When the next few characters have appeared before, it does not write them. It writes a note instead: go back this far, and copy this many. Text that has never been seen is written out as it is.

Below is that first idea on its own. The marked parts are copies. Point at one to see where it is copied from, and change the text.

What a compressor would write

Short codes for common things

The second idea is older. Morse code gives the letter E a single dot and Q four signals, because E is sent far more often. A compressor counts how often each byte and each kind of note occurs, and gives the common ones codes of a few bits and the rare ones longer codes. The count, or the table of codes made from it, travels in front of the data.

The familiar names are these two ideas with different engineering. gzip, from the 1990s, is both of them with a memory of 32,000 bytes. zstd is a newer design with a far larger memory and a faster way of making the codes, and for the same size it is several times quicker in both directions. Brotli, common on the web, also arrives knowing a list of words that web pages tend to contain.

What it costs

Much of what the earlier chapters built depends on knowing where a thing is. A pointer says how many bytes away its target lies. A reader follows it and touches nothing else, which is how three fields are read out of a large file. A doubly-linked sink can be read from its end. A file can be mapped into memory and used where it lies.

A compressed file has none of this. The byte you want is somewhere in the output of the decompressor, and the only way to reach it is to decompress everything before it. Whatever the format inside could do, the reader's work now grows with the size of the file.

How much does it save? One real log

The numbers below are measured, not estimated. The data is the commit history of Dagr's own repository: 815 entries, each with an identifier of 20 bytes, a time, a line of description, and three counts. It is written once as JSON Lines and once as a Dagr sink, and each is compressed in several ways.

A dictionary, for small messages

There is a cure for the small message. Take a sample of typical messages, let the compressor work out what they have in common, and save that as a dictionary. Both sides keep a copy. Every message is then compressed as if the dictionary had come just before it, so even the first record finds its field names already seen.

The second view of the panel shows it. With a dictionary, JSON one record at a time comes down to the size of plain Dagr. Dagr with a dictionary gains only a little more.

A dictionary is a small schema arrived at by statistics. It has to be distributed to every reader, it has to be the same version on both sides, and it fits less well as the data changes over the months. A schema says the same things on purpose, and Dagr's tools check them for you (chapter 16).

What was found on real systems

Two larger experiments with Dagr met these effects.

A stream of small events

The AT Protocol, which a number of social networks are built on, has a public stream of events: a post, a like, a follow, each a few hundred bytes. The widely used service that republishes this stream sends JSON and offers it compressed with zstd and a dictionary.

The study of that stream as Dagr found the same pattern as the panel above, on real traffic:

So for a stream of small, mostly structured messages, there was nothing left to compress for. That is the best case, and it is common: events, measurements, requests and replies between services.

A long conversation, opened at its end

The second experiment stored the conversation threads of an AI assistant inside a code editor. A thread is a sink: each message is appended, and a long one runs to megabytes of text. Text compresses well, so the stored thread was compressed, as one piece.

Measuring showed where the time went when a thread was opened. The editor shows the last screen of messages. The sink was doubly linked, which lets it be read from its end, so decoding those few messages took almost no time. Nearly all of the time was the decompressor working through the whole thread to reach them.

The cure is to compress in blocks. The records are grouped, around a hundred kilobytes at a time, and each group is compressed on its own, with its length in front. A reader that wants the end of the thread reads the last block and decompresses only that.

The panel below does this to the log from before. Choose how many records go into a block.

In the editor, the blocked form was a few percent larger than the single piece, and opening a thread stopped depending on its length. Blocks work because a sink's records stand alone (chapter 12): any run of records is a complete small sink, so a block can be cut anywhere between two records.

Which way to compress

sizeread a partappenduse in place
no compressionlargestyesyesyes
the whole filesmallestnocompress it all againno
each recordpooryesyesone record at a time
each record, with a dictionarygoodyesyesone record at a time
in blocksclose to smallesta block at a timeyesa block at a time

For a single graph, there is a further choice: compress only what is worth it. A graph's structure is already compact, and its bulk is usually a few large fields: a document's text, an image, a long list. Such a field can be stored as bytes that the program compressed itself, and the graph around it stays readable in place.

With Dagr, do you need it?

Dagr itself has no compression built in. Blocks, dictionaries and the choice of compressor are the business of the program around it, as checksums are. What the format contributes is that its pieces can be cut apart and put together again without being decoded.

What these panels are.

Try it

  1. In the first panel, choose the JSON records and add a fourth by hand. How much of it has to be written out?
  2. Choose the random letters. Why are there still one or two copies?
  3. Type the same letter fifty times. A copy can start one character back and be longer than that. How does the reader make sense of it?
  4. In the second panel, which of the bars would change if every description were twice as long? Which would not?
  5. In the third panel, find the smallest block that makes the file less than a tenth larger than one block. How much has to be decompressed to read the latest ten entries?

What comes next

Size is one of the two things people ask of a format. The other is speed. Chapter 18 is about where speed comes from, what can and cannot be done about it in a given language, and where Dagr stands in the measurements that exist.