python / python/cpython

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

未关闭
#157,509 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 mailbox.mbox 的迭代路径及其 PartialFile 实现开始,重点关注此处所述的逐行 tell() 和 lseek() 行为。先查看链接的 PR gh-157543,然后对解析大型 mbox 文件进行基准测试,并确认在不改变解析结果的情况下性能问题已得到解决。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
performance
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。