Cutting the BWT into cache-sized slabs, exactly and in near-linear time¶
Each run of a block's next-row map lands in fewer than two runs on average, which lets an exact optimiser find cache-sized slabs of rows in near-linear time.
Undoing bzip2's block sort means walking a permutation. Each of a block's rows, up to 900 thousand of them, names the next row to visit, and the decoder follows those names one at a time until it has visited every row. The walk jumps all over the block, so most steps miss the processor's caches. One way to tame it is to cut the rows into contiguous slabs, ranges of consecutive rows small enough to sit in a cache, chosen so that as many steps as possible start and end inside the same slab.
Choosing the best cuts is a classic dynamic programme, and done naively it costs time proportional to the square of the number of possible cut points. This post reports a mathematical result that removes the square for the cost that matters here. The next-row map of any block is built from bands, runs of rows that all shift by the same amount, and we proved that the bands' overlaps form a graph with at most \(2B-1\) edges for \(B\) bands, whatever the block. That bound places the locality objective in a family of nearly Monge problems that a recent algorithm by Yanqiao Chen solves exactly in near-linear time. Our implementation of that algorithm finds the optimal cuts for a whole 900 KiB block in between a sixth of a second and just over a second on a small cloud machine, and on real data it rediscovers groupings such as "the digits" without being told what a digit is.
The route is as follows. Section 1 recalls what the next-row map looks like and what a band is. Section 2 proves the edge bound on a seven-row example. Section 3 explains why that bound makes the optimiser fast, and section 4 shows that other natural costs break it badly. Section 5 reports what the optimiser found on 150 real blocks, and section 6 says what this does and does not yet mean for the decoder.
1. What is a band?¶
bzip2 compresses each block by sorting all of its rotations and keeping the last column of the sorted list: the Burrows–Wheeler transform (BWT). Because the rows are sorted, the first column holds the block's bytes in order: all the spaces, then all the digits, then the letters, and so on. To undo the sort, the decoder needs, for each row, the row holding the next byte of the text, and an earlier post showed that this next-row map has a striking shape. Every run of equal bytes in the last column is sent, as a whole, to a contiguous range of rows, keeping its order. Within a run the map is a single shift: row \(x\) goes to row \(x + d\) for one fixed \(d\).
We call each maximal range of rows with one shift a band, and write \(B\) for the number of bands, which is the number of runs in the last column. On well-compressed data it is far smaller than the number of rows. One 900 KiB block of a Wikidata dump has about 66 thousand bands; blocks of Markdown documentation, Rust source and JSON have 250 to 375 thousand. Everything below works on bands, not rows, so its costs are counted in \(B\).
A slab is then a run of consecutive bands, and a step of the walk stays inside a slab when it starts at a row of the slab and lands on a row of the same slab. Since rows are sorted by their first byte, a slab is a range of first bytes, such as the rows that begin with a digit. A step stays inside that slab exactly when a digit in the text sits next to another digit.
2. Why can a band touch only two neighbours on average?¶
To find good slabs we need to know which bands send rows to which. Draw an edge from band \(a\) to band \(b\) whenever some rows of \(a\) land in \(b\), weighted by how many. A band could in principle send rows to many bands, so this graph might have up to \(B^2\) edges. It never has more than \(2B - 1\).
The seven-row example below shows why. The rows split into three bands, at rows 3 and 5. Each band lands on one contiguous range of rows. Since the map is a permutation, those three landing ranges also cover all seven rows exactly once, so they split the rows a second way, at rows 2 and 4. Each edge of the graph is one piece of the rows cut by both sets of cuts at once: the rows that belong to band \(b\) and arrive from band \(a\).
Two ways of cutting the same rows, and the pieces they make together
Two sets of cuts on a line, with \(B - 1\) interior cuts each, make at most \(2(B-1)\) cuts in all and therefore at most \(2B - 1\) pieces. So for every permutation built from \(B\) bands, and so for every block bzip2 can produce,
where \(c\) counts the places where a band boundary and a landing boundary coincide, so that the two cuts are one. The weights of the edges add up to the number of rows, since each row sits in exactly one piece. On average, then, a band sends its rows to fewer than two bands, and the whole overlap graph can be built in one pass over the bands, in time proportional to \(B\).
We checked the identity on every permutation of up to seven elements, 5,913 of them, and on 38,728 edges of real and synthetic blocks, with no exception.
3. Why does an edge bound make the optimiser fast?¶
The partition problem asks for cut points that minimise the sum of a cost over the slabs. We used a cost of the form
The first term keeps slabs small, and the weight \(\gamma\) sets their typical size; the second rewards locality. The standard dynamic programme tries every earlier cut for every later one, which is quadratic in \(B\): tens of billions of cost evaluations for a block of 330 thousand bands.
Faster exact algorithms exist when the cost satisfies the Monge condition. Write \(\text{cost}[i, j]\) for the cost of a slab from band \(i\) up to, but not including, band \(j\). The condition asks that
for every \(i < j\): a long slab and a short one nested inside it cost at least as much as the two staggered slabs that share their ends. When that holds everywhere, the best cut points move monotonically, and the search no longer needs to try every pair. The squared-size term alone satisfies it.
The reward term is what breaks it, and the edge bound says by how much. An edge between bands \(a\) and \(b\) earns its reward only for slabs that contain both, which is a rectangle in the table of (start, end) pairs. The difference between the two sides of the inequality is non-zero for such a rectangle at a single corner, so each edge produces exactly one place where the Monge inequality can fail. With at most \(2B-1\) edges, at most \(2B-1\) of the roughly \(B^2/2\) cells violate the condition.
This is the situation that Chen's paper (arXiv:2611.34733, November 2026) handles: a Monge cost minus \(r\) rectangle rewards can be minimised exactly in time proportional to \((B + r)\log^3 B\). Our band bound makes \(r\) at most \(2B - 1\) for every block, so the locality partition of any BWT block is always within that algorithm's reach. We implemented the algorithm as a kinetic tournament tree in safe Rust with exact 128-bit integer arithmetic. We checked it against the quadratic programme on 17,739 exhaustive small cases and 500 randomised ones, and on windows of real blocks, with identical answers and identical cut points every time.
4. Which costs break the structure?¶
The locality objective is not the only partition one might want. A decoder that stores a small index per slab cares about the cost of searching that index, or of fitting the slab with a straight line so that a row can be predicted from its position. We measured how often each natural cost violates the Monge inequality on windows of up to 1,024 bands taken from real blocks, and on synthetic permutations.
The share of cells that violate the Monge inequality, on windows of real blocks:
| Cost of a slab | Violating cells, real blocks | Verdict |
|---|---|---|
| Linear scan, squared size, or variance about the mean | none | exactly Monge |
| Locality (squared size minus steps kept inside) | at most 0.02% | sparse, as proved |
| Binary search rounded up to whole steps | 1.6%, the same on every input | sparse, as proved |
| Best straight-line fit, worst-case error | 1.0% to 3.1% | in between |
| Best straight-line fit, squared error | 29% to 45% | dense |
| Binary search, exact logarithm | 27% to 69% | dense |
The last two rows are the surprise. The exact-logarithm search cost is perfectly Monge when all bands have the same width, and it fails only because real run lengths are heavy-tailed. The smallest failure is three bands of widths one, five and one, while one, four and one is fine. The squared-error line fit is fragile in a different way. A synthetic permutation with equal bands has no violations until a single pair of neighbouring bands is merged, and that one change makes 31% of all cells fail. For these costs no sparse correction exists, and Chen's algorithm does not apply.
Two costs that count where a slab's rows land, the number of distinct destination bands and the number of separate ranges they land on, sit on the opposite side: they never break the inequality the other way, so they are exactly anti-Monge.
5. What did the optimiser find on real blocks?¶
We ran the optimiser on 150 full blocks: 116 from the Wikidata dump and 34 from the Markdown, Rust and JSON corpora. Its running time grows almost linearly with the number of bands, as the theory predicts. For one full block of each corpus:
| Block | Bands | Optimal cuts found in | Memory |
|---|---|---|---|
| Wikidata | 66,401 | 0.16 to 0.19 s | 15 MB |
| Rust source | 256,388 | 0.66 to 0.89 s | 61 MB |
| JSON | 259,059 | 0.76 to 0.95 s | 60 MB |
| Markdown | 374,940 | 1.04 to 1.19 s | 88 MB |
These times come from a two-core cloud virtual machine, with the lower figure from a build tuned for that processor. They are offline measurements of the optimiser, not of decoding. On windows of 4,096 bands, where the quadratic programme can still be run for comparison, the near-linear one was 19 to 37 times faster on every repeat that passed our noise check.
When the slabs are allowed to grow to about half a block, the optimal cuts keep 72% of Wikidata's steps inside their slab, and 65% to 69% on the other corpora. That looks like a large win over cutting the same number of slabs evenly, which keeps under 8%, but the comparison flatters the optimiser. Its best partitions have a "core and dust" shape: three to five large slabs holding the bulk of the rows, plus hundreds or thousands of tiny ones. Simply halving the block keeps 72% on Wikidata too, because the midpoint of the byte order falls near the lowercase letters and so already separates them from digits and punctuation.
The optimiser adds most where slabs are smaller, around 85 to 150 KiB each, about the size of a core's private second-level cache. There we compare against the share of steps that would stay inside the same slabs if the steps were random, which for slabs of \(|S_k|\) rows out of \(n\) is \(\sum_k (|S_k|/n)^2\). The optimal slabs keep 3.6 to 5.4 times more steps inside than that baseline, and they line up with real structure in the text. In the first block of Wikidata and of the JSON corpus, the hottest slab is exactly the rows that begin with a digit, 0 to 9; in the first block of Rust source it is the whitespace and control bytes of indentation. Nothing in the objective knows about characters; the grouping comes from the walk alone.
6. What this means for the decoder¶
The chain of the argument is short. The next-row map is a set of bands; two sets of \(B\) intervals can only overlap in \(2B-1\) pieces; so the locality cost has at most \(2B-1\) Monge defects; so Chen's algorithm finds the exact optimal slabs in near-linear time, fast enough to run on whole blocks. The same census shows which partition costs cannot be treated this way.
This is a result about the structure of the problem, not yet a faster decoder. Nothing here has changed mibz2's code, and the timings are of an offline optimiser on a small virtual machine. The finding gives the decoder a new question to test: whether keeping the hot slab of a block in a core's cache, or storing that slab in a denser form than the rest, makes the walk cheaper on the dense blocks where it costs most. The lesson reaches beyond bzip2. Whenever a permutation is made of a few shifted runs, any objective that rewards keeping its steps local inherits the same \(2B-1\) bound, and with it an exact near-linear partition.
Sources:
- The frozen registration of the four theorems, the three questions and their decision rules, written before any data.
- The results receipt, with every figure above, the verification counts and the manifest of raw data.
- The solver and census code, in safe Rust.
- Yanqiao Chen, Min-plus products and dynamic programming with few violations of the adjacent Monge inequalities, arXiv, 25 November 2026, from our reading of recent permutation papers.
- The earlier post on the next-row map as one shift per run.