Skip to content

perf: parse WebSocket handshake headers lazily - #610

Merged
Emyrk merged 5 commits into
coder:masterfrom
Emyrk:core-824-websocket-header-parsing
Dec 4, 2026
Merged

Emyrk merged 5 commits into
coder:masterfrom
Emyrk:core-824-websocket-header-parsing

Conversation

@Emyrk

@Emyrk Emyrk commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Optimize WebSocket handshake header parsing with strings.SplitSeq iterators instead of constructing slices of tokens and extension parameters.

  • Consume header tokens lazily and stop when matching values are found.
  • Preserve subprotocol, compression, and client-side extension validation behavior.
  • Benchmark delimiter-heavy headers and assert bounded allocation growth for non-empty tokens in server and client handshakes.

Coder Agents on behalf of @Emyrk.

Use iterators for header tokens and extension parameters instead of collecting split values into slices. Preserve handshake negotiation behavior.
@Emyrk
Emyrk marked this pull request as ready for review December 2, 2026 23:34

@mafredri mafredri left a comment

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.

Nice cleanup. Switching handshake header parsing to iterators avoids intermediate slice allocations and skips extension parsing when CompressionDisabled is set.

A few things to address before merging:

  1. Per-offer heap escapes in acceptDeflate / websocketExtension (inline on accept.go:329-353).
  2. Repeated Sec-WebSocket-Protocol scans in selectSubprotocol (inline on accept.go:267-276).
  3. Broader test and benchmark coverage beyond delimiter-only Accept headers (inline on accept_test.go:595-631).

Minor notes (non-blocking):

  • verifyServerExtensions drops the : %+v (exts[1:]) suffix from its error string, and selectSubprotocol now skips empty tokens even if AcceptOptions.Subprotocols contains "". Both are reasonable changes; worth noting briefly in the PR description / commit message instead of saying all behavior is unchanged.
  • The test (1.26) goroutine-leak failure is not caused by this PR. The same persistConn leak reproduces locally on master with TestBadDials/badReq/badTLS.

🤖 Created with the help of Coder Agents.

Comment thread accept.go Resolved
Comment thread accept.go Resolved
Comment thread dial.go Resolved
Comment thread accept_test.go Resolved
Emyrk added 3 commits December 3, 2026 16:31
Assert that allocations and bytes stay flat as non-empty handshake tokens grow. Store extension parameters without per-offer closures and allocate compression options only for accepted offers.
Retain only higher-ranked server preferences after a match. Check exact heap allocation counts with a fixed byte ceiling across the handshake parsing cases.
@Emyrk
Emyrk requested a review from mafredri December 3, 2026 22:29
@mafredri

mafredri commented Dec 4, 2026

Copy link
Copy Markdown
Member

Looks good now, thanks for iterating on selectSubprotocol, websocketExtension, and the allocation tests.

Re: storing compressionOptions by value in Conn: probably not worth the churn for one 2 B allocation on accepted compressed connections, since c.copts != nil keeps the enabled check simple across the codebase.

One tiny non-blocking note for the PR description / squash commit: worth mentioning the shortened verifyServerExtensions error string and that empty subprotocol tokens are now skipped.

🤖 Created with the help of Coder Agents.

Comment thread accept.go Resolved
@Emyrk
Emyrk merged commit 701ae34 into coder:master Dec 4, 2026
9 of 10 checks passed
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