Repository navigation
[3.14] gh-160310: Fix quadratic one-shot decompress() with many members (GH-160311) - #161038
Merged
Merged
Conversation
…ny members (pythonGH-160311) gzip.decompress(), bz2.decompress(), lzma.decompress() and compression.zstd.decompress() handed each new decompressor all of the remaining input and copied the rest back through unused_data, so input made of many concatenated members took quadratic time. Each member is now fed in chunks that start at 1 KiB and double, and the loop steps back over unused_data by offset. The first chunk is the whole input, so single-member data still takes one decompressor call. bz2, lzma and zstd share the loop through a private helper in compression._common._streams; gzip keeps its own because it parses the member header, trailer and padding itself. ANT-2026-9R8SO5S2 (cherry picked from commit e80962a)
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.
gzip.decompress(), bz2.decompress(), lzma.decompress() and
compression.zstd.decompress() handed each new decompressor all of the
remaining input and copied the rest back through unused_data, so input
made of many concatenated members took quadratic time. Each member is
now fed in chunks that start at 1 KiB and double, and the loop steps
back over unused_data by offset. The first chunk is the whole input,
so single-member data still takes one decompressor call.
bz2, lzma and zstd share the loop through a private helper in
compression._common._streams; gzip keeps its own because it parses the
member header, trailer and padding itself.
ANT-2026-9R8SO5S2
(cherry picked from commit e80962a)
Manual backport notes (3.14): the cherry-pick conflicted in Lib/bz2.py, Lib/compression/zstd/init.py and Lib/test/test_gzip.py, only on surrounding context that exists on main but not on 3.14: the module-level getattr at the end of bz2.py, the extra names main imports from _zstd, and the test_truncated_header and test_corrupted_gzip_header tests next to the new gzip tests. Each conflict was resolved by keeping 3.14's own code and adding only what the original commit adds or changes. The rest applied cleanly, and test_gzip, test_bz2, test_lzma, test_zstd, test_tarfile, test_shutil, test_zipfile and test_zipimport pass.
decompress()ingzip,bz2,lzmaandcompression.zstdis quadratic in the number of members #160310