Skip to content

Commit

Permalink
Improve performance of handling skip_auto_headers (#8847)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 26, 2024
1 parent 50a18d6 commit 6d97427
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES/8847.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of making requests when there are no auto headers to skip -- by :user:`bdraco`.
2 changes: 1 addition & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ async def _request(
url,
params=params,
headers=headers,
skip_auto_headers=skip_headers,
skip_auto_headers=skip_headers if skip_headers else None,
data=data,
cookies=all_cookies,
auth=auth,
Expand Down
24 changes: 14 additions & 10 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(
*,
params: Optional[Mapping[str, str]] = None,
headers: Optional[LooseHeaders] = None,
skip_auto_headers: Iterable[str] = frozenset(),
skip_auto_headers: Optional[Iterable[str]] = None,
data: Any = None,
cookies: Optional[LooseCookies] = None,
auth: Optional[BasicAuth] = None,
Expand Down Expand Up @@ -381,12 +381,18 @@ def update_headers(self, headers: Optional[LooseHeaders]) -> None:
else:
self.headers.add(key, value)

def update_auto_headers(self, skip_auto_headers: Iterable[str]) -> None:
self.skip_auto_headers = CIMultiDict(
(hdr, None) for hdr in sorted(skip_auto_headers)
)
used_headers = self.headers.copy()
used_headers.extend(self.skip_auto_headers) # type: ignore[arg-type]
def update_auto_headers(self, skip_auto_headers: Optional[Iterable[str]]) -> None:
if skip_auto_headers is not None:
self.skip_auto_headers = CIMultiDict(
(hdr, None) for hdr in sorted(skip_auto_headers)
)
used_headers = self.headers.copy()
used_headers.extend(self.skip_auto_headers) # type: ignore[arg-type]
else:
# Fast path when there are no headers to skip
# which is the most common case.
self.skip_auto_headers = CIMultiDict()
used_headers = self.headers

for hdr, val in self.DEFAULT_HEADERS.items():
if hdr not in used_headers:
Expand Down Expand Up @@ -508,9 +514,7 @@ def update_body_from_data(self, body: Any) -> None:
# copy payload headers
assert body.headers
for key, value in body.headers.items():
if key in self.headers:
continue
if key in self.skip_auto_headers:
if key in self.headers or key in self.skip_auto_headers:
continue
self.headers[key] = value

Expand Down

0 comments on commit 6d97427

Please sign in to comment.