Skip to content

Commit 3a59b4a

Browse files
committed
Document in-place prefix deletion and the identity assertion
1 parent a741c6d commit 3a59b4a

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/h2/frame_buffer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

tests/test_basic_logic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)