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
- The first record pays in full. Nothing has been seen yet, so there is nothing to copy. The saving begins with the second record.
- Names repeat and values do not. In the JSON sample, the copies are the field names and the punctuation around them. That is the part of a text format that a schema would have removed in the first place.
- Random data does not compress. A hash, a key, a signature, or data that is already compressed has no repetition to find.
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
- Time, on both sides. Finding repetition means searching, and that is the slow half. Undoing it is quicker and still not free. For a service that sends many small messages, compressing them can be most of the work of sending them.
- Small messages gain little. A compressor learns from what came before, and a message of a hundred bytes has almost no before. Compressed alone, it can come out longer than it went in.
- Addresses. This is the cost that matters most to this guide, and the rest of the chapter is about it.
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.
- As written, Dagr is a little under three fifths of the JSON. The names are gone, the numbers are short, and the identifier is 20 bytes where JSON needs 40 characters.
- Compressed as one file, the two are nearly the same size. The compressor removes from JSON what the schema removed from Dagr. What is left in both is the descriptions, which are prose, and the identifiers, which are random. No format and no compressor can do much about either. This is an honest limit: where data is mostly free text, compression closes most of the gap between formats.
- One record at a time, it is different. Switch the panel. Compressed alone, a JSON record shrinks a little, and a Dagr record gets longer, because it has no repetition left to find. Plain Dagr, with no compressor at all, is well below JSON that has been compressed record by record.
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:
- An event as plain Dagr was smaller than the same event as JSON compressed with the dictionary.
- Compression was most of what the JSON service did for each event. Without it, the server's work per event was a small fraction.
- The reader of plain Dagr can use an event where it lies. The reader of the compressed JSON has to decompress and then parse.
- For a stored archive, where many events are compressed together, the two came out within a few percent of each other, as in the first view of the panel.
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.
- One block is the smallest file and the slowest to open. Move the slider to the right end. The file is as small as it gets, and reading the latest ten entries means decompressing all of it.
- Small blocks open instantly and compress badly. At the left end every record is its own block, which is the one-record-at-a-time case from before.
- In between, a small price buys most of both. With a few dozen records in a block, the file is some percent larger than the smallest, and the work of reading the end no longer depends on how long the log is.
- Appending stays appending. A new block goes after the last. Nothing that is already compressed is touched, where compressing a file as one piece means doing it all again on every save.
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
| size | read a part | append | use in place | |
|---|---|---|---|---|
| no compression | largest | yes | yes | yes |
| the whole file | smallest | no | compress it all again | no |
| each record | poor | yes | yes | one record at a time |
| each record, with a dictionary | good | yes | yes | one record at a time |
| in blocks | close to smallest | a block at a time | yes | a 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?
- Measure the plain bytes first. For small structured messages, plain Dagr is often already below compressed JSON, and nothing more is needed.
- If the data is mostly text, compress, and expect about the size that compressed JSON would have had. What Dagr still gives you is on the other side of the decompressor: nothing to parse.
- If you read parts of it, compress in blocks, and pick the block size by what one reader usually wants.
- If it is mapped into memory or shared between programs, leave it alone. An aligned array (chapter 14) or a shared buffer (chapter 15) is used at its address, and compressed data has none.
- Compare like with like. A table that sets compressed JSON beside plain Dagr flatters JSON, and one that sets plain JSON beside plain Dagr flatters Dagr. Dagr's own benchmarks report both sides plain and both compressed.
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.
- The first panel is a small program in this page that looks for the longest earlier copy of at least three characters. Real compressors search less thoroughly and add the second idea, the short codes, which the panel does not show.
- The sizes in the other two panels are measured by a script in the guide's repository, with Dagr's reference writer, gzip at its usual level and zstd at level 3. The page only displays them. Dictionaries are 4,096 bytes, made from the first 400 records and measured on the remaining 415. Each block carries 4 bytes for its length.
- It is one data set, and a text-heavy one. Yours will differ, which is the reason to measure.
Try it
- In the first panel, choose the JSON records and add a fourth by hand. How much of it has to be written out?
- Choose the random letters. Why are there still one or two copies?
- 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?
- In the second panel, which of the bars would change if every description were twice as long? Which would not?
- 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.
