deflate_parallel
Mojo function 🡭
deflate_parallel
fn def deflate_parallel(data: List[UInt8], max_chain: Int = Int(32), max_lazy: Int = Int(64), max_workers: Int = Int(0)) -> List[UInt8]deflate, with the LZ77 pass split across cores.
The match search is about 85% of an encode and the Huffman pass the rest, so this splits the first and leaves the second alone: each chunk is tokenized on its own core, the token lists are concatenated in order, their symbol frequencies are summed, and the whole thing is written as one block under one code.
That is what makes it nearly free in bytes. Coding each chunk as its own block instead would give every chunk a dynamic Huffman header of its own, which on a small image can cost more than the image itself. Sharing one code leaves only the matches that would have reached across a chunk boundary.
The tokens concatenate because every distance a chunk emits points inside that chunk, so it reaches back only over bytes already written by the time the decoder gets there.
Args:
- data (
List[UInt8]): Bytes to compress. - max_chain (
Int): Hash-chain candidates the match search may walk. - max_lazy (
Int): Match length below which the search looks ahead. - max_workers (
Int): Cap on chunks; 0 asks the runtime.
Returns:
List[UInt8]: The compressed bytes, no zlib wrapper – decodable by any
RFC 1951 decoder, inflate included.
Raises: