Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Avoid constructing headers mulitidict twice for web.Response #10043

Merged
merged 2 commits into from
Nov 25, 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/10043.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of constructing :class:`aiohttp.web.Response` with headers -- by :user:`bdraco`.
14 changes: 12 additions & 2 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ def __init__(
status: int = 200,
reason: Optional[str] = None,
headers: Optional[LooseHeaders] = None,
_real_headers: Optional[CIMultiDict[str]] = None,
) -> None:
"""Initialize a new stream response object.

_real_headers is an internal parameter used to pass a pre-populated
headers object. It is used by the `Response` class to avoid copying
the headers when creating a new response object. It is not intended
to be used by external code.
"""
super().__init__()
self._length_check = True
self._body = None
Expand All @@ -122,7 +130,9 @@ def __init__(
self._body_length = 0
self._state: Dict[str, Any] = {}

if headers is not None:
if _real_headers is not None:
self._headers = _real_headers
elif headers is not None:
self._headers: CIMultiDict[str] = CIMultiDict(headers)
else:
self._headers = CIMultiDict()
Expand Down Expand Up @@ -581,7 +591,7 @@ def __init__(
content_type += "; charset=" + charset
real_headers[hdrs.CONTENT_TYPE] = content_type

super().__init__(status=status, reason=reason, headers=real_headers)
super().__init__(status=status, reason=reason, _real_headers=real_headers)

if text is not None:
self.text = text
Expand Down
Loading