Skip to content

Cache SignalR stream converter factories instead of reflecting per uploaded stream - #70188

Merged
davidfowl merged 2 commits into
mainfrom
perf/signalr-stream-converter-cache
Nov 16, 2026
Merged

davidfowl merged 2 commits into
mainfrom
perf/signalr-stream-converter-cache

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Cache SignalR stream converter factories instead of reflecting per uploaded stream

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Cache SignalR stream converter factories instead of reflecting per stream.

Description

For every client-to-server stream argument (ChannelReader<T> or IAsyncEnumerable<T> hub method parameters), StreamTracker.AddStream built the channel with MakeGenericMethod(itemType).Invoke(null, args). That runs reflection on every uploaded stream: generic method instantiation lookup, a reflection invoke, and the boxed/array arguments.

Change

  • Cache a Func<int, IStreamConverter> per item type in a static ConcurrentDictionary, created once with MakeGenericMethod(itemType).CreateDelegate(...), and invoke the delegate per stream.
  • Each call still creates a new ChannelConverter<T> and channel. The cache holds only per-type factory delegates. No channel, converter or connection state is cached or shared, so nothing leaks between connections.
  • Item types come from hub method signatures (HubMethodDescriptor.StreamingParameters), so the cache is bounded.
  • The trimming and AOT suppressions move to the new CreateConverterFactory helper with the same justifications. The IsDynamicCodeSupported invariant is unchanged.

There are no public API, behavior or wire changes.

Benchmark

A new StreamTrackerBenchmark.AddAndCompleteStream registers a ChannelReader<string> stream and completes it, which is the per-upload-stream bookkeeping. BenchmarkDotNet, 3 interleaved base/PR rounds, warmup 3 / iterations 10, 2 vCPU Linux x64:

Round 1 Round 2 Round 3 Avg Gen0/1k ops
main 606.7 ns 591.9 ns 609.5 ns 602.7 ns 0.0734
PR 307.2 ns 302.3 ns 311.7 ns 307.1 ns 0.0620

That is -49% time and about 16% less allocated per stream: the Invoke argument and boxing overhead is gone, while the channel itself still dominates allocations. I also tried the existing end-to-end DefaultHubDispatcherBenchmark.UploadStream*_One, but its thread-hop noise (StdDev 3–5 µs on a 5–20 µs mean) makes it too unstable to quote.

Tests

  • New StreamTrackerTests:
    • Two streams with the same item type get independent channels.
    • Reference-type and value-type item types both work, with ChannelReader<T> and IAsyncEnumerable<T> targets.
    • A duplicate stream ID still throws.
  • Microsoft.AspNetCore.SignalR.Tests: 499 passed, 1 skipped.

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

…loaded stream

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 15:50
@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.

🟢 Approval recommended

No active review issues are reported for the upload-stream factory caching change.

No open issues 🎉

What changed in this PR

This PR reduces SignalR upload-stream setup overhead by caching converter factory delegates per item type instead of invoking reflection for every stream. Each stream still receives its own converter and channel.

Changes:

  • Cache factory delegates while preserving the existing trimming and AOT safeguards.
  • Add tests for independent channels, item and target types, and duplicate stream IDs.
  • Add a microbenchmark for stream registration and completion.
File Description
src/​SignalR/​server/​SignalR/​test/​Microsoft.AspNetCore.SignalR.Tests/​Internal/​StreamTrackerTests.cs Adds stream isolation, type handling, and duplicate-ID tests.
src/​SignalR/​server/​Core/​src/​StreamTracker.cs Replaces per-stream reflection invocation with cached factory delegates.
src/​SignalR/​perf/​Microbenchmarks/​StreamTrackerBenchmark.cs Measures stream registration and completion overhead.

Review effort: Balanced

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

Copy link
Copy Markdown
Member Author

Pushed an empty commit (264369d) to retrigger CI. Every build and Helix job passed. The only failure was 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.86% failure rate, and it also flaked on unrelated PRs #70142 and #70185. It doesn't exercise hub streaming.


🤖 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 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 3cd798f into main Nov 16, 2026
44 checks passed
@davidfowl
davidfowl deleted the perf/signalr-stream-converter-cache branch November 16, 2026 23:05
@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