perf: improve compression speed and ratio in lzsst2_file_compression.pl#5
Closed
perf: improve compression speed and ratio in lzsst2_file_compression.pl#5
Conversation
…ression.pl Co-authored-by: trizen <614513+trizen@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Improve runtime performance and compression ratio in lzsst2
perf: improve compression speed and ratio in lzsst2_file_compression.pl
Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LZ77+Huffman compressor had O(n²log n) Huffman tree construction, no early exit in match search, redundant lazy-match probes, and a suboptimal chunk size and chain length.
LZ77 (
find_match/lz77_compression)last if ($best_n > $max_len)in match loop — no point scanning further once a 258-byte match is foundla+1when a match was already found atla($n1 > 1)max_chain_len: 48 → 64Huffman (
mktree_from_freq)Replaced O(n²log n) full re-sort loop with O(n log n) insertion-sort using binary search on the already-sorted remainder:
Single-symbol frequency tables are explicitly wrapped so the symbol receives code
'0'(≥1 bit), matching original behavior and preventing a zero-length encoding that breaks decompression.find_distance_indexLinear scan → binary search (O(n) → O(log n)).
CHUNK_SIZE1 << 19→1 << 21(512 KB → 2 MB) for a larger LZ77 window and better compression ratio.Original prompt
Objective
Improve both the runtime performance and compression ratio of
Compression/lzsst2_file_compression.pl, which implements LZ77 compression (LZSS variant with hash tables and lazy matching) + Huffman coding, using a DEFLATE-like approach.The changes must preserve full backward-compatible decompression — i.e., archives produced by the new code must still decompress correctly, and the decompressor should remain unchanged in behavior.
File to modify
Compression/lzsst2_file_compression.plCurrent bottlenecks and areas for improvement
After careful analysis, these are the specific improvements to make:
1. LZ77 Compression — Performance (
lz77_compressionandfind_match)a) Early termination in
find_matchwhen max_len is reached:In
find_match, the innerforeachloop iterates over all positions in the chain even after finding a maximum-length match (258). Add an early exit:b) Skip updating the hash table for positions already covered by a long match:
Currently, every position inside a match is inserted into the hash table. For very long matches this is wasteful. When
$best_nis large (e.g., > 32), only insert the first few and last few positions into the hash table instead of all of them, to save time without significantly affecting compression ratio:c) Avoid redundant
find_matchcall atla+1when no match exists atla:The lazy matching currently always tries to find a match at
la+1even when it's unlikely to help. Only do the secondfind_matchcall when the first match was found (i.e.,$n1 > 1):2. Increase default CHUNK_SIZE for better compression ratio
The current
CHUNK_SIZEis1 << 19(512 KB). Increase it to1 << 21(2 MB). Larger chunks give the LZ77 engine a bigger window to find matches, improving compression ratio at the cost of slightly more memory (which is acceptable for modern systems). The script already says "higher value = better compression":3.
find_distance_index— Use binary search instead of linear scanThe
find_distance_indexfunction uses a linear scan through@DISTANCE_SYMBOLSto find the right bucket for a distance value. Replace this with a binary search for O(log n) lookup:4. Huffman tree construction — avoid repeated full sorts
In
mktree_from_freq, the "poor man's priority queue" re-sorts the full@nodesarray on every iteration. This is O(n² log n). Instead, use an insertion-sort approach: after splicing the two smallest nodes and creating a merged node, insert the new node into the already-sorted remainder at the correct position using binary search:5. Increase `max_cha...
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.