`zipimport._zstd_decompress()` repeatedly processes remaining input for concatenated frames
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug description
zipimport._zstd_decompress() handles concatenated Zstandard frames by creating a new _zstd.ZstdDecompressor for each frame. Each decompressor is passed the entire remaining compressed suffix, and the next iteration obtains the same suffix, minus the frame just decoded, through unused_data.
As a result, when a zstd-compressed ZIP entry using compression method 93 contains many concatenated frames, most of the compressed input is passed to decompressor calls repeatedly. For N similarly sized frames, the cumulative amount of input handled across those calls grows quadratically rather than linearly with the size of the entry.
Reproducer and local results
import time
import zipimport
from compression import zstd
FRAMES = 64_000
frame = zstd.compress(b"x")
data = frame * FRAMES
input_sizes = []
original = zipimport._get_zstd_decompressor_class()
class RecordingDecompressor:
def __init__(self, *args, **kwargs):
self._decompressor = original(*args, **kwargs)
def __getattr__(self, name):
return getattr(self._decompressor, name)
def decompress(self, data, max_length=-1):
input_sizes.append(memoryview(data).nbytes)
return self._decompressor.decompress(data, max_length)
zipimport._zstd_decompressor_class = RecordingDecompressor
start = time.perf_counter()
try:
output = zipimport._zstd_decompress(data)
finally:
elapsed = time.perf_counter() - start
zipimport._zstd_decompressor_class = original
print(f"input size: {len(data):,} bytes")
print(f"output size: {len(output):,} bytes")
print(f"decompress calls: {len(input_sizes):,}")
print(f"cumulative input passed: {sum(input_sizes):,} bytes")
print(f"amplification: {sum(input_sizes) / len(data):,.2f}x")
print(f"elapsed: {elapsed:.4f} seconds")
Local result:
input size: 640,000 bytes
output size: 64,000 bytes
decompress calls: 64,000
cumulative input passed: 20,480,320,000 bytes
amplification: 32,000.50x
elapsed: 1.5364 seconds
After fix:
input size: 640,000 bytes
output size: 64,000 bytes
decompress calls: 64,000
cumulative input passed: 640,000 bytes
amplification: 1.00x
elapsed: 0.2333 seconds
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-155154
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Điểm vào là zipimport._zstd_decompress(); trước tiên hãy chạy trình tái hiện được cung cấp cho các frame nối tiếp và kiểm tra đầu vào mà mỗi lần gọi bộ giải nén nhận được. Được xem là hoàn tất khi đầu ra vẫn chính xác trong khi tổng đầu vào được truyền có mức tăng tuyến tính, như trong kết quả 1.00x đã báo cáo. PR được liên kết gh-155154 cho biết công việc đã được tiến hành.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100