Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/h2/frame_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ def __next__(self) -> Frame:

# At this point, as we know we'll use or discard the entire frame, we
# can update the data.
self._data = self._data[9+length:]
# Deleting the consumed prefix mutates the bytearray in place instead
# of copying the remaining bytes into a new object, as slicing would.
# ``del s[i:j]`` is documented for mutable sequences in
# https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
# and CPython's bytearray tracks an internal offset (``ob_start`` in
# Objects/bytearrayobject.c) that makes repeated deletes from the
# front amortized O(1) per byte rather than O(len) per frame.
del self._data[:9+length]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here about the behaviour with in-place update without copy, ideally referencing CPython docs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 3a59b4a — the comment explains that the slice delete mutates the bytearray in place instead of copying the remainder, with a reference to the mutable-sequence docs (https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types) and to the ob_start offset in CPython's Objects/bytearrayobject.c that makes front deletes amortized O(1).


# Pass the frame through the header buffer.
new_frame = self._update_header_buffer(f)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_basic_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
from . import helpers


class TestFrameBuffer:
def test_consumed_frames_are_removed_in_place(self) -> None:
frame = hyperframe.frame.SettingsFrame(0).serialize()
buffer = h2.frame_buffer.FrameBuffer()
buffer.max_frame_size = 65535
buffer.add_data(frame * 2)
data = buffer._data

next(buffer)

# ``is`` checks object identity (CPython compares ``id(...)`` of both
# operands): the buffer must still be the very same bytearray object,
# proving the consumed frame was deleted in place rather than the
# buffer being replaced by a sliced copy.
assert buffer._data is data

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment to clearly call out that this checks if the object is still the same (internally checked with the id(...) function.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3a59b4a — the comment now calls out that is asserts object identity (CPython compares id(...) of the operands), i.e. the buffer is still the very same bytearray object rather than a sliced copy.

assert buffer._data == frame


class TestBasicClient:
"""
Basic client-side tests.
Expand Down