[mibz2 · Research blog](<https://lmmx.github.io/mibz2/blog/index.md>)

# A leaner expander for bzip2's first run-length step

Spotting runs with half the vector loads and writing them without library calls nearly halves bzip2's run-length stage, decoding ordinary text about 7% faster.

30 November 2026 · 9 min read · [Research result](<#5-what-ships-and-what-comes-next>)

The last thing a bzip2 decoder does to a block, before checking it, is undo the very first thing the compressor did. Before any of bzip2's other transforms, the compressor shortens every run of four or more identical bytes. It writes the first four bytes, then one count byte giving how many extra copies follow, from 0 to 255. The format calls this first run-length encoding, RLE1. Undoing it means scanning the block, spotting every stretch of four equal bytes, and replacing those four bytes and their count by the full run.

mibz2 0.5.22 ships a new kernel for that step. It finds runs with two vector loads per 32 byte positions where the old kernel used four. It also writes output with plain 32-byte vector stores instead of calling the library's copy and fill routines. Measured on its own, the stage takes 45% less time. On a whole decode of ordinary text with one thread, mibz2 is about 7% faster. That gain replicated on three processors, including our physical Intel i9 workstation. On the [Wikidata sample](<https://lmmx.github.io/mibz2/blog/2026-11-28-when-the-virtual-machine-lies/index.md>) that most of our benchmarks use, the step is about 1% of a decode. No change was detected on the cloud runners or at most thread counts on the i9, where four threads ran 1.8% faster.

Earlier versions of the kernel kept looking 2% to 3% slower on a benchmark of random bytes. Section 4 uses a fixed-layout experiment to show that code placement, not the kernel, caused that loss. Section 1 works the run-finding trick on eight bytes. Section 2 scales it to the real kernel, section 3 gives the measurements, and section 5 says what ships.

**Takeaways**

- **Half the loads, and no library calls.** One neighbour comparison per byte, combined in an ordinary register, finds every run of four. Plain fixed-width vector stores replace the library's copy and fill routines.
- **The stage cost fell by 45%.** On Wikidata it dropped from 0.19 to 0.11 processor cycles per byte.
- **Ordinary text decodes 6.6% to 7.4% faster with one thread.** The gain held beyond noise on two AMD cloud processors and our Intel i9. Wikidata stayed within noise except for a 1.8% speedup at four threads on the i9, matching its small RLE1 share.
- **A same-binary switch cleared the kernel of a phantom loss.** Recompiled next to the new kernel, the old kernel itself ran 3% slower on random bytes from code placement alone.

## 1\. How do you find four equal bytes without looking at four bytes?

Take an encoded fragment of eight bytes, `b a a a a 2 c d`. It encodes one `b`, then a run of `a` with four literal bytes and a count byte of 2 (six `a`s in all), then `c` and `d`. The decoder must find where the run starts (position 1) and expand it to `baaaaaacd`.

The direct test asks, at each position \\(k\\), whether bytes \\(k\\), \\(k+1\\), \\(k+2\\) and \\(k+3\\) all match. A vector kernel checks 32 positions at once by loading the bytes four times, each load shifted by one byte, and comparing the first load against the other three. That takes four loads and three comparisons for every 32 positions. It is what mibz2's old kernel did.

The new kernel asks a smaller question: does byte \\(k\\) equal byte \\(k+1\\)? Call the answer \\(e\_k\\). Four equal bytes in a row mean three equal neighbouring pairs, so a run starts at \\(k\\) when

\\\[t\_k = e\_k \\wedge e\_{k+1} \\wedge e\_{k+2}.\\\]

Finding the run in b a a a a 2 c d from one comparison of neighbours

Conceptual, worked by hand. Position 1 is the only place where three neighbouring pairs are equal, so the run of a starts there. Its count byte, 2, sits four places later.

Comparing neighbours needs only two loads: the bytes from \\(k\\) and the bytes from \\(k+1\\). The vector unit packs its 32 answers into 32 bits of an ordinary integer, one bit per position. Shifting that integer right by one and by two lines \\(e\_{k+1}\\) and \\(e\_{k+2}\\) up under \\(e\_k\\). Two shifts and two ANDs on a general register replace two vector loads and two vector comparisons.

## 2\. What changes at full size?

The real kernel runs two 32-position comparisons side by side, packing 64 neighbour bits into one 64-bit integer. The last two positions lack \\(e\_{k+1}\\) or \\(e\_{k+2}\\) inside the window. Each pass therefore decides 62 positions and advances 62 bytes. That is four loads for 62 positions, against the old kernel's four for 32.

Most passes over ordinary data find no run. When the 64-bit result is zero, the kernel writes the 62 input bytes to the output as two 32-byte stores from the registers it already loaded, then moves on. When the result is non-zero, it drains the runs in order, lowest bit first. It copies the literals before each run with one or two 32-byte stores, reads the run's byte and count, and fills 4 to 259 output bytes with 32-byte stores of that byte.

Writing a fixed 32 or 64 bytes when fewer are needed is safe only with room past the output's end. The kernel keeps at least 384 spare bytes reserved ahead of its writes. Any overshoot lands in reserved memory that the next write overwrites. This is the same idea as the [fixed-width writes](<https://lmmx.github.io/mibz2/blog/2026-11-21-sixteen-at-a-stroke/index.md>) that sped up the open-addressing BWT stage's queue filing and walk output. When a short copy's length depends on the data, write a fixed width and advance by the true length, so the processor never guesses a branch. That earlier post named this run-length step as a candidate; this kernel puts it to work.

The old kernel handed each literal stretch and each run to the standard library's copy and fill routines. Those routines shine on long copies, not short ones. Each call adds call overhead, a length dispatch, and an instruction that resets the upper halves of the vector registers. The new kernel makes none of these calls inside its loop.

**Safety.** The kernel still uses unchecked pointer writes, as its predecessor did. Each write carries a written safety argument showing it stays inside the reserved buffer. The change removes more unchecked code than it adds, because the old kernel and an unused second variant were deleted. Its tests pass under Miri, Rust's interpreter for detecting undefined behaviour. Across four run-heavy stream shapes, 8,400 decodes of 2,100 randomly generated valid streams matched libbzip2, the reference implementation, byte for byte.

We did not measure inside this kernel how much gain came from the cheaper test versus the direct stores. One related measurement points to the stores. When the same two-load test went alone into a separate pass that only lists runs, that pass got no faster.

## 3\. How much faster is it, and where?

On Wikidata with one processor, our per-stage profile puts RLE1 at 0.19 processor cycles per byte before the change and 0.11 after. That is a 45.1% drop, with a 95% interval of 44.7% to 45.5%. Because RLE1 is about 1% of a Wikidata decode, the whole decode can gain at most about half a percent, too little for most runs to resolve.

Ordinary text is where the gain stands out. The figure below shows the ratio of new decode time to old for one thread on our text benchmark: the text files of mibz2's own repository at a fixed commit, code and prose, repeated. Each of the three independent runs compares builds that differ in this kernel alone. Ratios below 1 are faster, and the bars show 95% intervals across each run's paired rounds.

Ordinary text, one thread: new decode time over old

Measured. The two cloud runs used four virtual processors (ten rounds each). The i9 run is the release gate on our physical workstation, twelve paired rounds. Intervals describe spread within each run.

The gain also shows on the two run-heavy benchmark files: long runs of varied bytes, and a single byte repeated. On the first cloud processor they decode 5.7% to 6.9% and 8.3% to 10.2% faster at one, two and four threads. With more threads the text gain fades into noise. At two and four cloud threads its intervals include zero, and on the i9 every text cell from 2 to 20 threads shows no change detected either way. Each worker expands its own block's runs, so with several workers the step runs in parallel and leaves the critical path.

No file decoded more slowly beyond noise in any of these runs. The reserved headroom is unchanged and the output is the same size, so no file showed a structural memory increase. Peak memory rose by at most 4.4% on one small cloud file at two threads, and by at most 1.3% (0.6 MB) across primary files on the i9.

## 4\. Why did earlier versions look slower on random bytes?

Earlier versions of this kernel measured 2% to 3% slower on random bytes. In random data, RLE1 is under 1% of a decode and almost no runs occur. A kernel that clears run-free windows faster should not slow that file down, which pointed to code layout. Changing one function's size shifts every function compiled after it. Because the processor fetches and caches instructions in 64-byte lines, a function that started on one line can now straddle two. We checked that earlier prototype: none of the 327 comparable functions in mibz2's decoder kept its offset within a 64-byte line.

To separate the kernel from its placement, we compiled both kernels into one program behind a run-time switch. Both kernels then share every other byte of the program's code. Running the old kernel inside the new program, against the old program, isolates placement. Running the new kernel against the old kernel, inside the same program, isolates the kernel.

The placement arm slowed beyond noise on two-thread runs, even though it ran the unchanged old kernel. Random bytes took 3.0% longer (0.6% to 5.4%), and a patterned file took 7.5% longer. The kernel arm was slower beyond noise nowhere. On random bytes its ratios were 0.997, 0.986 and 0.993 at one, two and four threads. On one-thread text it ran 5.9% faster. The losses that had held the kernel back were the size of losses that code placement produces on its own.

This changes how we judge small losses on files where the edited code barely runs. A 2% to 3% shift there sits inside normal code-placement noise. A same-binary switch, holding layout fixed while changing only the kernel, is now how we test a suspicious one.

## 5\. What ships and what comes next

The two-load kernel is the default on processors with AVX2 in [mibz2 0.5.22](<https://github.com/lmmx/mibz2/releases/tag/v0.5.22>), with no switch. The old kernel is gone. Processors without AVX2 keep the unchanged scalar loop.

The chain is short. A run of four equal bytes is three equal neighbouring pairs, so testing neighbours takes half the loads and four integer instructions. Fixed-width stores into reserved room remove the library calls from the loop. Together they nearly halve the stage, making single-threaded text decoding about 7% faster on every processor we measured.

Testing neighbours applies wherever a decoder looks for a fixed-length repeat. The related result in section 2 is a caution, though: in a separate pass that only listed runs, the two-load test gained nothing measurable on its own. How much of this kernel's gain comes from the test versus the stores stays open. A same-binary switch like the one in section 4 could settle it.

---

**Sources:**

- The kernel: [src/rle.rs at the release](<https://github.com/lmmx/mibz2/blob/91b7d73f2986c9c6c276e658e5c742c79356a13d/src/rle.rs>), shipped in [PR \#1703](<https://github.com/lmmx/mibz2/pull/1703>); its decision rule and benchmark table are in the [lab notebook](<https://github.com/lmmx/mibz2/blob/69bc1ec026ee9223ef9439fdd7e6a6ba8a1fd4fd/docs/notebook/2026-11-30-rle1-raw-unrle-adopt-registration.md>).
- The same-binary switch experiment: [registration and result](<https://github.com/lmmx/mibz2/blob/69bc1ec026ee9223ef9439fdd7e6a6ba8a1fd4fd/docs/notebook/2026-11-30-rle1-unrle-runtime-switch-registration.md>); the code-placement census: [lab notebook](<https://github.com/lmmx/mibz2/blob/69bc1ec026ee9223ef9439fdd7e6a6ba8a1fd4fd/docs/notebook/2026-11-29-explore-rle1-placebo-layout.md>).
- The differential test against libbzip2: [PR \#1721](<https://github.com/lmmx/mibz2/pull/1721>).
- The 0.5.22 release gates (second cloud processor and the i9): [release record](<https://github.com/lmmx/mibz2/blob/69bc1ec026ee9223ef9439fdd7e6a6ba8a1fd4fd/frontier/deltas/2026-11-30-release-0-5-22-readback.md>).
- Earlier posts: [Sixteen at a stroke](<https://lmmx.github.io/mibz2/blog/2026-11-21-sixteen-at-a-stroke/index.md>), [When the virtual machine lies](<https://lmmx.github.io/mibz2/blog/2026-11-28-when-the-virtual-machine-lies/index.md>), [Why mibz2 stops scaling](<https://lmmx.github.io/mibz2/blog/2026-11-07-why-mibz2-stops-scaling/index.md>).
