Skip to content

perf: defer send buffer locking while receiving - #933

Open
brentechols wants to merge 1 commit into
hyperium:masterfrom
brentechols:agent/defer-recv-send-buffer-lock
Open

perf: defer send buffer locking while receiving#933
brentechols wants to merge 1 commit into
hyperium:masterfrom
brentechols:agent/defer-recv-send-buffer-lock

Conversation

@brentechols

@brentechols brentechols commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Summary

  • avoid taking the outbound frame-buffer mutex for every successfully received HEADERS or DATA frame
  • acquire it only when receive processing must enqueue a response or reset

Motivation

recv_headers and recv_data currently lock SendBuffer before processing an inbound frame, although the common successful path does not write to that buffer. Deferring the lock removes an unnecessary nested mutex acquisition and its cache traffic from the hot receive path.

The caller still holds the connection-wide Inner lock, so this patch does not remove the primary sender/driver serialization. It is intentionally a small lock-overhead optimization; oversized responses and receive errors still lock before queuing outbound frames.

Scope

The implementation is limited to one file. Existing integration coverage already exercises ordinary HEADERS/DATA receipt, oversized responses, and receive errors that emit RST_STREAM, so no test tied only to mutex placement was added.

Performance

In the initial isolated 256-stream, 65,536-frame run, median elapsed time moved from 369 ms to 355 ms (+3.9% throughput). Later Windows runs were scheduler-bimodal and placed the standalone result within roughly 1% in the fast cluster, so this should be treated as a modest hot-path improvement rather than a broad contention fix.

Validation

  • cargo fmt --all -- --check
  • cargo check --all-targets --offline
  • cargo test -p h2 --lib --no-default-features --offline (428 passed)
  • cargo test -p h2-tests --offline (complete integration suite, including the 5,000-connection hammer test)

@seanmonstar

Copy link
Copy Markdown
Member

Awesome! I'd like to merge this (haven't fully reviewed yet), just waiting for any potential quick fixes needed for the release yesterday. Probably start merging new non-fixes next week.

@brentechols

Copy link
Copy Markdown
Contributor Author

Sounds good! I'll try and get this dressed up and ready for review/merge very soon - this was still mostly just perf golfing around and code isn't well thought through :)

@brentechols
brentechols marked this pull request as ready for review August 22, 2026 01:02
@brentechols

Copy link
Copy Markdown
Contributor Author

@seanmonstar - just marked it as ready to review, let me know what you think

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