Skip to content

perf(compression): skip data that arrived compressed, and measure what that is worth (#184) - #409

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/compression-analyzer-184
Aug 9, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/compression-analyzer-184

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #184, step 1 — the step the issue itself recommends landing alone.

What changes

Compressor.Compress now returns the original bytes without entering the codec when the object's
leading bytes name a compressed format. That is 15 formats via the existing classifyByMagic: gzip,
zstd, bzip2, lz4, xz, zip, PNG, JPEG, GIF, WebP, MP4, Matroska, Ogg, MP3, PDF. A BAM is included,
because BGZF is gzip per block and so a BAM starts with gzip's magic bytes.

What it is worth

Apple M4 Max, darwin/arm64, zstd level 3, six runs per case compared with benchstat:

object without the skip with it
8 MiB zstd frame 1.85 ms, 33 MiB allocated 3 ns, 0 allocations
1 MiB zstd frame 237 µs, 4.3 MiB allocated 3 ns, 0 allocations
64 KiB zstd frame 7.4 µs, 112 KiB allocated 3 ns, 0 allocations
1 MiB compressible text 91.5 µs 90.4 µs

Every difference is at p=0.002 except the last row, where the two are indistinguishable — and that
row is the one that matters most. A gate that taxes the path it does not apply to is not an
optimization, and this is the repo whose docs index once claimed 800–1200 MB/s that no benchmark
produced. So: a number, reproducible with

go test ./internal/compression/ -run '^$' \
  -bench 'BenchmarkCompressAlreadyCompressed|BenchmarkCompressCompressibleText|BenchmarkGateVersusAnalyze' \
  -benchmem -count=6

Why AlreadyCompressed and not Analyze

The first attempt called Analyze, as the issue suggested, and the control benchmark said +53% on a
64 KiB text write
. Analyze runs a Shannon entropy pass over its 4 KiB sample — 3.8 µs against a
6.7 µs codec pass — and the skip decision discards the entropy figure entirely. So the write path calls
a new AlreadyCompressed (17 ns) instead. Both route through the same classifyByMagic, so there is
still one authority on what "already compressed" means; a private magic-byte table in the write path
would be a second one, free to drift from the one RuleSelector consults.

Both gates are ordered so nothing is scanned unnecessarily: below min_size the object is not sampled,
and with compression disabled it is not sampled either.

The tests assert the codec was not called

This is the trap the issue names, and it is real. Compress already returned the original when the
compressed form was no smaller, so an already-compressed object came back byte-identical before
this change — a test comparing got to data passes on the unfixed code and would certify a fix that
does nothing. countingCodec wraps the real codec and counts entries; the assertion is calls == 0.

Verified by mutation, twice:

mutation result
delete the gate from Compress 11 skip cases fail, 3 controls pass
AlreadyCompressed → return false same 11 fail

The three controls — a plain .tar with ustar at offset 257, an uncompressed VCF, a JSON document —
assert calls == 1. A skip that fires on everything saves the same CPU and stores the same bytes as
compression switched off, which is the failure this table exists to rule out.

Not done, deliberately

No entropy gate. High entropy is evidence compression will not help, not proof, and no threshold
for it has been measured in this repository. Declining to compress on a guess is worse than spending
the pass. That is the open half of #184 and wants its own measurement.

Formats whose compression is internal to the container are not skipped — CRAM, Parquet, ORC, HDF5,
Zarr. The container's magic bytes say nothing about its contents. docs/features/compression.md now
sorts the formats into two explicit lists, since "does ObjectFS re-compress my BAM files" is a question
a user asks, and TestAlreadyCompressedMatchesTheDocumentedFormatLists fails if that page and the
table disagree.

Steps 2–3 of the issue — whether RuleSelector/AdaptiveSelector earn their place — are left
open, per the issue's own sequencing. The read path already dispatches on the object's own
Content-Encoding, so per-object algorithms are safe; whether they are worth it is now a question
with a benchmark harness in hand to answer.

Verification

  • go build ./..., gofmt -l ., go vet ./..., golangci-lint run — all clean
  • go test -race ./... — no FAIL, panic, or DATA RACE
  • coverage floor internal/compression 87 → 90, measured 90.0%, gate re-run against a fresh profile

…t that is worth (#184)

With compression enabled, a write above min_size whose leading bytes name a compressed format is
stored as-is and the codec is never entered. These are the formats a research bucket is mostly made
of — and a BAM counts, since BGZF is gzip per block, so its first bytes are gzip's.

On an M4 Max at zstd level 3, six runs compared with benchstat: an 8 MiB zstd frame goes from 1.85 ms
and 33 MiB of allocation to 3 ns and none; 1 MiB from 237 µs; 64 KiB from 7.4 µs. Compressible text
is unchanged at 1 MiB — 91.5 µs against 90.4 µs — which is the row that decides whether the gate pays
for itself, because a check that taxes the compressible path is not a win.

The gate is AlreadyCompressed, a 17 ns magic-byte comparison, not Analyze. Analyze additionally runs
a Shannon entropy pass over its 4 KiB sample; that pass costs 3.8 µs and measured +53% on a 64 KiB
text write for a figure the decision does not use. Both classify through the same classifyByMagic, so
there is still one authority on what "already compressed" means — a private table in the write path
would be a second one, free to drift.

Tests assert the codec was not invoked, not that the bytes came back equal. Compress already returned
the original when the compressed form was no smaller, so an already-compressed object came back
unchanged before this change too: a byte comparison passes on the unfixed code. Verified by mutation
— removing the gate fails all eleven skip cases and none of the three controls (plain tar, VCF text,
JSON), and stubbing AlreadyCompressed to false does the same.

Nothing is skipped on entropy. High entropy is evidence compression will not help, not proof, and no
threshold for it has been measured here; declining to compress on a guess is worse than spending the
pass. Formats whose compression is internal to the container — CRAM, Parquet, ORC, HDF5, Zarr — are
not skipped either, and docs/features/compression.md now sorts the formats into those two lists with
TestAlreadyCompressedMatchesTheDocumentedFormatLists failing if the page and the table disagree.

Coverage floor 87 → 90: the new statements arrived covered rather than a suite being widened over old
ones.
@scttfrdmn
scttfrdmn merged commit 0c7d6ce into main Aug 9, 2026
35 checks passed
@scttfrdmn
scttfrdmn deleted the feat/compression-analyzer-184 branch August 9, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wire up the compression analyzer: ObjectFS re-compresses the formats research data is already stored in

1 participant