File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -155,6 +155,13 @@ 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+ # Deleting the consumed prefix mutates the bytearray in place instead
159+ # of copying the remaining bytes into a new object, as slicing would.
160+ # ``del s[i:j]`` is documented for mutable sequences in
161+ # https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
162+ # and CPython's bytearray tracks an internal offset (``ob_start`` in
163+ # Objects/bytearrayobject.c) that makes repeated deletes from the
164+ # front amortized O(1) per byte rather than O(len) per frame.
158165 del self ._data [:9 + length ]
159166
160167 # Pass the frame through the header buffer.
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ def test_consumed_frames_are_removed_in_place(self) -> None:
3333
3434 next (buffer )
3535
36+ # ``is`` checks object identity (CPython compares ``id(...)`` of both
37+ # operands): the buffer must still be the very same bytearray object,
38+ # proving the consumed frame was deleted in place rather than the
39+ # buffer being replaced by a sliced copy.
3640 assert buffer ._data is data
3741 assert buffer ._data == frame
3842
You can’t perform that action at this time.
0 commit comments