The 14% xz Left on the Table: lzcsv Transposes CSVs Before Compressing Them

Same xz, same preset, 13.9% smaller — because the CSV was transposed first. The hot_qwen Cycle 1 idea, measured and shipped as lzcsv 1.0.

The 14% xz Left on the Table: lzcsv Transposes CSVs Before Compressing Them

Your CSVs are beating xz — and xz is not the problem. The layout is. A new tool, lzcsv 1.0, transposes a CSV column-wise before compressing it, and the result is 13.9% smaller than xz -6 on the same files. Same compressor, same preset. The only thing that changed is the order the bytes arrive in — and xz's strongest preset cannot catch up.

This came out of hot_qwen Cycle 1, the local-model research loop running on hot_house: the coordinator proposed grid-aware compression for CSVs, hot_qwen (Qwen4-35B-A3B) was asked to disprove it, and the attempt failed in the interesting way — the idea survived its own adversarial review and then survived the measurement too. lzcsv is that test turned into a real command, in the lzply repo.

Why row-major CSVs waste xz's time

A CSV is the worst possible layout for a compressor. Every row alternates fields — a date, a country, a number, a string — so the same kind of value is always a full row away from the last one. xz's matcher sees a repeating pattern interrupted every few bytes by a different field type.

lzcsv transposes the file column-wise into the COLT1 format (byte-compatible with the Cycle 1 Python prototype): all the dates together, all the countries together, all the numbers together. Same-field values become adjacent long runs, and xz's matcher finally has something to chew on. Then it runs plain xz over that.

The compressed bytes prove the point: the transposed stream's output is the same liblzma preset-6 stream xz -6 produces — within a few bytes, because the encoder's output varies slightly with how it is fed (1,500 vs 1,504 bytes on tips.csv). Same algorithm, same preset, not a different compressor. The win comes entirely from the layout.

The container is a 26-byte header — magic LZCSV001, original size, transposed size, preset, flags — followed by the xz stream (liblzma, CRC64 check). Integrity is a chain: liblzma verifies the CRC64, the COLT1 decoder is strict (magic, counts, column field counts, no trailing data), and the header's original size must match the reconstructed output exactly.

The numbers: 14 corpus CSVs

14 CSVs, 12,096,190 bytes, measured on hot_house with xz/liblzma 5.8.1. lzcsv −6 wins on 13 of 14 files against xz -6 and against xz -9e alike — no file flips at any preset.

fileoriginalxz −6xz −9elzcsv −6lzcsv −9elzcsv −6 vs xz −6
covid-countries.csv5,512,931646,112654,748464,786464,446−28.1%
gen-log.csv373,14738,06038,10029,34629,626−22.9%
gen-numeric.csv971,501171,016171,256136,142137,990−20.4%
hydra-passwords.csv515,37679,83279,81670,43470,050−11.8%
gapminder.csv82,07924,35224,47221,50621,694−11.7%
diabetes.csv23,8738,0768,0807,4227,446−8.1%
default-passwords.csv90,85625,90025,89223,89823,906−7.7%
tips.csv7,9431,6361,6401,5261,502−6.7%
mam.csv608,108192,364192,496179,690179,906−6.6%
iab.csv381,908128,396128,392120,114120,342−6.5%
stocks.csv67,92424,09224,13222,69822,718−5.8%
oui.csv3,372,302718,664713,760692,506687,758−3.6%
iris.csv4,601824828818818−0.7%
pin-codes.csv83,64112,86412,84413,99414,066+8.8%
aggregate (ratio)12,096,1902,072,188 (0.1713)2,076,456 (0.1717)1,784,880 (0.1476)1,782,268 (0.1473)−13.9%

Aggregate: lzcsv −6 beats xz -6 by 13.9% (2,072,188 → 1,784,880 bytes) and beats xz -9e by 14.0%. The extreme preset buys almost nothing on top: lzcsv −9e is only 0.15% smaller than lzcsv −6. Preset 6 is the sweet spot.

xz's best does not erase the win

This is the check that matters, stated plainly: reaching for xz's strongest setting does not close the gap. Direct xz -9e is slightly worse than xz -6 on these CSVs (2,076,456 vs 2,072,188 bytes), and xz -9 is byte-identical to xz -6 on all 14 files. The transpose is doing work no preset can do, because the preset was never the bottleneck — the row-major layout was.

Held out: 18 files it never saw

A corpus win is only interesting if it survives contact with new files. The held-out set is 18 CSVs totalling 4,431,565 bytes — 5 downloaded real-world files plus 13 generated edge cases:

aggregatexz −6xz −9elzcsv −6lzcsv −9elzcsv −6 vs xz −6
18 held-out CSVs (ratio)844,752 (0.1906)842,764 (0.1902)730,756 (0.1649)726,864 (0.1640)−13.5%

lzcsv −6 wins 9 of 18 held-out files, and the aggregate win is −13.5% — essentially the same as the corpus result. Best held-out wins: flights −21.9%, text −21.4%, diamonds −18.9%, taxis −13.3%. The win is not a corpus artefact.

The losses, stated honestly

Two files in the corpus table deserve attention. pin-codes is the only corpus loss: +8.8% (13,994 vs 12,864 bytes). Short, near-random numeric codes gain nothing from being grouped — the transpose just adds headers.

All 9 held-out losses follow the same pattern: files under ~1 KB, or single-column files. The fixed overhead — the COLT1 header plus per-column headers plus the 26-byte LZCSV header plus the xz stream itself — dominates on tiny files (empty.csv: 0 bytes → 98; tiny.csv: 24 bytes → 134). Single-column files gain nothing from transposition, because there is only one column to transpose into — xz −9e beats lzcsv there (single-col.csv: 1,152 vs 1,474 bytes). A size gate — transpose only above a row/column threshold, pass small files through as plain xz — is the obvious next step, and it is left for a later cycle.

Scope, stated plainly: CSVs are about 0.6% of the full 48 MB corpus this work draws from. This is a real win on one category, not a breakthrough. It does not make lzcsv a general compressor, and it should not be sold as one. What it does show is that the cheapest compression improvement available is often not a better algorithm — it is feeding the algorithm you already have in a better order.

Speed and correctness

Encode times are roughly comparable to xz: on the corpus, xz −6 took 3.59 s against lzcsv −6's 4.08 s (sums of per-file medians, approximate); on the held-out set, 3.30 s against 2.00 s. Decode is at parity. The transpose itself is negligible next to xz.

Round-trips are byte-exact on all 32 files (14 corpus + 18 held-out). COLT1 output is byte-identical to the Cycle 1 Python prototype. Malformed input is rejected cleanly — embedded newlines and mixed line endings both fail with an error instead of silently producing a wrong file.

Get it

lzcsv 1.0 is in the lzply repo at commit e8f61d7 on main — github.com/cnmcdee/lzply. It is standalone C that links only liblzma, completely separate from the lzply FEC core. Build it with make lzcsv (needs liblzma-dev; the target is skipped cleanly without it).

make lzcsv
lzcsv c in.csv out.lzcsv    # compress (preset 6)
lzcsv c -e in.csv out.lzcsv # extreme preset (-9e)
lzcsv d in.lzcsv out.csv    # decompress
lzcsv test in.csv           # in-memory round-trip check
lzcsv selftest              # built-in synthetic tests

There is also lzcsv info (show the container header) and lzcsv raw-enc / raw-dec for writing and reading the COLT1 intermediate directly — useful if you want to inspect what the transpose actually does to your data before xz ever sees it.

If your CSVs live above a few KB and have more than one column, this is a free 14% — the same xz, fed properly.

Linux Rocks Every Day