Skip to content

Commit 8922ab8

Browse files
bdracoOk3ks
andauthored
[PR #9745/0d10447 backport][3.11] Speed up preparing headers by avoiding populating cookies when there are no cookies (#9749)
Co-authored-by: Emmanuel Okedele <[email protected]> Co-authored-by: pre-commit-ci[bot] Co-authored-by: J. Nick Koston <[email protected]> Co-authored-by: Emmanuel Okedele <[email protected]>
1 parent 8482975 commit 8922ab8

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

aiohttp/web_response.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,10 @@ async def _prepare_headers(self) -> None:
465465
version = request.version
466466

467467
headers = self._headers
468-
for cookie in self._cookies.values():
469-
value = cookie.output(header="")[1:]
470-
headers.add(hdrs.SET_COOKIE, value)
468+
if self._cookies:
469+
for cookie in self._cookies.values():
470+
value = cookie.output(header="")[1:]
471+
headers.add(hdrs.SET_COOKIE, value)
471472

472473
if self._compression:
473474
await self._start_compression(request)
@@ -515,9 +516,8 @@ async def _prepare_headers(self) -> None:
515516
if keep_alive:
516517
if version == HttpVersion10:
517518
headers[hdrs.CONNECTION] = "keep-alive"
518-
else:
519-
if version == HttpVersion11:
520-
headers[hdrs.CONNECTION] = "close"
519+
elif version == HttpVersion11:
520+
headers[hdrs.CONNECTION] = "close"
521521

522522
async def _write_headers(self) -> None:
523523
request = self._req

tests/test_imports.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def test_web___all__(pytester: pytest.Pytester) -> None:
4949
not sys.platform.startswith("linux") or platform.python_implementation() == "PyPy",
5050
reason="Timing is more reliable on Linux",
5151
)
52+
@pytest.mark.dev_mode
5253
def test_import_time(pytester: pytest.Pytester) -> None:
5354
"""Check that importing aiohttp doesn't take too long.
5455

0 commit comments

Comments
 (0)