Field notes/Memoryrusty_zlib

Tuning a zlib-rs Fork Without Changing One Output Byte

rusty_zlib 0.1.1 speeds up deflate and inflate while writing identical bytes. How each change was gated, which one the clock rejected, and why levels 6 and 9 stayed flat.

Signed by Tim Almond·
Painted hayfield at dawn with round bales stacked in a low pyramid, two hand rakes leaning on them and a lit lantern on the ground, loose cut hay lying in rows behind
How do you speed up a zlib-rs fork without changing its compressed output?

Each change is its own commit, and it must produce the same output length and FNV hash as the baseline before it lands. rusty_zlib 0.1.1 also checks 92 unit tests and 280 tests against a C oracle, then reports instruction counts and paired clock runs.

Ship it

Identical Output, 1.19x Faster

Level 1 deflate against the fork's own 0.1.0. Levels 6 and 9 are flat, and every level matches upstream byte for byte.

Open the rusty_zlib conversion pin →

I wanted to know whether a zlib-rs fork could get faster without changing a single byte it writes. Version 0.1.1 of rusty_zlib is the first attempt. Level 1 deflate runs 1.189 times faster than 0.1.0, decompression is a few percent quicker, and the compressed output is identical. It also shows how easy it is to fool yourself with a counter.

This zlib-rs fork keeps its upstream's shape on purpose. The repo is Remade-With-Rust/rusty_zlib, a fork of the Trifecta Tech Foundation's zlib-rs. It starts from upstream 0.6.9 plus one later fix, and its HOUSE.md lists every change we make.

Ground rules for changing someone else's compressor

A compressor has a contract. For the same input and settings, it should write the same bytes. If a change breaks that, files made by two versions stop matching and nobody can tell whether the old or new one is wrong.

So every change here is a separate commit, and it must clear a gate first. The harness compares output length and an FNV hash against the baseline. The crate's 92 tests pass. A second test crate with 280 tests compares behaviour against a C zlib-ng, which is used as a referee and never ships.

The change then needs a number. We use callgrind to count instructions, because the count barely moves between runs. If a change does not reduce work, it is reverted, and the commit message records the census.

Keeping the fork rebasable is part of the contract. Inside the workspace the dependency key is still zlib-rs, set with package = "rusty_zlib", and the crate directory keeps its old name. That keeps upstream's member crates, feature references and paths unchanged, so rebasing onto the next upstream tag stays cheap. The C-ABI crate, libz-rs-sys, is not published at all. We will not put a second crate on crates.io under upstream's name, and if we need one it gets a house name first. A zlib-rs fork that cannot follow its parent is just a copy.

Instruction counts are not time, though. So the clock decides, using paired runs. I'll get to why that matters in the last section.

What the 0.1.1 changes did

Setup cost on small inputs

Small inputs pay for setup on every call. The head table was being zeroed twice, so it is now zeroed once. The symbol buffer is never zeroed, since it is MaybeUninit. fill_window exits early when it has nothing to do. These help calls on 4 KiB inputs most.

Level 1 and the hash chain

Level 1, deflate_quick, got fixed-size window views, a steady-state inner loop and slices held in locals. Where an index is provably in range, the code now says so with a mask or a re-slice, so the compiler drops the bounds check. That cut level 1 instructions by 21.5 percent.

The medium and slow levels got similar proofs in the hash insert and the tally, plus a byte-mask reject in longest_match, a fused chain step and a small table for the end-probe offset. The level 9 walk is kept out of line with inline(never), since inlining it caused spills.

Inflate and the Huffman builders

On the tree side, the Huffman heap became a Heap<N> sized to its tree with packed keys. Trees are built in place, encode_dist lost its branch and bit reversal uses a table. Inflate got exact-length root slices, an unchecked literal push, a cheaper loop guard and CodeLens on local state. Decompression instructions fell 19.7 percent on the d6 workload.

Reading the clock

Paired runs against 0.1.0

The harness pins one core, runs both builds in alternating order and stops the run if the work does not match. Each workload ran 16 pairs. Level 1 on a whole file was 1.189 times faster, winning 16 of 16. Level 1 on 512 inputs of 4 KiB was 1.390 times, also 16 of 16.

Decompression was 1.037 times on a whole file, and 1.024 times on small inputs, winning 12 of 16 there. The release notes call the small win faster, and its z-score of 2.00 is the weakest in the table.

The change the clock rejected

One brick read the root table in the inflate fast loop without bounds checks. It dropped 4.8 percent of instructions. The clock said 0.879 times, which is slower. A bisect found it, and the commit was reverted.

My guess is that the unchecked reads changed code layout in a way the counter cannot see. That is a guess, not a finding. The point stands: a counter is a hint, and the clock gets the vote.

Why levels 6 and 9 stayed flat

Levels 6 and 9 executed about 10 percent fewer instructions. Wall time did not move: 0.975 and 0.997, winning 5 and 6 of 16 pairs. The notes say the chain walk there waits on memory, not on instructions.

So 0.1.1 makes no time claim for those levels, and that is the next campaign's question. It is the kind of result I expected to be wrong about: ten percent less work, no gain. Nobody wants to publish that line, and a zlib-rs fork that hides it is not worth trusting. For the product view of this crate, see MATA's note on the pure-Rust zlib. The rusty_zlib repo page has the install line, and rusty_zstd is the sibling compressor. Our parser limits note and allocator API guide cover the same careful style, and rusty_pdf reads PDF Flate streams through upstream zlib-rs by way of flate2.

FAQ

Quick answers for builders evaluating this technology.

Which workloads got faster in rusty_zlib 0.1.1?

Against 0.1.0, level 1 on a whole file ran 1.189 times faster and level 1 on 512 inputs of 4 KiB ran 1.390 times faster, each winning 16 of 16 pairs. Decompression gained 1.037 times on a whole file and 1.024 times on small inputs, winning 12 of 16 pairs there.

Why are levels 6 and 9 listed as flat?

They ran about 10 percent fewer instructions and did not run faster. Level 6 measured 0.975 and level 9 measured 0.997, winning 5 and 6 of 16 pairs. The release notes say the chain walk there is bound by memory latency, so no time claim is made for those levels.

What did the clock reject?

A change that read the Huffman root table without bounds checks cut instructions by 4.8 percent but measured 0.879 times by the clock, which is slower. A bisect confirmed it, and the commit was reverted. Fewer instructions are not a speed result until the clock agrees.

How was the output checked on other CPUs?

The notes report byte-identical output on x86 with AVX2, x86 scalar under qemu as a Nehalem, aarch64 NEON under qemu, and wasm simd128. Miri is clean on the inflate and deflate unit tests. The qemu runs prove correctness, not timing.

Is the C library involved anywhere in the repository?

Only as a referee. The test-libz-rs-sys crate depends on libz-sys with the zlib-ng backend as a conformance oracle. It is a test crate and is never published, so the shipped rusty_zlib crate contains no C.