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

Speed up starting compression #9169

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Speed up starting compression #9169

wants to merge 4 commits into from

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Sep 17, 2024

What do these changes do?

Enumerating a enum and accessing all the .value is not performant. Switching to a pre-built dict is significantly faster

Are there changes in behavior for the user?

no

Is it a substantial burden for the maintainers to support this?

no

related issue #2779

before
start_compression_before

after
start_compression_after

Enumerating a enum and accessing all the .value is
not performant. Switching to a dict is significantly
faster
@bdraco bdraco added backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot labels Sep 17, 2024
Copy link

codecov bot commented Sep 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.24%. Comparing base (98b363e) to head (03afd40).

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9169      +/-   ##
==========================================
- Coverage   98.31%   98.24%   -0.07%     
==========================================
  Files         107      107              
  Lines       34485    34486       +1     
  Branches     4095     4096       +1     
==========================================
- Hits        33903    33882      -21     
- Misses        411      429      +18     
- Partials      171      175       +4     
Flag Coverage Δ
CI-GHA 98.15% <100.00%> (-0.06%) ⬇️
OS-Linux 97.81% <100.00%> (-0.06%) ⬇️
OS-Windows 96.28% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.54% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.64% <100.00%> (+<0.01%) ⬆️
Py-3.10.14 97.49% <100.00%> (-0.09%) ⬇️
Py-3.10.15 97.39% <100.00%> (?)
Py-3.11.9 97.81% <100.00%> (+<0.01%) ⬆️
Py-3.12.5 97.58% <100.00%> (+<0.01%) ⬆️
Py-3.12.6 97.64% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 97.53% <100.00%> (+<0.01%) ⬆️
Py-3.9.19 97.47% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 ?
VM-macos 97.54% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 97.81% <100.00%> (-0.06%) ⬇️
VM-windows 96.28% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Sep 18, 2024
@bdraco bdraco marked this pull request as ready for review September 18, 2024 05:51
for coding in ContentCoding:
if coding.value in accept_encoding:
for value, coding in CONTENT_CODINGS.items():
if value in accept_encoding:
Copy link
Member

Choose a reason for hiding this comment

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

I'm struggling with this one. It looks to me like all we've saved is one attribute access...

With the dict, I was expecting a reversal of logic or something here, like for enc in accept_encoding: CONTENT_CODINGS.get(enc).

Copy link
Member Author

@bdraco bdraco Sep 18, 2024

Choose a reason for hiding this comment

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

Iterating enums in python is surprisingly expensive and good chunk of its the __get__. _start_compression was the most expensive call in _prepare_headers before:

We save:

  • creating the iterator for ContentCoding enum (although it might be an even exchange to iterate .items(), but thats all going to be in native code for sure)
  • creating a enum singleton for each one in the loop
  • accessing the value which does some magic under the hood (its not a simple attribute)

... maybe we can save more here but I was thinking the in 3x was cheaper than a dict lookup

Zoomed in on _prepare_headers before the change:
prepare_headers_zoomed

Zoomed in on _start_compression before the change:
start_compression_zoomed

Copy link
Member Author

Choose a reason for hiding this comment

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

a dict get might make sense if we parsed the string but parsing is probably more expensive Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.10 Trigger automatic backporting to the 3.10 release branch by Patchback robot backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants