Skip to content

Compression: settle the entropy gate with a measurement, and decide whether the three selectors have a caller #410

Open

Description

@scttfrdmn

#184 was three questions and shipped one of them. This is the other two, recorded before the closed
issue stops being read.

What #184 shipped (PR #409, 0c7d6ce)

Compressor.Compress now gates on compression.AlreadyCompressed, so the codec is not run over data
that arrived compressed. Measured on an M4 Max at zstd level 3: an 8 MiB zstd frame went 1.85ms → 2.2µs
and 33 MiB of allocation → zero; data that does compress pays 16ns.

That closes the defect the issue was filed about. It does not answer either of the following.

Step 2 — does an entropy gate earn its place, and at what threshold?

AlreadyCompressed recognizes formats by magic bytes only. It says nothing about the case the magic
bytes cannot see: CRAM, Parquet, ORC, HDF5 and Zarr compress per column or per chunk inside an
uncompressed container, so the container's first four bytes are readable and the payload behind them is
already at its entropy limit. Compress runs a full codec pass over those and then discards the result
via the len(compressed) >= len(data) fallback — correct output, wasted CPU, and these are exactly the
formats a research bucket is full of.

Analyze already computes Shannon entropy over the leading 4 KiB, so the number is available. It was
deliberately not used as a gate:

  • High entropy is evidence, not proof. A gate on it declines to compress data that might have
    compressed, which is a correctness-shaped decision made on a guess. Contrast the magic-byte gate,
    where the format defines the answer.
  • No threshold has been measured in this repository. Picking 7.5 bits/byte because it appears in
    other projects is the kind of unmeasured claim this tree has a documented history of (see the
    throughput table that claimed 800–1200 MB/s no benchmark produced).
  • It is not free. The entropy pass is 2.13µs of Analyze's 3.70µs against AlreadyCompressed's
    17ns. On a 64 KiB compressible write — 6.7µs of codec time — routing the gate through Analyze cost
    +53.16% (p=0.002, benchstat, six runs). That regression is why the shipped gate is a separate
    function. Any entropy gate has to buy more than it costs, and only for objects whose leading 4 KiB is
    representative of the rest.

What would settle it: sample real CRAM/Parquet/HDF5 objects, measure the entropy of their leading 4 KiB
against the compression ratio zstd actually achieves on the whole object, and find whether a threshold
separates "will not compress" from "will" with few enough false positives to be worth 2.1µs. If no
threshold does, the answer is to say so and close this — a measured "no" is a result.

Note the sampling problem is real for these formats specifically: Parquet's leading bytes are PAR1
plus row-group data, HDF5's are a superblock. A 4 KiB prefix may be the least representative part of the
file.

Step 3 — do Selector, RuleSelector and AdaptiveSelector earn their place?

Still unreferenced outside internal/compression after #409:

  • Selector (selector.go:32) — the interface
  • RuleSelector (selector.go:39) — extension/content-class rules to an algorithm and level
  • AdaptiveSelector (ml_selector.go:31) — the same, with per-key history

The write path takes its algorithm and level from config, one setting per mount, and #409 did not
change that. So there are three types deciding a question nobody asks them. Two directions:

  1. Wire RuleSelector, so a mount's algorithm: zstd becomes a default that per-object rules can
    override — lz4 for a file being appended to, a higher zstd level for cold-tier writes. That is a
    user-visible feature and needs config surface, which is design work, not wiring.
  2. Delete what is not wanted. AdaptiveSelector is the one to look at hardest: per-key history in
    a write path that is per-mount by configuration, with the memory that implies, and no evidence any
    deployment would benefit. pkg/api and two other packages were deleted in this project on the same
    reasoning.

Either is fine. What is not fine is three types staying in the tree with no caller and no decision,
which is the state #184 was filed about in the first place.

Step 4 — noted, not required

wasCompressed now carries four distinct reasons for false: compression disabled, below min_size,
already compressed, and the codec made it bigger. Callers only need the boolean today, so this is
recorded rather than filed — if a metric ever wants to distinguish "we skipped this" from "we tried and
it did not help", that is when the return type needs to change.

Where to look

  • internal/compression/analyzer.go — Analyze, AlreadyCompressed, classifyByMagic, shannonEntropy
  • internal/compression/s3_integration.go:126 — Compress, the one caller of the gate
  • internal/compression/skip_compressed_bench_test.go — BenchmarkGateVersusAnalyze is the 17ns-vs-3.8µs measurement
  • docs/features/compression.md — the two format lists, pinned by TestAlreadyCompressedMatchesTheDocumentedFormatLists

Activity

  1. added
    type: enhancement
    priority: low
    area: compression
    effort: medium
    on Aug 9, 2026
  2. scttfrdmn commented on Sep 17, 2026

    @scttfrdmn
    OwnerAuthor

    Moved off the v0.15.0 milestone (renamed from v0.16.0) because 0.15.0 has been cut and this is not in it.

    Worth noting what did ship in 0.15.0, since it changes the context slightly: seekable framing (#185) is now in the write and read paths, so Compress in internal/compression/s3_integration.go — the one caller of the gate this issue is about — now also decides frame layout. The measurement this issue asks for is unaffected, but the decision about whether the three selectors have a caller should be made with the framing code in view rather than against the pre-#185 tree.

  3. added this to the Future Work milestone on Sep 19, 2026
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: lowtype: enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions