Repository navigation
Conversation
C's ZopfliBlockSplit, ZopfliLZ77Optimal and ZopfliLZ77OptimalFixed allocate the hash (about 960 KB) before they look at the block, so an empty block holds it too. lz77_greedy and get_best_lengths returned for an empty block before allocating theirs.
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.
Allocate the hash for an empty block, as C does
Problem
C allocates the LZ77 hash once per block, before it looks at the block.
ZopfliLZ77Optimal(C),ZopfliLZ77OptimalFixed(C) andZopfliBlockSplit(C) each callZopfliAllocHash(fixed, block split), which allocates seven arrays, about 960 KB, and only then doesGetBestLengthsreturn for an empty block.The port makes its hash inside
lz77_greedy(Rust) andget_best_lengths(Rust), after they return for an empty block (get_best_lengths). So compressing an empty input makes no hash in the port. The output is the same; the peak heap is not. For example, with an empty input (peak heap in bytes):ZopfliCompress, gzip formatZopfliZlibCompressZopfliDeflate,btype = 1ZopfliDeflatePart,btype = 1,instart = inendQuestion
Which behaviour do we want: C's or the Rust port's? Specifically, for an empty block, should the hash be allocated anyway (like the C source), or not at all (like the Rust port)?
This PR implements C's behaviour.
Fix
lz77_greedyandget_best_lengthsmake their hash before the empty-block return (lz77_greedy,get_best_lengths). The two lines move; nothing else changes:The examples above then hold what C holds. Replayed before and after (x86-64), peak heap in bytes:
ZopfliCompress, gzip formatZopfliZlibCompressZopfliDeflate,btype = 1ZopfliDeflatePart,btype = 1,instart = inendDownstream check. The output does not change:
ZopfliGzipCompresson the repository'sREADMEgives the same 831-byte stream from the port before and after this patch, at the same peak heap, andgzip.decompressrestores the file.Notes for review:
ZopfliBlockSplitfor the empty block, freed beforeZopfliLZ77Optimalmakes its own. The port'ssplitreturns beforelz77_greedyfor an empty range, so with this PR it makes one hash where C makes two in turn. The peak is the same, since C holds only one at a time.memory.peakdivergences.