You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
email.header: folding a long header takes quadratic time #161284
email.header.Header.encode() builds each output line in the private _Accumulator list, whose __len__() adds up the lengths of all its parts on every call:
_ValueFormatter._append_chunk() calls len() after every word it appends (L514), so the cost of a line grows with the square of the number of words on it. With folding disabled the whole value is a single line. That is the default for Message.as_string() (maxheaderlen=0) with the default compat32 policy, so a header with many words, such as a long References: or To: header on a mailing list message, makes as_string() quadratic:
Header(value).encode(maxlinelen=0) behaves the same way. (macOS arm64, release build of main at 7e3290d, best of 3.)
This is a different code path from gh-160675 and gh-136063, which are about email._header_value_parser, used by the newer policies.
Proposed fix
Keep the total length up to date in push(), pop(), pop_from() and reset(), which are the only places the accumulator changes, and return it from __len__(). The output is unchanged. Ordinary messages get a little faster too, because len() is also called once per word when lines are folded at 78 characters.
Activity