Files
cnmcdee e8f61d7bb9 Add lzcsv: standalone high-speed CSV compressor (transpose + xz)
Column-wise transpose (COLT1, byte-compatible with the hot_qwen Cycle 1 Python prototype) + xz via liblzma. 14 corpus CSVs: 2,072,188 -> 1,784,880 bytes vs xz -6 (-13.9%), 13/14 win; vs xz -9e -14.0%. 18 held-out CSVs: -13.5% vs xz -6, 9/18 win (losses are tiny files and single-column). Byte-exact round-trips on all 32; oracle byte-identical to the prototype; clean rejections for embedded newlines, mixed endings, truncated/bad-magic input. CPU-only, conditional build on liblzma presence; own binary, no liblzply.a.
2026-12-04 22:09:01 -05:00
..

lzcsv — high-speed CSV compression

lzcsv compresses CSV files by transposing them column-wise and then compressing with xz (liblzma). Same-field values become adjacent long runs, which xz's matcher uses far better than alternating fields on every row. It is a standalone tool in the lzply repo, completely separate from the lzply FEC core: it links only liblzma, no liblzply.a, no liblz, no CUDA.

The idea comes from hot_qwen Cycle 1 (Dec 2026): the coordinator proposed grid-aware compression, hot_qwen tried to disprove it (prior art, failure modes) and called it testable, and the test on 14 CSVs gave −13.9% vs xz -6, 13/14 files, all round-trips byte-exact. This tool is that test turned into a real command.

Format (LZCSV v1)

bytes 0..7    magic "LZCSV001"
bytes 8..15   uint64 LE original (CSV) size
bytes 16..23  uint64 LE transposed (COLT1) size
byte 24       xz preset (0-9)
byte 25       flags (bit 0 = extreme)
bytes 26..    xz stream (liblzma, preset, CRC64 check) holding COLT1

The transposed stream is COLT1, byte-compatible with the Cycle 1 Python prototype (/root/hot-qwen/cycle1/transpose.py):

line 1: COLT1 <nrows> <ncols> <crlf:0|1> <trailing_nl:0|1> <uniform:0|1>
if uniform=0: line "COUNTS <len>" then <len> bytes of comma-separated
    per-row field counts (ragged rows; padding stripped on decode)
per column j: line "COL <len>" then exactly <len> bytes:
    the column's raw field texts joined by "\n" (no trailing newline)

The compressed payload is the same liblzma preset-6 stream xz -6 produces on the same transposed bytes (within a few bytes: the encoder's output varies slightly with how it is fed — 1500 vs 1504 bytes on tips.csv — same algorithm, same preset, not a different compressor).

Integrity chain: liblzma verifies the CRC64 of the xz stream, the COLT1 decoder is strict (magic, counts, column field counts, no trailing data), and the container header's original size must match the reconstructed output exactly.

Build

make lzcsv        # needs liblzma-dev; skipped cleanly without it

Usage

lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv>   compress (preset 6)
lzcsv d [-f] [-v] <in.lzcsv> <out.csv>               decompress
lzcsv test <file.csv> ...                            in-memory round-trip check
lzcsv info <file.lzcsv>                              show container header
lzcsv raw-enc <in.csv> <out.colt>                    write COLT1 intermediate
lzcsv raw-dec <in.colt> <out.csv>                    decode COLT1 intermediate
lzcsv selftest                                       built-in synthetic tests

Measured results (hot_house, xz/liblzma 5.8.1)

14 corpus CSVs (12,096,190 bytes, /root/fftdict-shared/corpus2):

file xz -6 xz -9e lzcsv -6 lzcsv -9e lzcsv-6 vs xz-6
covid-countries.csv 646,112 654,748 464,786 464,446 −28.1%
gen-log.csv 38,060 38,100 29,346 29,626 −22.9%
gen-numeric.csv 171,016 171,256 136,142 137,990 −20.4%
hydra-passwords.csv 79,832 79,816 70,434 70,050 −11.8%
gapminder.csv 24,352 24,472 21,506 21,694 −11.7%
diabetes.csv 8,076 8,080 7,422 7,446 −8.1%
default-passwords.csv 25,900 25,892 23,898 23,906 −7.7%
tips.csv 1,636 1,640 1,526 1,502 −6.7%
mam.csv 192,364 192,496 179,690 179,906 −6.6%
iab.csv 128,396 128,392 120,114 120,342 −6.5%
stocks.csv 24,092 24,132 22,698 22,718 −5.8%
oui.csv 718,664 713,760 692,506 687,758 −3.6%
iris.csv 824 828 818 818 −0.7%
pin-codes.csv 12,864 12,844 13,994 14,066 +8.8%
aggregate 2,072,188 (0.1713) 2,076,456 (0.1717) 1,784,880 (0.1476) 1,782,268 (0.1473) −13.9%
  • lzcsv -6 wins on 13/14 files vs xz -6 and vs xz -9e. No file flips at any preset. xz's best does not erase the win: direct xz -9e is slightly worse than xz -6 on these CSVs (2,076,456 vs 2,072,188), and lzcsv -6 beats direct xz -9e by −14.0%.
  • Encode time (sums of per-file medians, 4 parallel workers, approximate): xz -6 3.59 s, lzcsv -6 4.08 s. Decode: xz -6 0.14 s, lzcsv -6 0.19 s. Roughly comparable; the transpose itself is negligible next to xz.

18 held-out CSVs (4,431,565 bytes — 5 downloaded real-world files plus 13 generated edge cases):

aggregate xz -6 xz -9e lzcsv -6 lzcsv -9e lzcsv-6 vs xz-6
bytes 844,752 (0.1906) 842,764 (0.1902) 730,756 (0.1649) 726,864 (0.1640) −13.5%
  • lzcsv -6 wins on 9/18. Best held-out wins: flights.csv −21.9%, text.csv −21.4%, diamonds.csv −18.9%, taxis.csv −13.3%.
  • All 9 losses are files under ~1 KB (or a single column): the fixed overhead — COLT1 header + per-column headers + the 26-byte LZCSV header + the xz stream itself — dominates on tiny files (empty.csv: 0 → 98 bytes; tiny.csv: 24 → 134 bytes). Single-column files gain nothing from transposition (there is only one column) and xz -9e beats lzcsv there (single-col.csv: 1,152 vs 1,474).
  • Honest scope: lzcsv wins on real-world CSVs above a few KB and loses on tiny files. A size gate (transpose only above a row/column threshold, pass small files through as plain xz) is the obvious next step and is left for a later cycle.

Limitations (documented, tested)

  • Fields holding a raw newline (embedded newlines in quoted fields) are rejected with a clear error — the COLT1 column join uses "\n", so such a field cannot be represented. Same as the prototype. The test suite checks the rejection is clean.
  • Files with mixed line endings (some rows CRLF, some LF) are rejected: the single crlf flag cannot represent them, and the prototype would silently mis-round-trip such a file.
  • In-memory: the whole file is read into RAM (CSVs are small; a 5.5 MB file uses tens of MB during the transpose).
  • CSV only. Other file types gain nothing and are not the tool's job.

Tests

./lzcsv selftest                       # built-in synthetic cases
bash tests/run_lzcsv_tests.sh ./lzcsv  # full suite (also in `make test`)
python3 lzcsv-src/bench.py             # benchmark (corpus + held-out)

The suite checks: selftest, --help, byte-exact round-trips on the 14 corpus CSVs and the 18 held-out CSVs, byte-identical COLT1 output vs the Python prototype (oracle) plus cross-decoding both ways, clean rejection of embedded-newline input, and clean failure on truncated / bad-magic input.