python / python/cpython

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

オープン
#157,509 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

mailbox.mbox の反復パスとその PartialFile 実装から始め、ここで説明されている行ごとの tell() と lseek() の動作に注目してください。まずリンクされている PR gh-157543 を確認し、その後、大きな mbox ファイルのパースをベンチマークして、パース結果を変更せずにパフォーマンスの問題が解消されていることを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
performance
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
20/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。