Skip to content

Commit 5b5eb8b

Browse files
authored
small speedup by avoiding redundant IOBasePayload.size calls (#11335)
1 parent b7ffe61 commit 5b5eb8b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

aiohttp/multipart.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,15 @@ def size(self) -> Optional[int]:
980980
"""Size of the payload."""
981981
total = 0
982982
for part, encoding, te_encoding in self._parts:
983-
if encoding or te_encoding or part.size is None:
983+
part_size = part.size
984+
if encoding or te_encoding or part_size is None:
984985
return None
985986

986987
total += int(
987988
2
988989
+ len(self._boundary)
989990
+ 2
990-
+ part.size # b'--'+self._boundary+b'\r\n'
991+
+ part_size # b'--'+self._boundary+b'\r\n'
991992
+ len(part._binary_headers)
992993
+ 2 # b'\r\n'
993994
)

aiohttp/web_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,8 @@ async def _start(self, request: "BaseRequest") -> AbstractStreamWriter:
690690
del self._headers[hdrs.CONTENT_LENGTH]
691691
elif not self._chunked:
692692
if isinstance(self._body, Payload):
693-
if self._body.size is not None:
694-
self._headers[hdrs.CONTENT_LENGTH] = str(self._body.size)
693+
if (size := self._body.size) is not None:
694+
self._headers[hdrs.CONTENT_LENGTH] = str(size)
695695
else:
696696
body_len = len(self._body) if self._body else "0"
697697
# https://www.rfc-editor.org/rfc/rfc9110.html#section-8.6-7

0 commit comments

Comments
 (0)