GeoZL

How PFOR works

A spatial predictor guesses each sample from its neighbours and keeps only the difference, or the residual. On a smooth tile most residuals are small, such as 3 for an elevation of 1532 m predicted as 1529 m. They are small but rarely repeat, so dictionary compressors such as zstd gain little.

PFOR (patched frame of reference) writes each residual with fewer bits. It packs a block at one small width and keeps the few values that do not fit in a patch list. Every value then sits at a known bit position, so a decoder can unpack any of them without reading the ones before it. The eight-value example below follows one block from the input to the bytes on the wire.

Plain bit packing

A byte has 8 bits, yet the value 5 needs only three, 101. Bit packing gives every value in a block the same width w and writes them back to back, so value i always starts at bit i × w. Eight values at 3 bits take 24 bits, or 3 bytes, instead of 8. The catch is that w must fit the largest value.

The example has eight predictor residuals, including one outlier. Written in binary, most of them need two or three bits. The last one needs seven.

in352746190
binary112 bits1013 bits102 bits1113 bits1003 bits1103 bits11 bit10110107 bits
seven values fit in 3 bits, one needs 7

Plain bit packing must use seven bits for every value, because w has to fit the 90. It is easy to decode but wastes four bits on each of the seven small values. Real residuals look like this all the time, at a cloud edge, a road crossing a field or a single noisy pixel.

One width, and a patch for the rest

PFOR chooses one width b per block, here b = 3, and uses it for every value. The values that do not fit are called exceptions. Here, 90 is the only exception.

An exception stays in the body, with its low bits in place. Its high bits and its position go in a side list. For 90, or 1011010 in binary, the body keeps 010 and the side list keeps 1011.

The animation stacks each value's bits, lowest first, and moves a line at b. At b = 7 nothing sticks out, but every value pays seven bits. At b = 2 the body is thin, but five values stick out. At b = 3 only the 90 does. Its four high bits leave for the exception list together with its position, and its low bits 010 stay where they were.

Choosing a width for the values 3 5 2 7 4 6 1 90 as stacks of bits. A line at b = 7 leaves no exceptions but a 56-bit body. At b = 2 the body is 16 bits but five values stick out. At b = 3 only the 90 sticks out, its high bits 1011 move to the exception list with position 7, 010 stays in the body, and the short values are padded with zeros to three bits.
a wider b means a bigger body, a narrower b means more exceptions. At b = 3 only the 90 sends 1011 to the side list

Zero padding gives the body eight uniform three-bit slots. Decoding first reads the body as if there were no exceptions at all, which gives 3 5 2 7 4 6 1 2. Then it patches each exception with one shift and one OR, value = body | (high << b). For the outlier the high bits 1011 are 11, so that is 2 | (11 << 3), which is 2 + 88 = 90.

Every body slot holds a real value, so no sentinel bit pattern is needed. The patch step also never moves a body value, so reading slot i still means reading bits i × b to i × b + b − 1.

What a block looks like

pfor cuts the values into blocks of 256. Inside an OpenZL frame the codec carries a nine-byte header, the element count and the element width, and its stream is the blocks back to back. The last block is padded with zeros, and the decoder drops them again.

Inside an OpenZL frame, pfor carries a nine-byte codec header, a 64-bit element count and an 8-bit element width, followed by blocks of 256 values back to back. Block 0, the eight example values repeated 32 times, holds b equal to 3, an exception count of 32, a format byte saying eb is 4 bits and positions are a bitmap, a 32-byte bitmap, 16 bytes of high bits, and a 96-byte body, 147 bytes in all.
The eight values repeated 32 times fill one block of 147 bytes instead of 256. Every 90 is an exception and keeps its 4 high bits 1011 apart.

A block opens with b and its exception count, and the count decides what follows. A block stores its exception positions in one of three ways.

exceptionsafter b and the count
nonethe body
1 to 31a format byte, one byte per position, the high bits, the body
32 to 255a format byte, a 32-byte bitmap, the high bits, the body

The format byte holds eb, the high bits every exception carries, and one bit that says list or bitmap. A list costs one byte per exception, so from 32 exceptions on a bitmap with one bit per value is no larger, and the encoder switches to it there. The block above has one 90 in every eight values, exactly 32 exceptions, so it uses the bitmap.

A decoder reads all three, on a CPU or on a GPU. A CPU core walks the positions in order, from the list or from the set bits of the bitmap, and patches each value as it goes. On a GPU every thread patches its own values at the same time, so the threads first turn either form into the same bitmap, as What a GPU wants shows.

The body is always 32 × b bytes, laid out in 16-byte lane groups that Fast on one CPU core explains. Block size and layout are part of the wire format, so changing either would need a new CTid. Every field is described in core/src/pfor/spec.md.

How the encoder chooses b

Choosing b trades body against exceptions. Each bit taken off b saves 32 bytes of body, but more values become exceptions, and each one costs a position and its high bits. The encoder tries every b and keeps the one with the smallest estimated block, where each exception also counts a small penalty described below.

Block size in bytes for each width b, for the eight example values repeated 32 times. b 7 gives 226 bytes, 6 gives 231, 5 gives 203, 4 gives 175, 3 gives 147, the smallest, 2 gives 199 and 1 gives 235. b 0 would need 256 exceptions and is skipped.
The block from above at every width. Below 3 bits the 5, 7, 4 and 6 become exceptions too, and they cost more than the body saves.

A histogram of bit widths gives the exception count at every b without packing anything. Positions go in a list below 32 exceptions and in a bitmap from 32 on, a width needing more than 255 exceptions is skipped, and each exception adds a 4-bit penalty so a near tie goes to fewer exceptions.

The search is not part of the format. A decoder reads whatever b a block declares, so another encoder could choose differently and every reader would still decode its frames.

Thanks

PFOR is not our idea. Marcin Zukowski, Sándor Héman, Niels Nes and Peter Boncz described patched frame of reference in Super-Scalar RAM-CPU Cache Compression, at ICDE 2006, and almost everything on this page follows from that paper.

Our block layout follows TurboPFor by powturbo and its contributors, years of careful work that made fast integer coding easy to learn from. GeoZL is an independent implementation, and its frames are not bitstream compatible with TurboPFor, so any mistake in the adaptation is ours.