Repository navigation
Avoid per-header allocations in SignalR hub protocols - #70137
Merged
Merged
Conversation
- 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>
Contributor
There was a problem hiding this comment.
🔵 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
Member
|
/review |
1 of 2 tasks
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/tracestateinto every invocation.ReadHeadersbuilt 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.Headersis typed asIDictionary<string, string>, soforeachboxed the enumerator. When the instance is aDictionary<string, string>(what both protocols and the client create), it's now enumerated directly. Other implementations still use the interface path.HubProtocolHeadersBenchmark, which uses an invocation with W3C trace headers plus filler headers.Benchmarks
HubProtocolHeadersBenchmarkran 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.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 existingHeaderKeyInt,HeaderValueInt,HeaderKeyArrayandHeaderValueArrayinvalid-message cases, which check the exactReading 'headers[i].Key/Value' as String failed.messages.Protocols.JsonandProtocols.MessagePackbuild 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.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)