-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143542: Speed up decode_header by 1.02x #143576
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
Conversation
Avoid doing `list.pop(0)` in a loop, and use indexes instead. pyperf reports: ``` Mean +- std dev: [decode_header_baseline] 6.21 us +- 0.05 us -> [decode_header_optimized] 6.11 us +- 0.06 us: 1.02x faster ```
hauntsaninja
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you benchmark a version where we do parts.reverse() up front and then parts.pop()?
That would preserve some of the readability of the original — introducing more state with an index makes the code meaningfully more complicated for me
| charset = parts[i].lower() | ||
| i += 1 | ||
| encoding = parts[i].lower() | ||
| i += 1 | ||
| encoded = parts[i] | ||
| i += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may get out of range. You need a check after each i += 1. Because of that I'm not really interested in a 2% speed-up. Usually, we are interested in 10% speed-up when it comes down to a microbenchmark. In addition, your benchmark includes the import time as it's not part of the setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code has the same issue, 3 pop(0) calls in succession without checking if any elements remain. So my code retains the same behavior.
I'll create a proper benchmark.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I was wrong. The split would always have 3 parts because of the regex.
picnixz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced by the change. This is only a 2% increase for a micro-benchmark which also doesn't isolate the import statement (and thus is counted in the overall time).
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I initially considered changing the logic to process the list from the end to start, but decided against that because then I would need to reorder everything inside the loop, which in my opinion would be even harder to reason about. |
|
With the attached |
|
I'm still not convinced even with 3% improvements. Usually:
In addition, microbenchmarks for this specific function aren't really useful IMO. We should see how if E2E examples would benefit from it. In addition your benchmarks time 3 function calls and not just one (you're evaluating 3 times the function), so there is still noise. I am still -1 on this one also because adding indices reduces readability.
I'm not sure I follow. With reverse(), all |
|
Modifying the benchmark file to just keep the test for middle one: You are right, If you feel this is not significant enough, feel free to close. Or let me know if you want me to modify the PR for |
We don't always select the fast approach because sometimes readability counts as much as performance. If we lose readability at the cost of a 1-3% speed-up in microbenchmarks, we usually don't bother about the change. Thanks for the suggestion but I'm closing the PR. |
Avoid doing
list.pop(0)in a loop, and use indexes instead.pyperf report: