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
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dev

**Bugfixes**

-
- Avoided copying unmodified DATA and HEADERS bodies during serialization.

6.1.0 (2025-01-22)
------------------
Expand Down
14 changes: 14 additions & 0 deletions src/hyperframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ def serialize_body(self) -> bytes:
padding = b"\0" * self.pad_length
if isinstance(self.data, memoryview):
self.data = self.data.tobytes()

if type(self.data) is bytes and type(padding_data) is bytes and not padding_data and not padding:
return self.data

return b"".join([padding_data, self.data, padding])

def parse_body(self, data: memoryview) -> None:
Expand Down Expand Up @@ -748,6 +752,16 @@ def serialize_body(self) -> bytes:
else:
priority_data = b""

if (
type(self.data) is bytes
and type(padding_data) is bytes
and not padding_data
and type(priority_data) is bytes
and not priority_data
and not padding
):
return self.data

return b"".join([padding_data, priority_data, self.data, padding])

def parse_body(self, data: memoryview) -> None:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ def test_data_frame_serializes_properly(self, data):
s = f.serialize()
assert s == self.payload

def test_plain_data_frame_reuses_bytes_body(self):
data = b'testdata'
f = DataFrame(1, data)

assert f.serialize_body() is data

def test_data_frame_with_padding_serializes_properly(self):
f = DataFrame(1)
f.flags = set(['END_STREAM', 'PADDED'])
Expand Down Expand Up @@ -791,6 +797,12 @@ def test_headers_frame_serializes_properly(self):
b'hello world'
)

def test_plain_headers_frame_reuses_bytes_body(self):
data = b'hello world'
f = HeadersFrame(1, data)

assert f.serialize_body() is data

def test_headers_frame_parses_properly(self):
s = (
b'\x00\x00\x0B\x01\x05\x00\x00\x00\x01' +
Expand Down