Repository navigation
perf(compression): skip data that arrived compressed, and measure what that is worth (#184) - #409
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #184, step 1 — the step the issue itself recommends landing alone.
What changes
Compressor.Compressnow returns the original bytes without entering the codec when the object'sleading 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: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
Why
AlreadyCompressedand notAnalyzeThe first attempt called
Analyze, as the issue suggested, and the control benchmark said +53% on a64 KiB text write.
Analyzeruns a Shannon entropy pass over its 4 KiB sample — 3.8 µs against a6.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 sameclassifyByMagic, so there isstill 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
RuleSelectorconsults.Both gates are ordered so nothing is scanned unnecessarily: below
min_sizethe 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.
Compressalready returned the original when thecompressed form was no smaller, so an already-compressed object came back byte-identical before
this change — a test comparing
gottodatapasses on the unfixed code and would certify a fix thatdoes nothing.
countingCodecwraps the real codec and counts entries; the assertion iscalls == 0.Verified by mutation, twice:
CompressAlreadyCompressed→return falseThe three controls — a plain
.tarwithustarat 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 ascompression 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.mdnowsorts the formats into two explicit lists, since "does ObjectFS re-compress my BAM files" is a question
a user asks, and
TestAlreadyCompressedMatchesTheDocumentedFormatListsfails if that page and thetable disagree.
Steps 2–3 of the issue — whether
RuleSelector/AdaptiveSelectorearn their place — are leftopen, 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 questionwith a benchmark harness in hand to answer.
Verification
go build ./...,gofmt -l .,go vet ./...,golangci-lint run— all cleango test -race ./...— no FAIL, panic, or DATA RACEinternal/compression87 → 90, measured 90.0%, gate re-run against a fresh profile