Repository navigation
Cache SignalR stream converter factories instead of reflecting per uploaded stream - #70188
Conversation
…loaded stream Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 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>
|
Pushed an empty commit (264369d) to retrigger CI. Every build and Helix job passed. The only failure was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Cache SignalR stream converter factories instead of reflecting per uploaded stream
Cache SignalR stream converter factories instead of reflecting per stream.
Description
For every client-to-server stream argument (
ChannelReader<T>orIAsyncEnumerable<T>hub method parameters),StreamTracker.AddStreambuilt the channel withMakeGenericMethod(itemType).Invoke(null, args). That runs reflection on every uploaded stream: generic method instantiation lookup, a reflection invoke, and the boxed/array arguments.Change
Func<int, IStreamConverter>per item type in a staticConcurrentDictionary, created once withMakeGenericMethod(itemType).CreateDelegate(...), and invoke the delegate per stream.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.HubMethodDescriptor.StreamingParameters), so the cache is bounded.CreateConverterFactoryhelper with the same justifications. TheIsDynamicCodeSupportedinvariant is unchanged.There are no public API, behavior or wire changes.
Benchmark
A new
StreamTrackerBenchmark.AddAndCompleteStreamregisters aChannelReader<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:That is -49% time and about 16% less allocated per stream: the
Invokeargument and boxing overhead is gone, while the channel itself still dominates allocations. I also tried the existing end-to-endDefaultHubDispatcherBenchmark.UploadStream*_One, but its thread-hop noise (StdDev 3–5 µs on a 5–20 µs mean) makes it too unstable to quote.Tests
StreamTrackerTests:ChannelReader<T>andIAsyncEnumerable<T>targets.Microsoft.AspNetCore.SignalR.Tests: 499 passed, 1 skipped.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)