Skip to content

Commit

Permalink
Improve performance of creating the ConnectionKey
Browse files Browse the repository at this point in the history
Reuses the same idea as aio-libs/yarl#1316 and aio-libs/yarl#1322

Calling `tuple.__new__` is much faster because it avoids the extra runtime lambda having to be run and arguments unpacked for every message
https://github.com/python/cpython/blob/d83fcf8371f2f33c7797bc8f5423a8bca8c46e5c/Lib/collections/__init__.py#L441

This only works if the object being created is a `NamedTuple` so this speed up is only recommended internally and should not be used outside of `aiohttp` since we do not guarantee that ConnectionKey will remain a `NamedTuple` in the future.
  • Loading branch information
bdraco committed Oct 18, 2024
1 parent da0099d commit 8e2c4ec
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,17 @@ def connection_key(self) -> ConnectionKey:
else:
h = None
url = self.url
return ConnectionKey(
url.raw_host or "",
url.port,
url.scheme in _SSL_SCHEMES,
self._ssl,
self.proxy,
self.proxy_auth,
h,
return tuple.__new__(
ConnectionKey,
(
url.raw_host or "",
url.port,
url.scheme in _SSL_SCHEMES,
self._ssl,
self.proxy,
self.proxy_auth,
h,
),
)

@property
Expand Down

0 comments on commit 8e2c4ec

Please sign in to comment.