Skip to content

gh-159739: Avoid quadratic grouped-address parsing - #159990

Merged
JelleZijlstra merged 1 commit into
python:mainfrom
jelle-openai:jelle/codex/gh-159739-email-addresslist-growth
Oct 21, 2026
Merged

JelleZijlstra merged 1 commit into
python:mainfrom
jelle-openai:jelle/codex/gh-159739-email-addresslist-growth

Conversation

@jelle-openai

@jelle-openai jelle-openai commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Avoid quadratic grouped-address parsing. email.utils.getaddresses() parses a group by repeatedly concatenating the accumulated address list with each newly parsed member. Each concatenation copies all prior entries, so a group with many members takes quadratic time and can consume excessive CPU. Extending the list in place avoids that repeated copying.

Backport to 3.15 and 3.14.

@jelle-openai
jelle-openai requested a review from a team as a code owner October 20, 2026 23:23
@JelleZijlstra

Copy link
Copy Markdown
Member

Not going to backport this as it's a performance improvement.

@JelleZijlstra
JelleZijlstra enabled auto-merge (squash) October 21, 2026 18:46
@JelleZijlstra
JelleZijlstra merged commit 080fc54 into python:main Oct 21, 2026
104 of 106 checks passed
seanthegeek added a commit to seanthegeek/mailsuite that referenced this pull request Nov 16, 2026
The 2.3.5 changelog entry and the _without_address_groups() docstring
described the slow parse of an RFC 5322 address group without saying
where the time goes. It is CPython's email.utils.getaddresses(): its
group loop in Lib/email/_parseaddr.py built a new list for every member
(returnlist = returnlist + self.getaddress()), so the time grows with
the square of the member count.

CPython changed that line to an in-place extend for Python 3.16 in
python/cpython#159990 (merged 2026-10-21). The
author's comment there says the fix will not be backported, and the
3.10 through 3.15 branches still had the old line on 2026-11-16, so
mailsuite's own bound on long address headers stays needed.

Documentation only; no behaviour change.

Co-authored-by: Sean Whalen <seanthegeek@users.noreply.github.com>
Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants