Learn

Run-Length Encoding: Compressing Repeated Values

Run-length encoding replaces consecutive repeated values with a value and a count. It is easy to inspect, but its usefulness depends strongly on the lengths and arrangement of the runs.

Describe a run once

A run is a consecutive sequence containing the same value. Instead of writing that value over and over, a representation can record the value and the number of repetitions. The decoder expands each pair in order.

This idea appears in practical formats, though their layouts include details beyond a simple pair. Apache Parquet specifies a hybrid encoding that combines run-length encoding with bit packing. The simplified example below teaches the concept; it is not the Parquet wire format. Parquet encodings.

An original byte-level example

Assume an input of eight one-byte values: A, A, A, A, A, B, B, B. For this example, define an encoded pair as one byte for the value and one byte for the count. The result is (A,5), (B,3), or four bytes instead of eight, excluding any file header.

The decoder reads the first pair and writes five A values, then reads the second and writes three B values. It recovers the exact sequence because both the order and the repetition counts are present. The check is a full comparison with the original eight bytes, not just a matching total length.

Now try A, B, A, B, A, B, A, B. There are eight runs of length one. The same pair layout uses sixteen bytes, twice the original size. Both inputs have identical symbol counts. Their arrangement determines whether this simple method is useful.

The count field creates boundaries

If a count occupies one unsigned byte and zero is reserved, the largest count might be 255. A run of 300 equal values then needs at least two pairs, such as 255 and 45. Another format could use a larger or variable-length count, with different overhead.

This decision belongs to the format contract. An encoder and decoder must agree on field widths, count limits, byte order where relevant, and how invalid counts are handled. A human-readable expression such as “300 A” hides those engineering details.

Input containing a delimiter character also exposes the difference between an informal notation and a complete encoding. If commas or brackets separate text fields, how are those characters represented when they occur as actual values? A binary fixed-field layout avoids that particular ambiguity, but introduces its own explicit size limits.

Exact decoding still needs resource limits

A tiny encoded input can request a large decoded output if its counts are large. A safe decoder should enforce declared output limits before allocating or writing. It should reject truncated pairs, invalid fields, and output lengths beyond the accepted bound.

Containers may add checksums for detecting accidental corruption. The zlib specification documents one such mechanism, separate from the run-length idea itself. A checksum does not replace field validation or a resource budget. Zlib format.

When the pattern is useful

Consider a hypothetical status column sorted by status: many identical values become adjacent, creating long runs. The same records in timestamp order might alternate statuses and offer much less opportunity. Sorting can therefore change compressibility, but sorting itself costs work and may change the access pattern needed by the application.

Compare total encoded bytes, preparation cost, decode cost, and the actual query workload. Saving bytes is useful when those savings help the task rather than merely improving a ratio in isolation. Read compressed-data queries for that wider view, or dictionary compression for repeated values that need not be adjacent.