Repository navigation
Conversation
…e .NET SignalR client Implement IInvocationBinder.GetTarget in the client's binder so hub protocols can return the registered handler name instead of allocating a new string per received invocation. Only use the raw JSON target bytes for the lookup when they contain no escape sequences. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Malformed UTF-8 targets can match a registered replacement-character handler and bypass protocol validation, requiring strict decoding and a client regression test.
2 open issues
What changed in this PR
This PR reduces per-invocation allocations in the .NET SignalR client by reusing registered handler-name strings.
Changes:
- Adds a bounded UTF-8 target lookup in the client binder on modern .NET.
- Bypasses the JSON binder lookup for escaped targets to preserve correct decoding.
- Adds protocol tests for cached target reuse and escaped-target handling.
| File | Description |
|---|---|
| src/SignalR/common/SignalR.Common/test/Internal/Protocol/JsonHubProtocolTests.cs | Tests target reuse and escaped-target decoding. |
| src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs | Prevents lookups using still-escaped target bytes. |
| src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs | Looks up cached handler names without allocating target strings. |
Review effort: Balanced
…t lookup tests Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Pushed an empty commit (e538432) to retrigger CI. The only failure was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Pushed an empty commit (5339660) to retrigger CI. The only failure was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
Replace stackalloc char[utf8Bytes.Length] with a fixed 256-char stack buffer, which is only used once the input is known to fit. Add boundary tests for 255, 256 and 257-byte targets, plus a multi-byte target that fills the buffer exactly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Switched 3 interleaved rounds of
Allocations match the old head, about 5.6 KB less than main per 100 messages. Full table is in the PR description. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Merge assessmentRecommendation: ready to merge once an area-signalr owner signs off. CI is green, the production change is small, and the allocation saving is measurable.
What it does: the .NET client's binder now implements Claimed win (PR body, 3 interleaved rounds, MessagePack): about 56 B less per received invocation. That's −39% allocated with 0 args × 100 messages (14.2 KB → 8.6 KB) and −28% with 1 arg. At 100 messages, time is lower in 11 of 12 paired rounds (−2% to −17%, with one +14% outlier). Single-message rows are noise. JSON (Newtonsoft) in the benchmark doesn't call Points for the reviewer:
I'm the agent that wrote this PR, so treat this as the author's own assessment. I reviewed the source and CI only and didn't rerun anything for this comment. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |


Avoid allocating the target string per invocation received by the .NET SignalR client
Stop allocating a new target string per invocation the .NET client receives.
Description
The server binder already implements
IInvocationBinder.GetTargetto return a cached method name, but the client's binder (HubConnection.ConnectionState) used the default implementation, which returnsnull. As a result, the JSON and MessagePack protocols allocated a newstringfor the target of every invocation the client received.HubConnection.ConnectionState(#if NETonly) now implementsGetTarget. It decodes the UTF-8 target into a constant-size 256-char stack buffer (targets over 256 bytes skip the lookup), and looks up the registered handler name withConcurrentDictionary.GetAlternateLookup<ReadOnlySpan<char>>(). On a match it returns the existing key. Unknown or oversized targets returnnull, and the protocol falls back to allocating, as before.JsonHubProtocolnow passes the raw target bytes toGetTargetonly when!reader.ValueIsEscaped.ValueSpanholds the still-escaped bytes, so an escaped target such as"\u004Dethod"could otherwise be matched against a literally named handler. Client handler names are arbitrary strings, so this matters once the client implementsGetTarget. Escaped targets fall back toreader.GetString().No public API changes.
Benchmark
HubConnectionReceiveBenchmark(existing), BenchmarkDotNet InProcess with MemoryDiagnoser,--warmupCount 3 --iterationCount 15. 2 vCPU Linux VM, .NET 12 (repo SDK), Release. Rounds were interleaved: base, tgt, base, tgt. The benchmark'sjsonparameter usesNewtonsoftJsonHubProtocol, which doesn't callGetTarget, so it's unaffected (allocations unchanged).MessagePack rows (before = 2 base rounds, after = 2 rounds with the change):
The saving is about 56 B per received invocation (one target string). The single-message rows are dominated by benchmark overhead and are noisy on this VM.
Tests
Microsoft.AspNetCore.SignalR.Common.Tests: 738 passed, 1 skipped (pre-existing). Two new tests:JsonHubProtocolTests.TargetUsesBinderGetTargetWhenNotEscapedJsonHubProtocolTests.EscapedTargetIsNotPassedToBinderGetTargetMicrosoft.AspNetCore.SignalR.Client.Tests: 379 passed, 1 skipped (pre-existing). Four new tests inHubConnectionTests.Protocol:ReceivedInvocationTargetReusesRegisteredHandlerNameGetTargetReturnsNullForUnknownOversizedOrInvalidTargetsUnknownOversizedAndInvalidTargetsStillDispatchLikeBeforeInvalidUtf8TargetIsRejectedEvenIfReplacementCharacterHandlerIsRegisteredTarget decoding is strict (
Utf8.ToUtf16withreplaceInvalidSequences: false), so malformed UTF-8 can never match a handler registered as"\uFFFD".Microsoft.AspNetCore.SignalR.Client.Corebuilds for net12.0, net462, netstandard2.0 and netstandard2.1.Benchmark (3 interleaved rounds)
HubConnectionReceiveBenchmark(existing; the same benchmark 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). Only themessagepackrows are shown because they useMessagePackHubProtocol, which calls the client's newGetTarget. The benchmark'sjsonparameter usesNewtonsoftJsonHubProtocol, which doesn't callGetTarget, so its allocations were unchanged and its timings were noise.| Benchmark | Round | main mean | PR mean | Δ | main alloc | PR alloc |
| ReceiveAsync ArgumentCount=0 MessageCount=1 Protocol=messagepack | 1 | 6.193 us | 7.583 us | +22.4% | 749 B | 694 B |
| ReceiveAsync ArgumentCount=0 MessageCount=1 Protocol=messagepack | 2 | 6.609 us | 7.725 us | +16.9% | 764 B | 684 B |
| ReceiveAsync ArgumentCount=0 MessageCount=1 Protocol=messagepack | 3 | 7.254 us | 5.178 us | -28.6% | 765 B | 703 B |
| ReceiveAsync ArgumentCount=0 MessageCount=100 Protocol=messagepack | 1 | 115.996 us | 107.228 us | -7.6% | 14,236 B | 8,605 B |
| ReceiveAsync ArgumentCount=0 MessageCount=100 Protocol=messagepack | 2 | 110.254 us | 106.864 us | -3.1% | 14,238 B | 8,631 B |
| ReceiveAsync ArgumentCount=0 MessageCount=100 Protocol=messagepack | 3 | 114.113 us | 105.678 us | -7.4% | 14,230 B | 8,576 B |
| ReceiveAsync ArgumentCount=1 MessageCount=1 Protocol=messagepack | 1 | 8.614 us | 7.565 us | -12.2% | 818 B | 756 B |
| ReceiveAsync ArgumentCount=1 MessageCount=1 Protocol=messagepack | 2 | 5.785 us | 3.179 us | -45.0% | 822 B | 741 B |
| ReceiveAsync ArgumentCount=1 MessageCount=1 Protocol=messagepack | 3 | 9.097 us | 7.365 us | -19.0% | 816 B | 748 B |
| ReceiveAsync ArgumentCount=1 MessageCount=100 Protocol=messagepack | 1 | 123.663 us | 102.954 us | -16.7% | 19,832 B | 14,226 B |
| ReceiveAsync ArgumentCount=1 MessageCount=100 Protocol=messagepack | 2 | 127.179 us | 111.328 us | -12.5% | 19,833 B | 14,224 B |
| ReceiveAsync ArgumentCount=1 MessageCount=100 Protocol=messagepack | 3 | 124.538 us | 115.489 us | -7.3% | 19,829 B | 14,232 B |
| ReceiveAsync ArgumentCount=10 MessageCount=1 Protocol=messagepack | 1 | 12.871 us | 11.692 us | -9.2% | 1,322 B | 1,253 B |
| ReceiveAsync ArgumentCount=10 MessageCount=1 Protocol=messagepack | 2 | 9.866 us | 4.238 us | -57.0% | 1,317 B | 1,258 B |
| ReceiveAsync ArgumentCount=10 MessageCount=1 Protocol=messagepack | 3 | 11.518 us | 8.638 us | -25.0% | 1,317 B | 1,256 B |
| ReceiveAsync ArgumentCount=10 MessageCount=100 Protocol=messagepack | 1 | 174.141 us | 164.856 us | -5.3% | 70,218 B | 64,615 B |
| ReceiveAsync ArgumentCount=10 MessageCount=100 Protocol=messagepack | 2 | 165.858 us | 189.266 us | +14.1% | 70,231 B | 64,633 B |
| ReceiveAsync ArgumentCount=10 MessageCount=100 Protocol=messagepack | 3 | 184.737 us | 160.169 us | -13.3% | 70,226 B | 64,626 B |
| ReceiveAsync ArgumentCount=100 MessageCount=1 Protocol=messagepack | 1 | 18.035 us | 20.677 us | +14.6% | 6,333 B | 6,297 B |
| ReceiveAsync ArgumentCount=100 MessageCount=1 Protocol=messagepack | 2 | 16.709 us | 19.405 us | +16.1% | 6,347 B | 6,302 B |
| ReceiveAsync ArgumentCount=100 MessageCount=1 Protocol=messagepack | 3 | 19.703 us | 16.177 us | -17.9% | 6,251 B | 6,286 B |
| ReceiveAsync ArgumentCount=100 MessageCount=100 Protocol=messagepack | 1 | 703.096 us | 667.599 us | -5.0% | 574,238 B | 568,595 B |
| ReceiveAsync ArgumentCount=100 MessageCount=100 Protocol=messagepack | 2 | 738.648 us | 722.861 us | -2.1% | 574,230 B | 568,638 B |
| ReceiveAsync ArgumentCount=100 MessageCount=100 Protocol=messagepack | 3 | 715.320 us | 664.093 us | -7.2% | 574,208 B | 568,630 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: the allocation reduction is consistent in every round, about 56 B per received invocation (one target string), so 100 messages allocate about 5.6 KB less. That's −39% with 0 arguments and −28% with 1. Time on the 100-message rows improved in 11 of 12 round pairs, by 2–17%, but one round with 10 arguments was +14%, so the time gain is modest and noisy on this 2-vCPU VM. The 1-message rows are dominated by benchmark overhead and are noise in both directions.
Benchmark after switching to a constant-size stack buffer (3 interleaved rounds)
GetTargetnow usesstackalloc char[256]instead ofstackalloc char[utf8Bytes.Length], so the stack allocation size is no longer a runtime value. SameHubConnectionReceiveBenchmark, BenchmarkDotNet InProcess with MemoryDiagnoser,--warmupCount 3 --iterationCount 10, rounds interleaved main, old PR head (5339660), new head (e21da1a).messagepackrows only, times in us. Same machine as above.Result: allocations with the fixed buffer match the old PR head (about 56 B less per received invocation than main). Times on the 100-message rows are lower than main in all 12 round pairs, by up to 15%, though two of the 10-argument rounds differ by only about 1%. They're in line with the old head. The 1-message rows are noise.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)