Skip to content
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

Avoid hasattr in hot loop in brotli decoder #1858

Merged
merged 1 commit into from Apr 19, 2020

Conversation

auscompgeek
Copy link
Contributor

hasattr has a nasty performance penalty when it returns false: in the background it is actually a try/except AttributeError. Since we know the method we want won't change, by instead caching the bound method object we can amortise the cost across an entire encoded body.

This removes the intermediate decompress method altogether, instead giving the underlying method the decompress name.

@codecov
Copy link

codecov bot commented Apr 19, 2020

Codecov Report

Merging #1858 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1858   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2013      2012    -1     
=========================================
- Hits          2013      2012    -1     
Flag Coverage Δ
#unittests 99.60% <50.00%> (-0.01%) ⬇️
Impacted Files Coverage Δ
src/urllib3/response.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1f0825b...cedf5cf. Read the comment docs.

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

While the change makes sense, can you please share the speed improvement you're seeing?

@auscompgeek
Copy link
Contributor Author

I haven't had a chance to actually benchmark this yet. I might get an opportunity to do so later this week, but no guarantees.

Copy link
Member

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you! Would still like to see the performance difference if possible.

@sethmlarson sethmlarson merged commit 8fadde1 into urllib3:master Apr 19, 2020
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.

None yet

3 participants