Skip to content

Reuse plain frame bodies during serialization - #177

Open
notatallshaw wants to merge 2 commits into
python-hyper:masterfrom
notatallshaw:perf/reuse-plain-frame-body-20261026
Open

notatallshaw wants to merge 2 commits into
python-hyper:masterfrom
notatallshaw:perf/reuse-plain-frame-body-20261026

Conversation

@notatallshaw

Copy link
Copy Markdown

Unpadded DATA and HEADERS serialization copies the complete body while joining it with empty fields, even though no framing data is added.

Avoid that copy for exact bytes bodies. Replaying the 40 affected frame operations from a local HTTP/2 capture is about 7% faster (99% interval: about 3% to 9%).

Comment thread src/hyperframe/frame.py Outdated
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:

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 use isinstance() as the canonical way of checking a if the object is of a certain instance of a class.

@notatallshaw notatallshaw Nov 24, 2026 •

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.

This is intentional, this a fast-path guard. because we know exactly the behavior of the bytes object.

A subclass of bytes could override __len__ and cause writing the wrong payload length. But with this guard they fall through to the join.

@Kriechi

Kriechi commented Nov 24, 2026

Copy link
Copy Markdown
Member

Could the condition check be simplified to only look at self.pad_length provided by the Padding mixin?
E.g., like self.pad_length == 0 and isinstance(self.data, bytes)?

@Kriechi

Kriechi commented Nov 24, 2026

Copy link
Copy Markdown
Member

Pinging @fhgffy from #174 to bring both conversations together.
I think you both raise valid points here, but we need to bring them together:

  • The "PADDED" flag is the single source of truth.
  • If the flag is not set
    • directly return the data as bytes (convert es needed!)
  • If the flag is set
    • serialize the padding data
    • build the b"\0" * self.pad_length padding string
    • assemble all together with the data into a bytes object

Since the same logic applies to 3 frames: HEADERS, DATA, PUSH_PROMISE, we should bring most of the logic into the Padding mixin.

@notatallshaw

Copy link
Copy Markdown
Author

Could the condition check be simplified to only look at self.pad_length provided by the Padding mixin?
E.g., like self.pad_length == 0 and isinstance(self.data, bytes)?

Added a commit that simplifies the check to look at the bytes each field adds. Can't take that approach because with PADDED set and pad_length set to zero, b"abc" has to go out as b"\x00abc", otherwise the receiver reads a as the padding count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants