Skip to content

Avoid parsing the Accept header on non-SSE SignalR connection GETs - #70190

Merged
halter73 merged 1 commit into
mainfrom
perf/signalr-sse-accept-prefilter
Nov 3, 2026
Merged

halter73 merged 1 commit into
mainfrom
perf/signalr-sse-accept-prefilter

Conversation

@davidfowl

Copy link
Copy Markdown
Member

HttpConnectionDispatcher.ExecuteAsync handles every GET to a connection endpoint, which includes each long-polling poll and each WebSocket upgrade. On every one of those GETs it called GetTypedHeaders().Accept, which parses the whole Accept header into MediaTypeHeaderValues, just to check for text/event-stream.

Change

The new IsServerSentEventsRequest first scans the raw Accept values for the substring event-stream (OrdinalIgnoreCase). It runs the original typed-header check only when that substring is present.

A parsed media type that equals text/event-stream requires the raw header to contain the contiguous ASCII token event-stream. So the prefilter can only produce false positives, and those fall through to the unchanged original check. Behavior is identical. There are no public API or wire changes.

Results

Side-by-side BenchmarkDotNet run on a DefaultHttpContext, 3 rounds (TypedParse is the current code, Prefilter is this PR):

Accept Before After Alloc
(none) 43–48 ns 5.2 ns 48 B → 0
*/* 132–138 ns 12.2–12.8 ns 184 B → 0
browser default (text/html,...,*/*;q=0.8) 754–767 ns 22.1–22.2 ns 864 B → 0
text/event-stream (once per SSE connection) 144.5 ns 178–198 ns 184 → 208 B

Hot GET paths, meaning every long-poll and every WebSocket upgrade, are 8–34× faster and allocation-free. The SSE GET happens only once per connection and costs about 40 ns more.

Tests

Added ServerSentEventsRequestDetectionTests. It covers 19 Accept shapes, including case and whitespace variants, multiple values, parameters, invalid entries and near-miss subtypes. Each case asserts that the new method matches the original typed-header expression. Microsoft.AspNetCore.Http.Connections.Tests: 315 passed, 7 skipped.


🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 16:03
@github-actions github-actions Bot added the area-signalr Includes: SignalR clients and servers label Oct 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Both changed files were inspected, but no frozen review bundle was supplied to verify head/base identities, routed guidance, or existing-feedback deduplication.

No open issues 🎉

What changed in this PR

This PR reduces unnecessary Accept-header parsing in SignalR’s HTTP connection dispatcher by adding a raw-header prefilter for server-sent events (SSE) detection.

Changes:

  • Skips typed-header parsing when no Accept value contains event-stream, retaining the original parsed check for potential matches.
  • Adds 19 test cases comparing detection with the original expression, covering casing, whitespace, multiple values, parameters, and malformed entries.
File Description
src/​SignalR/​common/​Http.Connections/​test/​ServerSentEventsRequestDetectionTests.cs Adds equivalence tests for SSE request detection.
src/​SignalR/​common/​Http.Connections/​src/​Internal/​HttpConnectionDispatcher.cs Adds and uses the raw Accept-header prefilter.

Review effort: Balanced

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@halter73
halter73 merged commit fec26c4 into main Nov 3, 2026
32 checks passed
@halter73
halter73 deleted the perf/signalr-sse-accept-prefilter branch November 3, 2026 20:05
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:05 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:05 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:07 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:09 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:10 — with GitHub Actions Inactive
@halter73
halter73 temporarily deployed to copilot-pat-pool November 3, 2026 20:11 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

No docs needed: internal performance optimization with identical behavior and no public API changes.

Documentation surfaces considered

  • Conceptual article: Not required — Internal perf optimization of Accept header checking; no public API, config, or behavior change.
  • Migration guidance: Not required — No user action needed when upgrading.
  • Breaking change: Not required — Behavior is identical; no compatibility impact.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 3a13aeee Deployed Nov 3, 2026 by halter73 via conclusion #182
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-signalr Includes: SignalR clients and servers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants