gh-143542: Speed up decode_header by 1.02x#143576
gh-143542: Speed up decode_header by 1.02x#143576heikkitoivonen wants to merge 1 commit intopython:mainfrom
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.
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.
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.
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.
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.
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: