Reading — step 1 of 5
Read
~1 min readDictionary Coding (LZ77)
Hash Chain for Faster Match Finding
Naive longest-match search is O(window_size) per position — too slow for large windows. Real LZ77 encoders use a hash chain:
- Build a hash of the next 3 bytes at each position:
hash(text[i], text[i+1], text[i+2]). - Maintain a hash table:
table[hash] = list_of_positions_with_that_3-byte-prefix. - To find a match at position p: hash the 3-byte prefix, look up the table, walk the list of candidates checking each.
python
The chain_limit is the speed/compression tradeoff knob. gzip's -1 to -9 levels mostly differ in this limit.
zstd uses a more sophisticated structure (suffix array / radix tree) for its higher levels. Snappy and lz4 use an even simpler one-position-per-hash table — fast but lower compression.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…