Repository navigation
Description
Summary
internal/compression/analyzer.go, selector.go, and ml_selector.go implement content-aware compression selection. None of it has a caller outside the package — verified: zero references to compression.Analyze, NewRuleSelector, or NewAdaptiveSelector anywhere else in the repo.
What the write path actually does (internal/compression/s3_integration.go:75-91):
func (c *Compressor) Compress(data []byte) ([]byte, bool, error) {
if !c.Enabled() || int64(len(data)) < c.minSize {
return data, false, nil
}
compressed, err := c.codec.Compress(data)
...
if int64(len(compressed)) >= int64(len(data)) {
return data, false, nil // discard when it didn't help
}
return compressed, true, nil
}Two gates: an on/off flag and a size floor. So with compression enabled, ObjectFS runs zstd over every object above min_size — including the ones this project's users store most of.
Why this matters specifically here
ObjectFS targets research computing. Research data is largely already compressed at the byte level:
| format | already compressed |
|---|---|
| BAM, CRAM | yes (BGZF / custom) |
| Parquet, ORC | yes (per-column, usually snappy or zstd) |
| Zarr, NetCDF-4/HDF5 with filters | yes |
tar.zst, tar.gz, tar.bz2 |
yes — and pkg/archive exists specifically to read these |
| MP4, MKV, FLAC | yes |
| PNG, JPEG, WebP | yes |
For all of these, zstd spends CPU and produces output within a fraction of a percent of the input — at which point the len(compressed) >= len(data) check discards it and the work is thrown away. That check is what keeps this from being a correctness problem, and it is why nobody noticed: the outcome is correct, just paid for twice. On a sequential read-heavy HPC workload writing multi-GB BAM files, that is a measurable fraction of write throughput spent producing nothing.
The min_size floor does not help — these files are large, so they clear it.
The code that already solves this
Analyze(data []byte) Analysis (analyzer.go:58) samples up to 4 KiB and returns:
ContentClass— one oftext,json,binary,compressed,archive,unknown.classifyByMagic(:97) recognizes gzip, zstd, bzip2, lz4, xz, zip, PNG, JPEG, GIF, WebP, MP4, MKV, MP3 by magic bytes.Entropy— Shannon entropy in bits/byte (shannonEntropy,:170)CompressScore— estimated compressibility in [0,1] (compressScore,:192)
ContentClassCompressed's own doc comment states the conclusion: "Re-compression is wasteful — use AlgorithmNone." The analysis is written, tested, and documented; nothing asks it.
The sampling constant is deliberate: "4 KiB gives sub-millisecond latency while remaining statistically reliable" (:52). This is cheap enough for the write path.
Implementation
Reuse Analyze — do not write new heuristics. The work is entirely wiring:
-
Compressor.CompressconsultsAnalyzebefore invoking the codec. OnContentClassCompressed, return(data, false, nil)immediately. That is the whole win for the formats above, and it is a few lines. -
Decide whether the
Selectorinterface earns its place.RuleSelector(selector.go:39) andAdaptiveSelector(ml_selector.go:31) choose an algorithm and level per object, andAdaptiveSelectorlearns fromRecordOutcome. That is more machinery than step 1 needs, and per-object algorithm choice means the storedContent-Encodingvaries across objects in a bucket.The read path handles that correctly — it dispatches on the object's own
Content-Encodingheader, not the write config (backend.go:416-427), which is the C2 fix — so per-object algorithms are safe. Whether they are worth it is a separate question from step 1, and step 1 should not wait on it. -
Entropy as a secondary gate, for content with no recognizable magic number. High-entropy binary that is not in the magic table (encrypted files, some scientific binary formats) also will not compress.
CompressScorebelow some threshold → skip. Needs a measured threshold, so it wants a benchmark, not a guess. -
A skip must not be silently indistinguishable from a failure. Today
CompressreturnswasCompressed == falsefor three different reasons — disabled, belowmin_size, didn't help — and now a fourth. If any caller ever needs to distinguish them, that is the moment to return a reason rather than a bool. Not required for step 1; noting it because the bool is already overloaded.
Testing
The -race round-trip fuzzer added in v0.10.1 covers correctness. What this needs additionally:
- A table test over real magic-byte prefixes asserting
Compressdoes not invoke the codec for each already-compressed format. Assert the codec was not called, not just that output equals input — thelen(compressed) >= len(data)fallback already makes output equal input, so a test that only checks bytes would pass on the unfixed code. That is the same trap that let eighteen inert FUSE config fields ship in v0.10.0. - A benchmark showing write throughput on a synthetic BAM-like or
tar.zstpayload before and after. Without a number this is a plausible-sounding optimization, and this repo has a documented history of those.
Sequencing
Step 1 alone is small, safe, and captures most of the benefit for this project's actual workloads. Steps 2-3 are a genuine design question about whether per-object algorithm selection is wanted at all. Suggest landing step 1 on its own and deciding 2-3 with the benchmark in hand.
Related: the compression guidance doc (filed separately) needs to describe whatever this lands as, since "does ObjectFS re-compress my BAM files" is a question a user will ask before enabling compression.
Activity
perf(compression): skip data that arrived compressed, and measure wha…
scttfrdmn commentedon Aug 9, 2026
Step 1 shipped in #409 (`0c7d6ce`): the write path now gates on `compression.AlreadyCompressed`, so the codec is not run over data that arrived compressed. 8 MiB zstd frame 1.85ms → 2.2µs, 33 MiB of allocation → zero, and compressible data pays 16ns for the magic-byte comparison.
Two parts of this issue were deliberately not implemented and are now #410:
Closing this as the defect it was filed about is fixed; #410 carries the open questions so they are not lost behind a closed issue.