python / python/cpython

Performance issue: mailbox.mbox slow because it does a tell/lseek for every line

Offen
#157,509 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug report

Bug description:

Parsing a large mbox file with mailbox.mbox is surprisingly slow and I did some profiling and debugging. For a large file (my testcase, 3,000 mails, 26,000,000 lines), somewhere around half the time is spent calling tell(), once per line in PartialFile. As documented in issue #107158, tell() here uses lseek which is a system call rather than just returning a constant.


# Slow code, assuming a large my_mail.mbox
import mailbox
box = mailbox.mbox("my_mail.mbox", create=False)
for message in box:
    pass

Replacing mailbox.mbox with a simple mmapped file that scans for b"\nFrom " and calls BytesParser(policy=policy.default).parsebytes(...) directly gives the same results in my testing (I'm sure it's not as powerful). In my tests that cuts the parsing overhead from 11 seconds to 1, and total parse time from >20 to 9.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-157543

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

Beginne mit dem Iterationspfad von mailbox.mbox und seiner PartialFile-Implementierung und konzentriere dich dabei auf das hier beschriebene Verhalten von tell() und lseek() pro Zeile. Sieh dir zuerst den verknüpften PR gh-157543 an, führe anschließend Benchmarks für das Parsen einer großen mbox-Datei durch und bestätige, dass das Performance-Problem behoben ist, ohne die Parsergebnisse zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
performance
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

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