Skip to content

Decompression: a faster checksum, larger first-level tables - #74

Merged
AOShei merged 1 commit into
mainfrom
inflate-speed
Nov 22, 2026
Merged

AOShei merged 1 commit into
mainfrom
inflate-speed

Conversation

@AOShei

@AOShei AOShei commented Nov 22, 2026

Copy link
Copy Markdown
Owner

The decompression item. A modest gain, and smaller than I estimated. No change to output.

Where the time was. On one worker, reading the tuning set spends about a fifth of its time decompressing zlib streams, mostly page content. Only 1.1% of decompressed bytes are streams decompressed twice, so keeping decoded streams wouldn't help; only a faster decoder would. On the new real-stream benchmark, the time split:

Part Share
Main decode loop 77%
Adler-32 checksum 10%
Building tables for each block 10%

What changes

  • adler.go: the checksum computed 32 bytes at a step.
    • For each 8-byte word, the sum of its bytes and their position-weighted sum come from two multiplications each, on the bytes spread into 16-bit lanes.
    • The sums are reduced modulo 65521 as often as zlib reduces them, so they never overflow.
    • It runs at 9.3 GB/s against 5.5 GB/s for hash/adler32. TestAdler32 and FuzzAdler32 compare the two.
  • First-level tables of 10 bits for literals and 7 for distances, where they were 9 and 6, so fewer codes need a second lookup. 11 bits cost more to build than they save on the short streams in PDFs.
  • BenchmarkZlibCorpus decompresses every zlib stream of the tuning set from the corpus cache, leaving out images since reading text doesn't decompress them. TestCorpusLikeCompressZlib checks those 11,394 streams against compress/zlib. Both skip without the corpus.

Results

  • Real streams: 281 → 255 ms per run (−9%), 5.65 → 5.28 G instructions (−6.5%).
  • Whole tuning set, one worker: −1.33% instructions, and about 1–1.5% in time (paired runs on a moderately busy machine).

Tried and left out, because each measured worse:

  • writing a round's literals through a slice of known length, to drop bounds checks;
  • an 8-byte refill in the block-header decoder;
  • table entries holding a pair of literals. Building them costs more than they save on streams of 11 KB on average: +15% instructions.

Same output

  • Corpus: the JSON output, warnings included, has the same SHA-256 as on main for all 3,253 files of the corpus cache and the fixtures.

  • Fuzzing with -fuzzminimizetime 0s, nothing found:

    Target Time Inputs
    FuzzZlib (against compress/zlib) 10 min 87 million
    FuzzAdler32 5 min 71 million
    FuzzFilters 5 min 65 million
    FuzzOpen 5 min —
    FuzzPage 5 min 6 million
  • FuzzAdler32 is added to .github/fuzz-targets.txt, the full test suite passes, and the CHANGELOG has an entry under Unreleased.

Why I'd stop here: what's left is the core decode loop, which is close to what pure Go allows without assembly or unsafe.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DzYLyEzLqkwuqPQ9LXgMpD

The Adler-32 checksum of each zlib stream is computed 32 bytes at a
step: the sum and the weighted sum of each word of eight bytes come
from two multiplications each, on its bytes spread into lanes of 16
bits, and the sums are reduced modulo 65521 as often as zlib reduces
them. 9.3 GB/s against 5.5 for hash/adler32; TestAdler32 and
FuzzAdler32 compare it with hash/adler32.

The first level of the decoding tables is of 10 bits for literals and
lengths and 7 for distances, where it was 9 and 6: fewer codes go
through a second level. 11 bits cost more to build than they save on
the short streams of PDF files.

BenchmarkZlibCorpus decompresses the zlib streams of the tuning set of
the public corpus, images left out as reading text leaves them, and
TestCorpusLikeCompressZlib checks them against compress/zlib. On it:
281 ms to 255, 5.65 G instructions to 5.28. Reading the tuning set
takes 1.33 percent fewer instructions; the JSON of all 3,253 files of
the corpus cache and the fixtures is the same as on main.

Tried and left out, as each measured worse: writing the literals of a
round through a slice of known length, a refill of eight bytes in the
block header, and entries that hold a pair of literals (their building
costs more than they save on streams of 11 KB on average).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DzYLyEzLqkwuqPQ9LXgMpD
@AOShei
AOShei merged commit ef142e7 into main Nov 22, 2026
12 checks passed
@AOShei
AOShei deleted the inflate-speed branch November 22, 2026 22:15
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.

1 participant