Skip to content

Commit 586f968

Browse files
authored
Avoid quadratic copying when sending large HTTP/2 request bodies (#1127)
1 parent d6d7ec3 commit 586f968

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎src/httpcore2/httpcore2/_async/http2.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ async def _send_stream_data(self, request: Request, stream_id: int, data: bytes)
246246
"""
247247
Send a single chunk of data in one or more data frames.
248248
"""
249-
while data:
249+
position = 0
250+
while position < len(data):
250251
max_flow = await self._wait_for_outgoing_flow(request, stream_id)
251-
chunk_size = min(len(data), max_flow)
252-
chunk, data = data[:chunk_size], data[chunk_size:]
252+
chunk = data[position : position + max_flow]
253+
position += len(chunk)
253254
self._h2_state.send_data(stream_id, chunk)
254255
await self._write_outgoing_data(request)
255256

‎src/httpcore2/httpcore2/_sync/http2.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ def _send_stream_data(self, request: Request, stream_id: int, data: bytes) -> No
246246
"""
247247
Send a single chunk of data in one or more data frames.
248248
"""
249-
while data:
249+
position = 0
250+
while position < len(data):
250251
max_flow = self._wait_for_outgoing_flow(request, stream_id)
251-
chunk_size = min(len(data), max_flow)
252-
chunk, data = data[:chunk_size], data[chunk_size:]
252+
chunk = data[position : position + max_flow]
253+
position += len(chunk)
253254
self._h2_state.send_data(stream_id, chunk)
254255
self._write_outgoing_data(request)
255256

0 commit comments

Comments
 (0)