Skip to content

Commit b6cbb61

Browse files
[PR #11335/5b5eb8bc backport][3.13] small speedup by avoiding redundant IOBasePayload.size calls (#11339)
**This is a backport of PR #11335 as merged into master (5b5eb8b).** Co-authored-by: Cycloctane <[email protected]>
1 parent 63c4d0d commit b6cbb61

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
@@ -976,14 +976,15 @@ def size(self) -> Optional[int]:
976976
"""Size of the payload."""
977977
total = 0
978978
for part, encoding, te_encoding in self._parts:
979-
if encoding or te_encoding or part.size is None:
979+
part_size = part.size
980+
if encoding or te_encoding or part_size is None:
980981
return None
981982

982983
total += int(
983984
2
984985
+ len(self._boundary)
985986
+ 2
986-
+ part.size # b'--'+self._boundary+b'\r\n'
987+
+ part_size # b'--'+self._boundary+b'\r\n'
987988
+ len(part._binary_headers)
988989
+ 2 # b'\r\n'
989990
)

aiohttp/web_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,8 @@ async def _start(self, request: "BaseRequest") -> AbstractStreamWriter:
791791
del self._headers[hdrs.CONTENT_LENGTH]
792792
elif not self._chunked:
793793
if isinstance(self._body, Payload):
794-
if self._body.size is not None:
795-
self._headers[hdrs.CONTENT_LENGTH] = str(self._body.size)
794+
if (size := self._body.size) is not None:
795+
self._headers[hdrs.CONTENT_LENGTH] = str(size)
796796
else:
797797
body_len = len(self._body) if self._body else "0"
798798
# https://www.rfc-editor.org/rfc/rfc9110.html#section-8.6-7

0 commit comments

Comments
 (0)