Repository navigation
Conversation
…C does C's ZopfliCalculateBitLengths always calls ZopfliLengthLimitedCodeLengths, which allocates one leaf per symbol. calculate_bit_lengths built an unlimited Huffman tree first and ran Katajainen's algorithm only when a length passed maxbits, so where several trees are optimal it could pick a different one, and dynamic blocks differed from C's.
This was referenced Oct 28, 2026
1c3t3a
approved these changes
Nov 20, 2026
1c3t3a
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the PR! We want C behavior!
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.
Compute Huffman code lengths with Katajainen's algorithm only, as C does
Problem
ZopfliCalculateBitLengths(C, Rust) gives each symbol its code length, at mostmaxbits(15 for the literal/length and distance codes, 7 for the code-length code). Every dynamic block's trees, and every block-size estimate, go through it. The two sides compute it differently:maxbits1. Different code lengths, different output
When a Huffman tree's lengths fit in
maxbits, both algorithms give optimal lengths for the symbols' counts. Often several sets of lengths are optimal, and the two algorithms can pick different ones. The trees then cost a different number of bits to write, and the block-size estimates that choose the block splits and block types can differ too. So the port's dynamic blocks differ from C's in bytes and sometimes in size.ZopfliGzipCompresswith the default options, on files from the repository:READMECOPYINGsrc/zopfli/deflate.csrc/zopfli/squeeze.csrc/zopfli/katajainen.cEvery stream decompresses to its file.
2. Different peak heap
For a literal/length code (288 symbols), C's leaves take 6,912 bytes and the port's Huffman nodes 18,432, whatever the counts. On a small block that is most of the heap:
ZopfliCalculateBlockSizeover an empty range withbtype = 2peaks at 6,912 bytes in C and 18,528 in the port.Question
Which behaviour do we want: C's or the Rust port's? Specifically:
ZopfliCalculateBitLengthsalways use Katajainen's algorithm (like the C source), or a plain Huffman tree whenever its lengths fit inmaxbits(like the Rust port)?This PR implements C's behaviour.
Fix
calculate_bit_lengthscallslength_limited_code_lengthsdirectly, asZopfliCalculateBitLengthscallsZopfliLengthLimitedCodeLengths.length_limited_code_lengthsreservesnleaves before it fills them, as C mallocsnleaves.The examples above then match C. Replayed before and after:
ZopfliGzipCompress, the five files aboveZopfliDeflate,btype = 3,README; both Rust columns with #212ZopfliCalculateBlockSize, empty range,btype = 2, peak heapThe peaks still differ, but within the harness's memory rule (it reports a divergence when the larger peak is at least 4 KiB and at least twice the smaller).
Notes for review:
LeafComparator, which returns asize_tdifference as anint, so it mis-sorts once a count reaches about 4.2 million (2^31 >> 9). The port sorts correctly.ZopfliDeflateworks in 1,000,000-byte master blocks, which keeps counts below that, so only a largerZopfliDeflatePartrange orZopfliCalculateBlockSizestore reaches it. Otherwise the two implementations gave the same lengths on 290,000 count sets (n = 19, 30, 32, 288;maxbits7 and 15; many ties).unlimited_code_lengthsis left in place: it is public, and nothing else in the crate calls it now.