Skip to content

Commit

Permalink
Change RequestInfo to be a NamedTuple to improve performances (#9692
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bdraco authored Nov 7, 2024
1 parent dd0b6e3 commit e85db24
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
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

0 comments on commit e85db24

Please sign in to comment.