Skip to content
HN On Hacker News ↗

GitHub - seb3773/lz0: Lightweight LZ compressor with a zero-malloc, ultra-fast decoder

▲ 29 points • 18 comments • by seb3773 • 4d ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this text is a mix of AI and human-written content.

79 %

AI likelihood · overall

Mixed
21% human-written 79% AI-generated
SEGMENTS · HUMAN 0 of 12
SEGMENTS · AI 12 of 12
WORD COUNT 1,168
PEAK AI % 100% · §1
Analyzed
Nov 21
backend: pangram/v3.3
Segments scanned
12 windows
avg 97 words each
Distribution
21 / 79%
human / AI fraction
Verdict
Mixed
Pangram v3.3

Article text · 1,168 words · 12 segments analyzed

Human AI-generated
§1 AI · 100%

A lightweight, asymmetric LZ compressor designed around an extremely simple, memory-free decoder. LZ0 is an experimental lossless compression format and C implementation designed around a single guiding principle: Move complexity from the decoder to the encoder. Instead of balancing computational cost equally between compression and decompression, LZ0 deliberately spends more effort during encoding to generate a compact, structured stream that can be decompressed by an ultra-minimal routine. The decoder requires no dynamic memory allocation (malloc), maintains minimal state, and processes explicit LZ tokens at hundreds of megabytes per second. LZ0 is primarily targeted at small-to-medium binary blobs and structured datasets, typically ranging from a few hundred kilobytes to several megabytes.

§2 AI · 100%

1.

§3 AI · 100%

Design Philosophy Compression algorithms face an inherent multi-dimensional trade-off between compression ratio, encoding speed, decoding speed, and implementation complexity. LZ0 resolves this trade-off strictly from the decoder's perspective: Minimal decoder footprint: The decompression logic is tiny, auditable, and easily embedded into resource-constrained environments (bootloaders, microcontrollers, retro computing, kernel space). Zero dynamic memory: The decoder uses only a handful of stack registers and an optional bitreader struct; it operates entirely within the caller-provided buffers. Fast decompression: By avoiding complex entropy decoding (such as arithmetic or adaptive range coding), decompression minimizes branch mispredictions and per-token overhead. High compression ratio: Targets ratios comparable to ZX0 on binary and text payloads. Practical encoding speed: Avoids the exhaustive $O(N^2)$ search strategies that make optimal parsing impractically slow on megabyte-sized files. Format independence: The bitstream describes only pure reconstruction instructions; the encoder is free to evolve (better search, smarter pruning, new heuristics) without breaking or complicating the decoder.

§4 AI · 100%

2.

§5 AI · 100%

Where LZ0 Fits LZ0 occupies a distinct design niche between simple retro LZ formats (like ZX0) and modern adaptive entropy-coded LZ compressors (like UPKR).

§6 AI · 100%

LZ0 vs.

§7 AI · 100%

ZX0 ZX0 established the gold standard for minimal-decoder LZ compression, pairing a byte-by-byte overlapping copy decoder with an optimal-rate dynamic programming parser. However, ZX0's exhaustive search can take several minutes on inputs larger than 1 MiB. LZ0 shares ZX0's philosophy of decoder simplicity and Elias-based variable integer coding, but adopts a fundamentally faster encoder architecture (combining a hash chain with an $O(N)$ SA-IS suffix array and a sparse dynamic programming parser) and its own bitstream grammar. This produces comparable ratios in seconds instead of minutes.

§8 AI · 100%

LZ0 vs.

§9 AI · 100%

UPKR UPKR achieves high compression ratios by coupling LZ matching with adaptive entropy coding (rANS / rABS). This requires the decoder to maintain probability distributions and perform entropy-state updates on every token. LZ0 deliberately eschews adaptive entropy coding. Its decoder processes raw bits and Elias gamma integers, trading a fraction of compression ratio for dramatic simplicity and higher decoding throughput. Architectural Comparison Characteristic ZX0 LZ0 (H2C8) UPKR Compression Model LZ77 / LZSS LZ-style explicit tokens LZ + adaptive entropy coding Entropy Coding Elias gamma Elias gamma (hybrid) Adaptive rANS / rABS Encoder Complexity Moderate / Exhaustive High (SA-IS + Sparse DP) High Encoding Speed Very slow ($> 1$ MiB) Fast (seconds on 5 MiB) Fast Decoder Complexity Ultra-simple Ultra-simple (~150 lines C) Complex Decoder RAM None (0 bytes malloc) None (0 bytes malloc) Requires state/tables Bitstream Compatibility ZX0 Independent format Independent format 3. Compression Model & Token Stream The LZ0 decoder interprets a sequence of four semantic operations: Token Parameters Description LiteralRun Length $L \ge 1$, raw bytes Copies $L$ uncompressed bytes directly from the stream. MatchLast Length $L \ge 1$ Copies $L$ bytes starting at out_pos - last_offset. MatchNew Offset $\ge 1$, Length $L \ge 2$ Updates last_offset = offset, then copies $L$ bytes from out_pos - offset. EOF None Terminates stream decoding cleanly. Decoder State The decoder maintains minimal state: out_pos: Current write position in the output buffer. last_offset: Previously used match offset (initialized to 1). Bitreader state: Current byte pointer, mask, and bit buffer. Overlapping Copy Semantics Matches are copied byte-by-byte (out[out_pos + i] = out[src + i]). Because src = out_pos - offset, when length > offset the copy automatically repeats earlier bytes. For example, an offset of 1 with a length of 8 expands a single byte into an 8-byte run. Standard non-overlapping memcpy must not be used.

§10 AI · 100%

4.

§11 AI · 100%

Format & Bitstream Specification LZ0 differentiates between the raw compressed bitstream and the on-disk container format. 4.1 On-Disk Container (CLI Format) Files stored on disk or handled by the lz0 CLI wrap the raw payload in a minimal 8-byte header: +-------------------+-----------------------------+-------------------------+ | Magic (4 bytes) | Uncompressed Size (4 bytes) | Raw Payload (N bytes) | | 'L' 'Z' '0' '1' | uint32 little-endian | Raw LZ0 bitstream | +-------------------+-----------------------------+-------------------------+ If uncompressed_size == 0, the payload is 0 bytes long. The raw decoder rejects empty inputs; empty files are handled strictly at the container level. 4.2 Raw Bitstream Layout The raw bitstream has no magic bytes, header, or length metadata. It is normative according to docs/LZ0_FORMAT.md. Bit packing: Packed most-significant-bit first (MSB-first) in each control byte (mask sequence: 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01). Interleaved raw bytes: Literal byte payloads and the 8-bit offset LSB byte are stored unaligned as raw bytes in stream order. When a raw byte is read, the bitreader consumes the next byte from the source; subsequent bits are drawn from a fresh control byte (no backtrack bit-stealing). 4.3 Grammar & Transitions Implied First Decision Bit: Every stream begins with a LiteralRun. The leading decision bit 0 is omitted. The decoder starts immediately by decoding the literal run length. State transitions: After LiteralRun: Bit 0 $\to$ MatchLast. Bit 1 $\to$ enter new_loop. After MatchLast: Bit 0 $\to$ next LiteralRun. Bit 1 $\to$ enter new_loop. Inside new_loop: Bit 0 $\to$ EOF (Stream ends). Bit 1 $\to$ MatchNew (Reads offset and length). After MatchNew: Bit 0 $\to$ next LiteralRun. Bit 1 $\to$ continue in new_loop. 4.4 Elias Gamma Coding Used for variable-length integers ($v \ge 1$): Let $k = \lfloor \log_2(v) \rfloor$. The encoder emits $k$ pairs of (0, bit) followed by a stop bit 1. Standard (uninverted): Used for literal length and MatchLast length. Inverted: Used for offset MSB. Payload bits are complemented (bit ^ 1). Prefix zeros and the stop bit remain unchanged. 4.5 Offset Encoding (LSB=8) Valid offsets satisfy $1 \le \text{offset} \le \text{out_pos}$. Offsets are split into an MSB and an 8-bit remainder: $$\text{msb} = \lfloor (\text{offset} - 1) / 256 \rfloor + 1 = ((\text{offset} - 1) \gg 8) + 1$$ $$\text{rem} = (\text{offset} - 1) \ & \ 255$$ $\text{msb}$ is written as an inverted Elias gamma code. The remainder is written as a raw byte: $255 - \text{rem}$. The decoder reconstructs the offset via: $\text{offset} = \text{msb} \times 256 - \text{raw_byte}$. 4.6 MatchNew Length Coding (LEN=3) Matches with a new offset require $L \ge 2$. Let $v = L - 1$: Short lengths ($2 \le L \le 8$, so $1 \le v \le 7$): Encoded as 3 fixed bits representing $v - 1$ in MSB-first order (000 for $L=2$ up to 110 for $L=8$). Long lengths ($L \ge 9$, so $v \ge 8$): Encoded with an escape prefix 111 followed by $\text{Elias}(v - 7)$ (uninverted).

§12 AI · 100%

5.