New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make email.message.Message.__contains__ faster
#100792
Comments
I'd say it's one of the fastest linear searches you can do. What times do you get if you simply change the listcomp to a genexp, i.e., just change
Btw in "After," you switched the order of the two benchmarks, better use the same as in "Before". |
and
Nope: |
|
Hmm, I hoped the "from" search would be good. Odd that it's slower than the "missing" search in the map solution. |
Right now the implementation of
Message.__contains__looks like this:cpython/Lib/email/message.py
Lines 450 to 451 in 2f2fa03
There are several problems here:
listin this case)listforinoperation, which is slowThe fastest way to do check if actually have this item is simply by:
We do not create any intermediate lists / sets. And we even don't iterate longer than needed.
This change makes
incheck twice as fast.Microbenchmark
Before
After
The second case is now twice as fast.
It probably also consumes less memory now, but I don't think it is very significant.
Importance
Since
EmailMessage(a subclass ofMessage) is quite widely used by users and 3rd party libs, I think it is important to be included.And since the patch is quite simple and pure-python, I think the risks are very low.
Linked PRs
email.message.Message.__contains__twice as fast #100793The text was updated successfully, but these errors were encountered: