Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/h2/frame_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ 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:]
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
Contributor 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
14 changes: 14 additions & 0 deletions tests/test_basic_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
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)

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
Contributor 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
Loading