Skip to content

perf(h1): pass ParserConfig by reference instead of cloning per parse call#4029

Open
DioCrafts wants to merge 1 commit intohyperium:masterfrom
DioCrafts:perf/parserconfig-zero-copy
Open

perf(h1): pass ParserConfig by reference instead of cloning per parse call#4029
DioCrafts wants to merge 1 commit intohyperium:masterfrom
DioCrafts:perf/parserconfig-zero-copy

Conversation

@DioCrafts
Copy link

Description:

ParseContext owned a full ParserConfig value. Every HTTP/1 parse iteration cloned it from the connection state, even though the config never changes mid-connection.

This PR changes h1_parser_config from ParserConfig to &'a ParserConfig. The clone disappears and the borrow checker guarantees the reference stays valid for the parse call.

What changed

  • ParseContext.h1_parser_config: owned ParserConfig&'a ParserConfig
  • conn.rs: removed .clone() when building ParseContext
  • io.rs: same removal in the buffered I/O parse path
  • role.rs: updated 20 test call sites to pass references

No public API changes, no behavioral changes. All existing tests pass.

Change ParseContext.h1_parser_config from owned ParserConfig to &ParserConfig.
This avoids cloning the parser config on every HTTP/1 parse iteration,
reducing allocations in the hot path.

- mod.rs: Change field type from ParserConfig to &'a ParserConfig
- conn.rs: Pass reference instead of clone
- io.rs: Remove .clone() call when building ParseContext
- role.rs: Update 20 test sites to pass references
@seanmonstar
Copy link
Member

From the previous PR:

I believe it originally used a clone for lifetimes. I assumed the compiler could inline and eliminate the code, does this result in a difference?

@DioCrafts
Copy link
Author

From the previous PR:

I believe it originally used a clone for lifetimes. I assumed the compiler could inline and eliminate the code, does this result in a difference?

Hi @seanmonstar,
Right, the compiler likely elides the clone for 7 bools, but the reference guarantees zero-cost regardless of future changes to ParserConfig and better communicates that the config is borrowed, not owned.

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