Skip to content

Avoid allocating the target string per invocation received by the .NET SignalR client - #70142

Open
davidfowl wants to merge 6 commits into
mainfrom
perf/signalr-client-target-lookup
Open

davidfowl wants to merge 6 commits into
mainfrom
perf/signalr-client-target-lookup

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Avoid allocating the target string per invocation received by the .NET SignalR client

  • Read the Contributor Guide and Code of Conduct.
  • Include unit tests for the changed behavior.
  • Include inline documentation where applicable.
  • Link an existing issue. No issue is linked to this PR.

Stop allocating a new target string per invocation the .NET client receives.

Description

The server binder already implements IInvocationBinder.GetTarget to return a cached method name, but the client's binder (HubConnection.ConnectionState) used the default implementation, which returns null. As a result, the JSON and MessagePack protocols allocated a new string for the target of every invocation the client received.

  • HubConnection.ConnectionState (#if NET only) now implements GetTarget. 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 with ConcurrentDictionary.GetAlternateLookup<ReadOnlySpan<char>>(). On a match it returns the existing key. Unknown or oversized targets return null, and the protocol falls back to allocating, as before.
  • JsonHubProtocol now passes the raw target bytes to GetTarget only when !reader.ValueIsEscaped. ValueSpan holds 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 implements GetTarget. Escaped targets fall back to reader.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's json parameter uses NewtonsoftJsonHubProtocol, which doesn't call GetTarget, so it's unaffected (allocations unchanged).

MessagePack rows (before = 2 base rounds, after = 2 rounds with the change):

Args Messages Mean before Mean after Allocated before Allocated after
0 100 114.97 / 113.06 us 105.79 / 104.61 us (-7%) 14,238 B 8,623 B (-39%)
1 100 119.26 / 124.41 us 116.34 / 118.54 us 19,829 B 14,233 B (-28%)
10 100 197.48 / 196.09 us 183.70 / 170.66 us (-10%) 70,226 B 64,633 B (-8%)
100 100 714.40 / 714.48 us 665.56 / 666.53 us (-7%) 574,242 B 568,583 B
0 1 4.78 / 5.38 us 9.02 / 4.45 us (noise) 751 B 710 B
1 1 6.56 / 6.48 us 6.45 / 6.70 us 822 B 767 B

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.TargetUsesBinderGetTargetWhenNotEscaped
    • JsonHubProtocolTests.EscapedTargetIsNotPassedToBinderGetTarget
  • Microsoft.AspNetCore.SignalR.Client.Tests: 379 passed, 1 skipped (pre-existing). Four new tests in HubConnectionTests.Protocol:

    • ReceivedInvocationTargetReusesRegisteredHandlerName
    • GetTargetReturnsNullForUnknownOversizedOrInvalidTargets
    • UnknownOversizedAndInvalidTargetsStillDispatchLikeBefore
    • InvalidUtf8TargetIsRejectedEvenIfReplacementCharacterHandlerIsRegistered

    Target decoding is strict (Utf8.ToUtf16 with replaceInvalidSequences: false), so malformed UTF-8 can never match a handler registered as "\uFFFD".

  • Microsoft.AspNetCore.SignalR.Client.Core builds 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 the messagepack rows are shown because they use MessagePackHubProtocol, which calls the client's new GetTarget. The benchmark's json parameter uses NewtonsoftJsonHubProtocol, which doesn't call GetTarget, 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)

GetTarget now uses stackalloc char[256] instead of stackalloc char[utf8Bytes.Length], so the stack allocation size is no longer a runtime value. Same HubConnectionReceiveBenchmark, BenchmarkDotNet InProcess with MemoryDiagnoser, --warmupCount 3 --iterationCount 10, rounds interleaved main, old PR head (5339660), new head (e21da1a). messagepack rows only, times in us. Same machine as above.

Args Msgs Round main old PR new main alloc old alloc new alloc
0 1 1 4.516 7.568 7.808 741 B 699 B 696 B
0 1 2 6.857 11.421 5.767 764 B 718 B 694 B
0 1 3 5.734 5.451 6.723 742 B 673 B 690 B
0 100 1 111.967 102.168 97.841 14,235 B 8,626 B 8,626 B
0 100 2 102.697 106.379 99.180 14,232 B 8,634 B 8,631 B
0 100 3 110.905 100.973 102.086 14,236 B 8,632 B 8,636 B
1 1 1 6.768 3.345 7.743 814 B 753 B 757 B
1 1 2 9.124 6.761 9.083 816 B 764 B 763 B
1 1 3 7.605 8.210 8.185 818 B 759 B 757 B
1 100 1 124.696 116.136 105.482 19,825 B 14,230 B 14,230 B
1 100 2 126.033 115.334 119.654 19,833 B 14,224 B 14,227 B
1 100 3 127.647 114.109 106.708 19,829 B 14,233 B 14,230 B
10 1 1 11.215 13.504 10.861 1,302 B 1,266 B 1,251 B
10 1 2 11.794 11.160 11.263 1,318 B 1,232 B 1,262 B
10 1 3 11.181 10.715 10.216 1,314 B 1,257 B 1,266 B
10 100 1 179.842 158.709 178.901 70,213 B 64,629 B 64,633 B
10 100 2 188.830 179.792 175.252 70,228 B 64,636 B 64,630 B
10 100 3 178.489 156.754 176.425 70,226 B 64,629 B 64,631 B
100 1 1 20.241 20.311 19.520 6,352 B 6,292 B 6,309 B
100 1 2 17.850 17.199 17.450 6,353 B 6,295 B 6,265 B
100 1 3 16.829 20.517 18.751 6,266 B 6,257 B 6,298 B
100 100 1 699.374 677.867 648.598 574,236 B 568,648 B 568,625 B
100 100 2 742.913 688.859 699.764 574,238 B 568,613 B 568,646 B
100 100 3 691.041 632.943 667.633 574,228 B 568,619 B 568,636 B

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)

…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>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 22:07
@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.

🟡 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

davidfowl and others added 2 commits October 30, 2026 23:00
…t lookup tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Member Author

Pushed an empty commit (e538432) to retrigger CI. The only failure was Microsoft.AspNetCore.Mvc.FunctionalTests.RealServerUsingMinimalBackedIntegrationTests.CanResolveServices on Windows (IServerAddressesFeature.Addresses cannot be modified after the server has started), which Build Analysis lists at a 0.37% failure rate. It's an MVC WebApplicationFactory test and doesn't touch the SignalR client code changed here.


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

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Member Author

Pushed an empty commit (5339660) to retrigger CI. The only failure was Microsoft.AspNetCore.Components.E2ETest.Tests.VirtualizationTest.VariableHeight_HtmlTable_DoesNotProduceNestedTrElements in the Blazor E2E job (Mono client and WebAssembly), with Assert.NotEmpty() Failure: Collection was empty. Build Analysis lists that test with a 10.57% failure rate. It's unrelated to the SignalR client. The previous commit was empty, and the same job passed on identical code at c921812.


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

@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 4, 2026 — with Copilot SWE Agent
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>

Copy link
Copy Markdown
Member Author

Switched GetTarget from stackalloc char[utf8Bytes.Length] to a constant-size stackalloc char[256] buffer, so the stack allocation size no longer depends on input (a safety rule for this work). Targets over 256 bytes still skip the lookup, and the protocol allocates the string as before. I also added boundary tests for 255-, 256- and 257-byte targets and a multi-byte target that fills the buffer exactly. Client tests: 379 passed, 1 skipped (pre-existing).

3 interleaved rounds of HubConnectionReceiveBenchmark (messagepack, 100 messages, mean in us, main / old head / new head):

Args Round 1 Round 2 Round 3
0 112.0 / 102.2 / 97.8 102.7 / 106.4 / 99.2 110.9 / 101.0 / 102.1
1 124.7 / 116.1 / 105.5 126.0 / 115.3 / 119.7 127.6 / 114.1 / 106.7
10 179.8 / 158.7 / 178.9 188.8 / 179.8 / 175.3 178.5 / 156.8 / 176.4
100 699.4 / 677.9 / 648.6 742.9 / 688.9 / 699.8 691.0 / 632.9 / 667.6

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)

@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 4, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 4, 2026 — with Copilot SWE Agent
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 4, 2026
@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 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Merge assessment

Recommendation: ready to merge once an area-signalr owner signs off. CI is green, the production change is small, and the allocation saving is measurable.

Production src +27 / −1 in 2 files, 2 assemblies (SignalR.Client.Core, plus a one-line guard in SignalR.Protocols.Json, which the server also uses)
Tests +228 (2 new JsonHubProtocolTests, 4 new HubConnectionTests.Protocol)
Benchmarks None added. Uses the existing HubConnectionReceiveBenchmark.
Public API None (explicit IInvocationBinder.GetTarget implementation, #if NET only)
CI on head d3ed8b13f7 All checks pass, including Build Analysis
Review threads 2 (Copilot), both resolved. No approvals yet.
Base Merge base 4f556f73a6, 469 commits behind main. GitHub reports it as mergeable. Since then, main changed HubConnection.cs (#70149) and JsonHubProtocol.cs (#70137), but neither touches these lines.

What it does: the .NET client's binder now implements GetTarget. It decodes the UTF-8 target into a fixed 256-char stack buffer and looks up the registered handler name through GetAlternateLookup<ReadOnlySpan<char>>(), which works because _handlers uses StringComparer.Ordinal. On a hit it returns the existing key instead of allocating a new string. Decoding is strict, so malformed UTF-8 falls back to the old path. The JSON protocol now passes ValueSpan to GetTarget only when the value isn't escaped. That fixes a latent mismatch where escaped bytes could be compared with a handler name. On the server, an escaped target already failed the lookup and fell back to GetString(), so the server's result is the same.

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 GetTarget, so it's unaffected. System.Text.Json JsonHubProtocol benefits but isn't benchmarked.

Points for the reviewer:

  • Only net targets benefit. net462 and netstandard keep the old behavior.
  • Targets over 256 bytes skip the lookup by design.

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)

@PureWeen

Copy link
Copy Markdown
Member

/review

This branch has not been deployed

No deployments
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