Performance issue: mailbox.mbox slow because it does a tell/lseek for every line · Issue #157509 · python/cpython · GitHub
Skip to content

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

Description

@dbratell

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions