Skip to content

Avoid per-header allocations in SignalR hub protocols - #70137

Merged
davidfowl merged 1 commit into
mainfrom
perf/signalr-protocol-header-allocs
Nov 16, 2026
Merged

davidfowl merged 1 commit into
mainfrom
perf/signalr-protocol-header-allocs

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Avoid per-header allocations in SignalR hub protocols

Remove per-header allocations in the MessagePack and JSON hub protocols.

Description

Invocation headers are on the hot path whenever the .NET client has distributed tracing enabled, because it injects traceparent/tracestate into every invocation.

  • MessagePack read: ReadHeaders built two interpolated strings per header ($"headers[{i}].Key" / .Value) only to use them in an exception message. The field name is now formatted only when reading fails, and the error messages stay the same.
  • MessagePack and JSON write: Headers is typed as IDictionary<string, string>, so foreach boxed the enumerator. When the instance is a Dictionary<string, string> (what both protocols and the client create), it's now enumerated directly. Other implementations still use the interface path.
  • Adds HubProtocolHeadersBenchmark, which uses an invocation with W3C trace headers plus filler headers.

Benchmarks

HubProtocolHeadersBenchmark ran with BenchmarkDotNet InProcess on a 2 vCPU Linux VM in two interleaved A/B rounds (base, PR, base, PR). Rows show round 1 / round 2.

Method Protocol Headers Base mean PR mean Base alloc PR alloc
ReadMessage MsgPack 2 422 / 433 ns 320 / 334 ns 952 B 728 B
ReadMessage MsgPack 8 1,056 / 1,075 ns 670 / 699 ns 2,880 B 1,984 B
WriteMessage MsgPack 2 237 / 237 ns 220 / 226 ns 56 B 0 B
WriteMessage MsgPack 8 379 / 373 ns 327 / 340 ns 56 B 0 B
ReadMessage Json 2 688 / 692 ns 691 / 694 ns 728 B 728 B
WriteMessage Json 2 278 / 280 ns 268 / 270 ns 56 B 0 B
WriteMessage Json 8 449 / 451 ns 406 / 413 ns 56 B 0 B

MessagePack reads with headers are 23–35% faster, and header writes no longer allocate in either protocol. JSON reads were unaffected, as expected.

Tests

  • Microsoft.AspNetCore.SignalR.Common.Tests (net12.0) passed: 736 passed, 0 failed, 1 skipped. This includes the existing HeaderKeyInt, HeaderValueInt, HeaderKeyArray and HeaderValueArray invalid-message cases, which check the exact Reading 'headers[i].Key/Value' as String failed. messages.
  • Protocols.Json and Protocols.MessagePack build for all target frameworks.

Benchmark (3 interleaved rounds)

HubProtocolHeadersBenchmark (added in this PR; the same source was compiled into both builds). BenchmarkDotNet InProcess with MemoryDiagnoser, --warmupCount 3 --iterationCount 10. Rounds were run interleaved: main, PR, main, PR, main, PR. "main" is the PR's merge base (4f556f7). - allocation means 0 B.

Benchmark Round main mean PR mean Δ main alloc PR alloc
ReadMessage HubProtocol=MsgPack HeaderCount=2 1 466.1 ns 333.5 ns -28.4% 952 B 728 B
ReadMessage HubProtocol=MsgPack HeaderCount=2 2 453.9 ns 377.3 ns -16.9% 952 B 728 B
ReadMessage HubProtocol=MsgPack HeaderCount=2 3 470.3 ns 341.0 ns -27.5% 952 B 728 B
WriteMessage HubProtocol=MsgPack HeaderCount=2 1 262.1 ns 217.5 ns -17.0% 56 B -
WriteMessage HubProtocol=MsgPack HeaderCount=2 2 236.0 ns 215.2 ns -8.8% 56 B -
WriteMessage HubProtocol=MsgPack HeaderCount=2 3 230.4 ns 219.3 ns -4.8% 56 B -
ReadMessage HubProtocol=MsgPack HeaderCount=8 1 1,067.6 ns 675.0 ns -36.8% 2,880 B 1,984 B
ReadMessage HubProtocol=MsgPack HeaderCount=8 2 1,074.2 ns 684.6 ns -36.3% 2,880 B 1,984 B
ReadMessage HubProtocol=MsgPack HeaderCount=8 3 1,119.5 ns 682.1 ns -39.1% 2,880 B 1,984 B
WriteMessage HubProtocol=MsgPack HeaderCount=8 1 396.1 ns 328.9 ns -17.0% 56 B -
WriteMessage HubProtocol=MsgPack HeaderCount=8 2 369.9 ns 330.0 ns -10.8% 56 B -
WriteMessage HubProtocol=MsgPack HeaderCount=8 3 367.7 ns 329.8 ns -10.3% 56 B -
ReadMessage HubProtocol=Json HeaderCount=2 1 690.1 ns 711.4 ns +3.1% 728 B 728 B
ReadMessage HubProtocol=Json HeaderCount=2 2 682.2 ns 697.7 ns +2.3% 728 B 728 B
ReadMessage HubProtocol=Json HeaderCount=2 3 704.1 ns 681.7 ns -3.2% 728 B 728 B
WriteMessage HubProtocol=Json HeaderCount=2 1 277.0 ns 269.4 ns -2.7% 56 B -
WriteMessage HubProtocol=Json HeaderCount=2 2 281.7 ns 262.6 ns -6.8% 56 B -
WriteMessage HubProtocol=Json HeaderCount=2 3 284.0 ns 264.7 ns -6.8% 56 B -
ReadMessage HubProtocol=Json HeaderCount=8 1 1,133.2 ns 1,122.0 ns -1.0% 1,984 B 1,984 B
ReadMessage HubProtocol=Json HeaderCount=8 2 1,142.9 ns 1,109.1 ns -3.0% 1,984 B 1,984 B
ReadMessage HubProtocol=Json HeaderCount=8 3 1,137.1 ns 1,118.0 ns -1.7% 1,984 B 1,984 B
WriteMessage HubProtocol=Json HeaderCount=8 1 454.2 ns 433.8 ns -4.5% 56 B -
WriteMessage HubProtocol=Json HeaderCount=8 2 457.6 ns 412.4 ns -9.9% 56 B -
WriteMessage HubProtocol=Json HeaderCount=8 3 453.1 ns 425.0 ns -6.2% 56 B -

Machine: AMD EPYC 9V74, 2 vCPU, Ubuntu 24.04 (container); .NET SDK 12.0.100-alpha.1.26521.103, runtime .NET 12.0.0 (12.0.26.52911), X64 RyuJIT, BenchmarkDotNet v0.13.0.

Result: consistent across all three rounds. MessagePack reads are 17–39% faster with 224 B (2 headers) or 896 B (8 headers) less allocated per message. Header writes drop from 56 B to 0 B in both protocols and are 3–17% faster. JSON reads are unchanged, within noise, as expected.

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

- MessagePack: don't build interpolated field-name strings for every header
  read; format them only when reading fails.
- JSON and MessagePack: enumerate Dictionary<string, string> headers directly
  to avoid boxing the enumerator on every write.
- Add HubProtocolHeadersBenchmark.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 21:23
@github-actions github-actions Bot added the area-signalr Includes: SignalR clients and servers label Oct 30, 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

The missing frozen source/guidance bundle and existing-feedback snapshot prevent establishing source provenance and feedback deduplication for automated approval.

No open issues 🎉

What changed in this PR

This PR reduces header-processing allocations in SignalR’s MessagePack and JSON protocols, improving the invocation hot path used by distributed tracing.

Changes:

  • Formats MessagePack header field names only when parsing fails, preserving existing error messages.
  • Avoids boxing enumerators when writing Dictionary<string, string> headers, while retaining the interface fallback.
  • Adds read/write benchmarks with two and eight headers, including W3C trace context headers.
File Description
src/​SignalR/​perf/​Microbenchmarks/​HubProtocolHeadersBenchmark.cs Adds header-focused benchmarks for both protocols.
src/​SignalR/​common/​Protocols.MessagePack/​src/​Protocol/​MessagePackHubProtocolWorker.cs Defers error-string formatting and avoids dictionary enumerator boxing.
src/​SignalR/​common/​Protocols.Json/​src/​Protocol/​JsonHubProtocol.cs Avoids dictionary enumerator boxing when writing headers.

Review effort: Balanced

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Nov 13, 2026
@davidfowl
davidfowl merged commit 2b775ea into main Nov 16, 2026
46 checks passed
@davidfowl
davidfowl deleted the perf/signalr-protocol-header-allocs branch November 16, 2026 23:14
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 18, 2026
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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants