Skip to content

gh-156379: Avoid materializing large sequence ranges in mailbox.MH.get_sequences - #156442

Open
sundeep8967 wants to merge 1 commit into
python:mainfrom
sundeep8967:fix-mh-sequence-ranges
Open

sundeep8967 wants to merge 1 commit into
python:mainfrom
sundeep8967:fix-mh-sequence-ranges

Conversation

@sundeep8967

Copy link
Copy Markdown
Contributor

Summary

In mailbox.MH.get_sequences(), sequence ranges declared in .mh_sequences (e.g. 1-10000000) previously materialized every single integer in the interval (keys.update(range(start, stop + 1))) before filtering against all_keys. For small mailboxes with wide sequence range definitions, this resulted in unnecessary memory allocation and CPU overhead.

This PR optimizes get_sequences() to intersect declared ranges directly with existing mailbox keys (all_keys), reducing execution time by orders of magnitude (~9000x on 10M ranges) and eliminating redundant memory allocation while preserving exact ordering and format validation semantics.

Issue reference

Fixes gh-156379

….MH.get_sequences

Intersect declared ranges directly with all_keys instead of enumerating the entire interval into a set.
@sundeep8967
sundeep8967 force-pushed the fix-mh-sequence-ranges branch from 871539b to c03c752 Compare September 5, 2026 09:41
@eendebakpt

Copy link
Copy Markdown
Contributor

There is a performance regression with this approach. A reproducer:

import mailbox, os, sys, tempfile, time

N = int(sys.argv[1]) if len(sys.argv) > 1 else 10_000

with tempfile.TemporaryDirectory() as tmp:
    box = mailbox.MH(os.path.join(tmp, 'box'))
    for key in range(1, N + 1):
        with open(os.path.join(box._path, str(key)), 'w') as f:
            f.write('Subject: x\n\nbody\n')
    box.set_sequences({'unseen': [k for k in range(1, N + 1) if k % 3]})

    t0 = time.perf_counter()
    sequences = box.get_sequences()
    elapsed = time.perf_counter() - t0
    box.close()

assert len(sequences['unseen']) == N - N // 3
print(f'{N} messages, {N // 3} ranges: get_sequences() took {elapsed * 1e3:.1f} ms')

For N=10_000 the PR is a factor 50 slower.

Is the issue something that happens in real-world applications?

Comment thread Lib/mailbox.py
Comment on lines +1244 to +1245
if start <= stop:
keys.update(k for k in all_keys if start <= k <= stop)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if start <= stop:
keys.update(k for k in all_keys if start <= k <= stop)
lo = bisect.bisect_left(sorted_keys, start)
hi = bisect.bisect_right(sorted_keys, stop)
keys.update(sorted_keys[lo:hi])

with sorted_keys set to sorted_keys = self.keys(). This avoids many start <= k <= stop checks.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mailbox.MH.get_sequences() unnecessarily materializes large sequence ranges

2 participants