python / python/cpython

`zipimport._zstd_decompress()` repeatedly processes remaining input for concatenated frames

Offen
#155,152 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Der Einstiegspunkt ist zipimport._zstd_decompress(); beginne damit, den bereitgestellten Reproducer für verkettete Frames auszuführen und die Eingabe zu untersuchen, die jeder Dekompressor-Aufruf erhält. Fertig ist die Arbeit, wenn die Ausgabe korrekt bleibt, während die kumulativ übergebene Eingabe linear ist, wie beim gemeldeten Ergebnis von 1.00x. Der verlinkte PR gh-155154 zeigt, dass die Arbeit bereits im Gange ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.