Skip to content

Commit a741c6d

Browse files
committed
Remove consumed frames in place
Deleting the consumed prefix preserves the bytearray's amortized left-delete behavior instead of copying the entire remaining buffer after every frame. Closes #474
1 parent 6cce763 commit a741c6d

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/h2/frame_buffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def __next__(self) -> Frame:
155155

156156
# At this point, as we know we'll use or discard the entire frame, we
157157
# can update the data.
158-
self._data = self._data[9+length:]
158+
del self._data[:9+length]
159159

160160
# Pass the frame through the header buffer.
161161
new_frame = self._update_header_buffer(f)

tests/test_basic_logic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
from . import helpers
2424

2525

26+
class TestFrameBuffer:
27+
def test_consumed_frames_are_removed_in_place(self) -> None:
28+
frame = hyperframe.frame.SettingsFrame(0).serialize()
29+
buffer = h2.frame_buffer.FrameBuffer()
30+
buffer.max_frame_size = 65535
31+
buffer.add_data(frame * 2)
32+
data = buffer._data
33+
34+
next(buffer)
35+
36+
assert buffer._data is data
37+
assert buffer._data == frame
38+
39+
2640
class TestBasicClient:
2741
"""
2842
Basic client-side tests.

0 commit comments

Comments
 (0)