Skip to content

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

Closed

Description

@scttfrdmn

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 of text, 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:

  1. Compressor.Compress consults Analyze before invoking the codec. On ContentClassCompressed, return (data, false, nil) immediately. That is the whole win for the formats above, and it is a few lines.

  2. Decide whether the Selector interface earns its place. RuleSelector (selector.go:39) and AdaptiveSelector (ml_selector.go:31) choose an algorithm and level per object, and AdaptiveSelector learns from RecordOutcome. That is more machinery than step 1 needs, and per-object algorithm choice means the stored Content-Encoding varies across objects in a bucket.

    The read path handles that correctly — it dispatches on the object's own Content-Encoding header, 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.

  3. 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. CompressScore below some threshold → skip. Needs a measured threshold, so it wants a benchmark, not a guess.

  4. A skip must not be silently indistinguishable from a failure. Today Compress returns wasCompressed == false for three different reasons — disabled, below min_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 Compress does not invoke the codec for each already-compressed format. Assert the codec was not called, not just that output equals input — the len(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.zst payload 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

  1. added this to the Future Work milestone on Aug 2, 2026
  2. added
    type: performance
    priority: medium
    area: compression
    on Aug 2, 2026
  3. added
    effort: medium
    on Aug 3, 2026
  4. scttfrdmn commented on Aug 9, 2026

    @scttfrdmn
    OwnerAuthor

    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:

    • the entropy gate — no threshold has been measured in this repository, and routing the gate through `Analyze` to get one cost +53.16% on a 64 KiB compressible write (p=0.002). It needs a measurement on real CRAM/Parquet/HDF5 objects before it is a gate rather than a guess.
    • the three selectors — `Selector`, `RuleSelector` and `AdaptiveSelector` are still unreferenced outside the package. perf(compression): skip data that arrived compressed, and measure what that is worth (#184) #409 did not give them a caller, and the choice between wiring `RuleSelector` and deleting `AdaptiveSelector` is design work.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compressioneffort: mediumpriority: mediumtype: performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions