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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/9169.misc.rst
6 changes: 4 additions & 2 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ContentCoding(enum.Enum):
identity = "identity"


CONTENT_CODINGS = {coding.value: coding for coding in ContentCoding}

############################################################
# HTTP Response classes
############################################################
Expand Down Expand Up @@ -341,8 +343,8 @@ async def _start_compression(self, request: "BaseRequest") -> None:
# Encoding comparisons should be case-insensitive
# https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
accept_encoding = request.headers.get(hdrs.ACCEPT_ENCODING, "").lower()
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

await self._do_start_compression(coding)
return

Expand Down
Loading