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

Change RequestInfo to be a NamedTuple to improve performances #9692

Merged
merged 5 commits into from
Nov 7, 2024
Merged
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/9692.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``ClientRequest.request_info`` to be a `NamedTuple` to improve client performance -- by :user:`bdraco`.
9 changes: 6 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ class ContentDisposition:
filename: Optional[str]


@dataclasses.dataclass(frozen=True)
class RequestInfo:
class RequestInfo(NamedTuple):
url: URL
method: str
headers: "CIMultiDictProxy[str]"
Expand Down Expand Up @@ -330,7 +329,11 @@ def port(self) -> Optional[int]:
@property
def request_info(self) -> RequestInfo:
headers: CIMultiDictProxy[str] = CIMultiDictProxy(self.headers)
return RequestInfo(self.url, self.method, headers, self.original_url)
# These are created on every request, so we use a NamedTuple
# for performance reasons.
return tuple.__new__(
RequestInfo, (self.url, self.method, headers, self.original_url)
)

def update_host(self, url: URL) -> None:
"""Update destination host, port and connection type (ssl)."""
Expand Down
4 changes: 2 additions & 2 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ Response object

.. attribute:: request_info

A namedtuple with request URL and headers from :class:`~aiohttp.ClientRequest`
A :class:`typing.NamedTuple` with request URL and headers from :class:`~aiohttp.ClientRequest`
object, :class:`aiohttp.RequestInfo` instance.

.. method:: get_encoding()
Expand Down Expand Up @@ -1824,7 +1824,7 @@ Utilities

.. class:: RequestInfo()

A data class with request URL and headers from :class:`~aiohttp.ClientRequest`
A :class:`typing.NamedTuple` with request URL and headers from :class:`~aiohttp.ClientRequest`
object, available as :attr:`ClientResponse.request_info` attribute.

.. attribute:: url
Expand Down
Loading