ALL INTELLIGENCE
Hacker News AIT2·Tools

LZ0 Debuts Asymmetric LZ Compressor with Zero-Alloc Decoder

Original title: Show HN: LZ0 -An asymmetric LZ compressor with ultra minimal, 0-alloc decoder
58AI Score

LZ0 is an experimental lossless compression format and C implementation that shifts complexity from the decoder to the encoder. Its decoder performs no dynamic memory allocation, keeps minimal state, and processes explicit LZ tokens at hundreds of megabytes per second. It targets small-to-medium binary blobs and structured datasets from a few hundred kilobytes to several megabytes, emphasizing a tiny, auditable decoding routine suitable for resource-constrained embedding.

Full Report

Contents8 sections

LZ0

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.


1. 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(N2)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.

2. 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).

LZ0 vs. 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)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.

LZ0 vs. 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

CharacteristicZX0LZ0 (H2C8)UPKR
Compression ModelLZ77 / LZSSLZ-style explicit tokensLZ + adaptive entropy coding
Entropy CodingElias gammaElias gamma (hybrid)Adaptive rANS / rABS
Encoder ComplexityModerate / ExhaustiveHigh (SA-IS + Sparse DP)High
Encoding SpeedVery slow (>1> 1 MiB)Fast (seconds on 5 MiB)Fast
Decoder ComplexityUltra-simpleUltra-simple (~150 lines C)Complex
Decoder RAMNone (0 bytes malloc)None (0 bytes malloc)Requires state/tables
Bitstream CompatibilityZX0Independent formatIndependent format

3. Compression Model & Token Stream

The LZ0 decoder interprets a sequence of four semantic operations:

TokenParametersDescription
LiteralRunLength L≥1L \ge 1, raw bytesCopies LL uncompressed bytes directly from the stream.
MatchLastLength L≥1L \ge 1Copies LL bytes starting at out_pos - last_offset.
MatchNewOffset ≥1\ge 1, Length L≥2L \ge 2Updates last_offset = offset, then copies LL bytes from out_pos - offset.
EOFNoneTerminates stream decoding cleanly.

Decoder State

The decoder maintains minimal state:

  1. out_pos: Current write position in the output buffer.
  2. last_offset: Previously used match offset (initialized to 1).
  3. 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.


(注:更多技术实现细节与完整 API 文档请查阅原项目 README)