Compression: prepare, then a fast zstd
Mova keeps big stores: PubMed Central texts, OpenAlex works, math web text, Wikidata. They were stored as they came: gzip tars, parquet with snappy or zstd, bz2, jsonl.zst. mova-pack (pack/) is the storage layer that prepares the data first and then compresses it with a fast zstd level. This page says how it works, how to run it, what it measured on our data and where it stops.
The idea
A general compressor finds repeats inside a window. It does not know that a JSON record is a fixed set of keys, that a field holds one of six values, or that two files of the same site share their boilerplate. Preparation tells it:
- Templates. A JSON record becomes a template (its keys, braces, quotes, commas, spacing) from a global template dictionary, plus its values. The dictionary is stored once per dataset; a record carries a template id.
- State header. One template covers a family of near-identical records. The header line of a record is a small state tuple: the template id, one token per array or map (how many elements, which element template), and the exceptions to the slot defaults (this slot is null here, that one is a number). A slot whose value is almost always the same costs nothing: the dictionary holds that value as the slot's default.
- Typical values. Frequent values of a field (types, languages, licences, venue names, keys of map-like objects such as language codes or Wikidata property ids) get short ids from a per-field value dictionary.
- Value lines. Everything else is written one value per line, in template slot order. JSON strings cannot hold a raw newline, so no escaping is needed. Strings keep their exact escaped bytes; when a dataset writes non-ASCII as
\uXXXX(Wikidata does), values are stored decoded to UTF-8 and re-escaped canonically on the way back, with the rare non-canonical value kept raw. - Grouping. Similar records next to each other: by site for web text, by id for catalog rows.
- Token files as streams. Mova's parsed corpora (
.tok: comment lines# key = valueand token rows FORM LEMMA UPOS HEAD DEPREL FEATS;.conllu) are cut apart: each comment key has its own value stream, each token column its own stream. Columns with few values (UPOS, DEPREL, FEATS,# prag,# pragrow) use a per-block table of typical values; a lemma is a code (same as the form, its lowercase, or written out); HEAD is the distance to its row. Values the context fixes are predicted and cost nothing:# sentis the previous one plus one,# sent_idfollows from the previous one or from# doc, and# textis spelled from the sentence's forms with one bit per gap (a space or none). - A trained zstd dictionary for small blocks and single records.
- Then zstd at level 9 (7 is the fast option) over the prepared text.
The format is exact: unpacking gives the original bytes, every time, checked against the original's sha256.
Every container is cut into blocks of whole records (documents for token files, lines for JSON lines and TSV), about 1 MiB of original text each, and each block decodes alone. A separate offset index, <name>.idx.zst, maps record keys to the block and the offset inside it, so one document is read by decoding one index chunk and one block.
How to run it
cargo build --release -p mova-pack
mova-pack pack works.jsonl works.mpk # verify, then write
mova-pack get works.mpk 12345 # one record, one block decoded
mova-pack unpack works.mpk works.jsonl
mova-pack dict --kind json --out DICTS part-*.jsonl.gz # one dictionary for a dataset
mova-pack pack part-0007.jsonl.gz part-0007.mpk --tdict DICTS/<sha>.tdict --zdict DICTS/<sha>.zdict --dicts DICTS
mova-pack repack part-*.jsonl.gz --dicts DICTS # in place, originals moved aside
mova-pack gc STORE --dicts DICTS # dry run; --apply removes
mova-pack repack train.tok.zst --min-free 100 # train.tok.mpk + train.tok.idx.zst
mova-pack cat train.tok.mpk | READER - # the original, byte for byte, as a stream
mova-pack get train.tok.mpk --id "Ada Lovelace" # one document by its # doc
mova-pack repack rows.jsonl --id json:/id --id doc=json:/source/doc # two named keys
mova-pack get rows.jsonl.mpk --key doc=D17 # every record of one document
pack and repack write the streaming container (format 2: one pass, bounded memory, any input size) and its index; the kind is detected (tok, json, lines) and the preparation is kept only when it makes the sample at least 3 % smaller. Keys are given with --id [NAME=]SPEC: tok:doc (the default for token files), tok:sent, json:KEY, json:/pointer, tsv:N, prefix:TEXT. --min-free GB keeps a floor of free space on the target filesystem; --source PATH reads an original that was already moved elsewhere (for example to another filesystem while the new container is written).
repack writes the new container beside the old file, reads it back in full and checks its content hash (and looks up a sample of records through the new index), then moves the original to .pack-aside/ and renames the new files into place; every step goes to .pack-aside/journal.tsv. gc removes moved-aside originals only after the grace period (7 days by default) and only when the replacement still carries the journaled hash; it also removes stale temporary files, failed candidates and dictionaries no container references. Without --apply it only prints.
The measurements below come from mova-pack bench (build with --features parquet for the parquet samples).
Measured (2026-11-11)
One representative unit of each store, measured on one core of the shared box while other jobs ran, so speeds are rough. Sizes, hashes and verdicts are deterministic: repeated runs gave byte-identical report.json files (PMC and Wikidata twice, OpenAlex and FineMath three times). Every row checked its round trip byte for byte; every prepared and dictionary row ran a negative control (a deliberately broken decoder, a corrupted dictionary) and it went red. Ratio = canonical bytes / stored bytes. "Global" dictionaries (templates, typical values) are counted once at zstd-19.
Samples:
- PMC:
comm-txt-00100.tar.gz, 5 289 articles, 303.7 MB tar (the canonical original). - OpenAlex: one works-slim partition (
updated_date=2026-10-12/part_0000.parquet), 304 973 rows. Parquet cannot be rebuilt byte for byte, so the canonical original is a deterministic JSON-lines export of every column (317.1 MB). - FineMath:
finemath-4plus/train-00000-of-00064.parquet, 104 680 pages, export 651.6 MB. - Wikidata: the first 1 GB of
latest-all.json.bz2(23 860 entities, cut at a line end); the current size is bzip2 -9 of that prefix. - A quick check on Proof-Pile 2:
open-web-math/validation-val.jsonl.zst(295 MB raw).
Solid (whole unit in one compressed stream)
| store, unit | current | zstd-7 | zstd-9 | zstd-19 --long | prepared + zstd-7 | prepared + zstd-9 | best prepared + zstd-9 | prepared + zstd-19 --long |
|---|---|---|---|---|---|---|---|---|
| PMC tar (gzip) | 99.6 MB, 3.05 | 84.6 MB, 3.59 | 82.5 MB, 3.68 | 70.6 MB, 4.30 | 83.5 MB, 3.64 | 81.5 MB, 3.73 | 81.5 MB, 3.73 (streams) | 70.2 MB, 4.33 |
| OpenAlex (parquet zstd) | 54.7 MB, 5.80 | 56.2 MB, 5.64 | 54.1 MB, 5.86 | 43.4 MB, 7.31 | 49.1 MB, 6.46 | 47.4 MB, 6.69 | 43.1 MB, 7.36 (id-sorted columns) | 39.7 MB, 7.98 |
| FineMath (parquet snappy) | 286.3 MB, 2.28 | 172.4 MB, 3.78 | 167.2 MB, 3.90 | 135.4 MB, 4.81 | 170.4 MB, 3.82 | 165.4 MB, 3.94 | 145.8 MB, 4.47 (host-grouped columns) | 133.8 MB, 4.87 |
| Proof-Pile 2 OWM (jsonl.zst) | 87.4 MB, 3.38 | โ | 77.4 MB, 3.81 | โ | โ | 77.5 MB, 3.81 | 76.0 MB, 3.88 (columns) | โ |
"Prepared" is the main variant: template lines (family) for JSON, front/body/references streams grouped by journal for PMC. Compression speed of the prepared rows on this box: 9-60 MB/s per core including preparation (plain zstd-9: 26-44 MB/s; zstd-19 --long: 1.3-3 MB/s); decompression 80-550 MB/s (plain: 450-1 500 MB/s).
How much of the bytes is pure JSON structure (keys, braces, quotes, commas, literals): OpenAlex 42.6 %, Wikidata 51.6 %, FineMath 3.7 %, OWM 0.4 %, a PMC metadata shard 16.6 %.
Parquet rewritten (DuckDB, the same files)
| as stored | parquet zstd-9 | parquet zstd-9, sorted | parquet zstd-19, sorted | |
|---|---|---|---|---|
| OpenAlex partition | 54.7 MB (zstd, default level) | 47.0 MB | 43.1 MB (by id) | 38.7 MB |
| FineMath file | 286.3 MB (snappy) | 159.7 MB | 145.0 MB (by host, url) | โ |
COPY (SELECT * FROM f ORDER BY ...) TO g (FORMAT parquet, COMPRESSION zstd, COMPRESSION_LEVEL 9), 4 threads, one run each. Row order changes; the rows do not.
Record access (held-out 20 %)
Records with index % 5 == 4; dictionaries trained on the other 80 %.
| PMC article | OpenAlex row | FineMath page | |
|---|---|---|---|
| per record, zstd-7, no dictionary | 2.95 | 1.79 | 2.62 |
| per record, zstd-9 + global dictionary | 3.35 | 4.27 | 3.62 |
| per record, zstd-7 + per-domain dictionaries | 3.25 | 4.15 | 3.54 |
| per record, template lines + zstd-9 + dictionary | โ | 4.84 | 3.67 |
| 256 KiB blocks grouped by domain + dictionary, zstd-7 | 3.32 | 5.13 | 3.84 |
| solid zstd-7 (no access, reference) | 3.46 | 5.50 | 3.79 |
mova-pack container, 1 MiB blocks, zstd-9 (whole unit) | 3.47 | 6.06 | 3.68 |
A get decodes one block (up to 1 MiB of record text): 0.8-15 ms per record on this box, the slowest being OpenAlex template decoding.
Wikidata (first 1 GB)
1 000.0 MB of entity lines, 23 860 entities. Two runs gave identical reports.
| variant | bytes | ratio | compress MB/s | decompress MB/s |
|---|---|---|---|---|
| current: bzip2 -9 | 101.2 MB | 9.88 | โ | โ |
| zstd-7 / zstd-9 plain | 104.1 / 100.4 MB | 9.61 / 9.96 | 141 / 71 | 1 190 / 733 |
| zstd-19 --long plain | 79.5 MB | 12.58 | 2.5 | 1 497 |
| template lines (family) + zstd-7 / zstd-9 | 88.0 / 85.9 MB | 11.36 / 11.64 | 9 | 80-86 |
| template lines (exact) + zstd-9 | 87.9 MB | 11.38 | 7 | 51 |
| typed columns + zstd-9 | 84.9 MB | 11.78 | 11 | 93 |
| template lines (family) + zstd-19 --long | 74.1 MB | 13.49 | 3.9 | 88 |
mova-pack container, 1 MiB blocks, zstd-9 | 92.6 MB | 10.80 | 10 | 120 |
| per entity, zstd-9 + dictionary (held-out) | 8.37 | 46 | 983 | |
| per entity, template lines + zstd-9 + dictionary (held-out) | 9.89 | 51 | 134 |
The global template dictionary is 1.33 MB compressed (counted once): Wikidata's labels, descriptions, aliases, sitelinks, claims, qualifiers and reference snaks are maps (more than 32 distinct keys), so their keys travel as values and one template covers each statement shape; without maps the exact templates take 8.8 MB. \uXXXX escapes are stored as UTF-8. Reading one entity takes 28-32 ms (one block decoded).
What mattered
- The codec, not the preparation, for text. gzip โ zstd-9 saves 17 % on PMC, snappy โ zstd-9 saves 42 % on FineMath. On prose, preparation adds 1-4 %; only a larger window (zstd-19 --long, at 1-3 MB/s) adds another ~20 %.
- Grouping similar records. FineMath grouped by site: โ9 % at the same level. OpenAlex sorted by id: โ5 % on top of columns (ids become small deltas). This is the biggest single transform for web text and catalog rows.
- Templates for JSON. Against plain zstd-9 of the same JSON: OpenAlex โ12 % (template lines) and โ16 % (typed columns), Wikidata โ14 % and โ15 %. The structure is 43-52 % of the raw bytes, but zstd already removes most of it; what the template takes away is the remainder. Per record the template matters more: a Wikidata entity alone compresses 18 % better as template lines than as JSON with the same kind of trained dictionary.
- Trained dictionaries matter for small units only. Per OpenAlex row: 1.79 โ 4.27 with a dictionary; with 1 MiB blocks or whole files: 0-4 %. Per-domain dictionaries (top 32 domains) did not beat one global dictionary on held-out records once their own size is counted; without it they gain 1-7 % (OpenAlex 4.20 โ 4.41, Wikidata 8.21 โ 8.79), which only a whole store could amortize.
- Level 9 vs 7: 2-4 % smaller, at about half the compression speed of 7 on this box; decompression is the same.
So the hypothesis "prepare the data right and zstd-7 matches zstd-19" holds for structured catalog data (OpenAlex: id-sorted columns at zstd-7, 43.9 MB, equal the plain zstd-19 --long, 43.4 MB, at ~10x the speed). It does not hold for text-heavy stores (PMC, FineMath, Proof-Pile), where the bytes are prose and the large window still wins; grouping by site recovers about half of that gain at fast levels.
Projected savings (from the samples)
Applying each sample's ratio to its whole store, assuming the unit is representative:
| store | now | parquet rewrite (sort + zstd-9) | mova-pack, zstd-9 | cold: zstd-19 --long |
|---|---|---|---|---|
| PMC OA text, 65 GB gzip | 65 GB | โ | 53 GB solid prepared (โ12 GB); 57 GB with 1 MiB blocks (โ8 GB) | 46 GB (โ19 GB) |
| OpenAlex works-slim, 68 GB parquet | 68 GB | 54 GB (โ14 GB) | 59 GB template lines (โ9 GB); 65 GB with 1 MiB blocks | 48 GB as sorted parquet zstd-19 (โ20 GB) |
| FineMath, 83 GB parquet snappy | 83 GB | 42 GB (โ41 GB) | 42 GB host-grouped (โ41 GB); 51 GB with 1 MiB blocks | 39 GB (โ44 GB) |
| Proof-Pile 2, 51 GB jsonl.zst | 51 GB | โ | 44-45 GB (โ6 to โ7 GB) | not measured |
| Wikidata dump, 103.5 GB bz2 | 103.5 GB | โ | 88 GB template lines (โ16 GB); 95 GB with 1 MiB blocks | 76 GB template lines + zstd-19 --long (โ28 GB) |
Together 370 GB โ about 281 GB with fast levels (โ89 GB, best variant per store), most of it from FineMath's snappy.
Mova's corpora (2026-11-18)
Choosing the format (samples)
One core-busy box (load 25-40 on 20 cores), so speeds are rough; sizes are deterministic. Ratio = original bytes / stored bytes. "Lines" is a format-2 container with the same 1 MiB blocks and trained dictionary but no preparation; "tok" adds the token-stream preparation.
| sample | original | zstd-9 solid | zstd-19 solid | lines, 1 MiB | tok, 1 MiB | tok, 4 MiB |
|---|---|---|---|---|---|---|
Wikipedia .tok (first 256 MiB of the 300M slice, 618 articles) | 268.4 MB | 42.5 MB, 6.32 | 34.6 MB, 7.77 | 45.2 MB, 5.94 | 28.6 MB, 9.37 | 27.9 MB, 9.63 |
Gutenberg history book .tok | 40.7 MB | 4.59 MB, 8.86 | 3.22 MB, 12.6 | 5.00 MB, 8.13 | 3.50 MB, 11.6 | โ |
Taskmaster-2 dialogues .tok | 150.0 MB | 12.0 MB, 12.5 | 7.36 MB, 20.4 | 13.2 MB, 11.4 | 11.7 MB, 12.9 | โ |
task items (morphology) .tok | 99.2 MB | 2.50 MB, 39.7 | 2.05 MB, 48.3 | 2.46 MB, 40.2 | 2.54 MB, 39.1 | โ |
early modern English .conllu | 38.5 MB | 5.41 MB, 7.13 | 4.16 MB, 9.26 | 6.01 MB, 6.41 | 3.62 MB, 10.6 | โ |
Taskmaster-2 dialogues .jsonl (json templates) | 29.8 MB | 3.89 MB, 7.66 | 3.06 MB, 9.73 | 4.26 MB, 7.00 | 3.96 MB, 7.54 | โ |
On the Wikipedia sample the trained dictionary saves 2 % at 1 MiB blocks (29.2 โ 28.6 MB) and nothing at 4 MiB (27.7 โ 27.9 MB). Decoding the 268 MB sample: solid zstd-9 0.53 s; the tok container 1.72 s on one thread (156 MB/s, including its sha256), 0.56 s on 4, 0.39 s on 8; lines 0.82 s on one. Reading one article by its # doc (index chunk + one block, a fresh process each time, 200 reads): p50 7.8 ms, p95 12.4 ms with 1 MiB blocks; p50 25 ms, p95 59 ms with 4 MiB blocks.
So: 1 MiB blocks, zstd-9, a trained dictionary, the tok preparation for token files. Preparation is decided per file on its first 32 MB: kept when it is at least 3 % smaller. Highly repetitive templated data (task items) compresses better as plain text, because whole repeated sentences are long matches for zstd. JSON templates win little on prose-heavy dialogue records. Solid zstd-19 is still smaller on repetitive dialogue data, at the price of record access and of 1-3 MB/s compression.
The corpus folder living/ already stores JSON lines as zstd-19 frames of about 16 MB with its own index (corpuspack): repacked to zstd-9 1 MiB blocks it grows (OANC 24.4 โ 30.2 MB, Europarl English 72.4 โ 95.0 MB), so it stays as it is.
Repacked in place
mova-pack repack --min-free 100, one file at a time, smallest first, as systemd user units (nice 15, MemoryMax=8G), files of 1 MB and more. Every file was read back in full (sha256 of its content) and every 64th record looked up through its new index before the original moved aside.
| corpus | files | before | after | ratio | index |
|---|---|---|---|---|---|
| alice | 2 | 4.1 MB | 0.4 MB | 10.4 | 0.01 MB |
| sft | 3 | 143.9 MB | 14.7 MB | 9.81 | 0.37 MB |
| gali-tasks | 24 | 1 390.2 MB | 89.4 MB | 15.6 | 20.1 MB |
| books-open | 173 | 1 040.3 MB | 98.9 MB | 10.5 | 0.34 MB |
| dialog | 42 | 8 079.5 MB | 920.0 MB | 8.78 | 42.7 MB |
| hard-cases.jsonl | 1 | 33.3 MB | 3.4 MB | 9.87 | 0.01 MB |
wiki-v5-slice-300m-full (.tok.zst, dev.tok) | 2 | 2 660.8 MB | 1 779.6 MB | 1.50 (9.38 on the text) | 0.9 MB |
wiki-v5-slice-300m-full shards (.tok.zst) | 18 | 3 373.1 MB | 2 265.4 MB | 1.49 | 1.4 MB |
wiki-all-v5-full (train.tok.zst, dev.tok) | 2 | 40 074.8 MB | 27 060.9 MB | 1.48 (9.63 on the text) | 96.3 MB |
"Before" is the file as it was (plain text, or zstd for the Wikipedia stores); "after" the container alone. The index is a separate file. Small .tok indexes (one key per document) cost 0.1-1 % of the container; TSV files indexed by their first column cost much more (the turns table of the book dialogues: 18.8 MB of index for a 27.0 MB container), since every line is a record.
The whole of Wikipedia (train.tok.zst, 260.6 GB of text, 6 779 957 articles, 229 120 blocks): packed in 54 min on 8 threads (80 MB/s of text), read back in 6.6 min (650 MB/s); one article by its # doc: p50 18 ms, p95 36 ms (200 reads). Together these files went from 56.8 GB to 32.2 GB, plus 162 MB of index.
Reading one record by key on the same busy box (100 reads, a fresh process each): Wikipedia slice p50 23 ms, p95 52 ms; book dialogues .tok p50 16 ms, p95 49 ms; their .jsonl p50 22 ms, p95 90 ms; their turns TSV p50 11 ms, p95 40 ms.
Recommendation
- Parquet stores (OpenAlex, FineMath) stay parquet: rewrite each file sorted (OpenAlex by id, FineMath by host then url) with ZSTD level 9. DuckDB keeps reading them directly, and the measured size equals the best
mova-packvariant. Level 19 only for cold copies. - JSON-lines and text stores (Wikidata, Proof-Pile, crawler dumps, PMC metadata) go to
mova-pack: template lines, one dataset dictionary (mova-pack dict, stored once in a dictionary directory), zstd-9, 1-4 MiB blocks for record access, grouped by site or id where the order is not meaningful. - PMC text tars: zstd-9
bytescontainers (or solid zstd-9) instead of gzip; a background repack to zstd-19 --long when the CPU is free (the Spider's practice). - Dictionaries: one global per dataset; per-domain dictionaries only for very large stores where their size amortizes, after a held-out check.
What a migration takes
- Train the dataset dictionaries on a sample (
mova-pack dict), store them in the dataset's dictionary directory. mova-pack repackfile by file in small waves (it is single-writer per directory; keep โค 4 threads), or a DuckDB COPY per parquet file followed by a row-count and content check.- Switch the readers:
.gzreaders becomemova-pack get/unpackor thepacklibrary; parquet readers do not change. mova-pack gcafter the grace period removes the moved-aside originals.
Rough cost on one core: FineMath and OpenAlex rewrites by DuckDB ~30 s per file at 4 threads (a few hours for both stores); mova-pack at 10-50 MB/s per core with the current encoder.
Limits
- The JSON template encoder is a prototype: it allocates a path string per value and makes several passes (8-60 MB/s per core). Path interning and a single-pass dictionary build are the next steps.
- Format 1 containers are built in memory. Format 2 streams, but keeps the index keys of all records in memory until the end; no append. One writer per directory, no locking.
- The tok preparation's typical-value tables are per block (each block decodes alone); a table shared by a whole store would save more, and would make every block depend on it.
- A line-prefix key's continuation records are checked only through their run, not by their own text.
- JSON is never normalized: key order, number spelling and escapes are kept. gzip and parquet encodings are not reproduced: their content is.
- Blocks cost compression: 1 MiB blocks lose 7-10 % against one solid stream on OpenAlex and FineMath. Larger blocks narrow it; record access gets slower in proportion.
- bsc and kanzi were not measured (not installed); on the published enwik9 benchmark (LTCB) libbsc is about 24 % smaller than zstd -22, at about 23 ns per byte both ways.
- Five samples, one unit each (Proof-Pile only a quick run); projections assume they are representative. The DuckDB rewrites ran once each.
This page in Markdown https://md.mova.elamur.ai/free/en/docs/repo-docs/compression/