Repository navigation
Conversation
…tion Resolving the scoped IHubActivator<THub> per invocation grows the scope's resolved-services cache and costs ~184 B and ~55 ns per call. When the container produces the built-in DefaultHubActivator<THub>, construct it directly with the same scoped service provider. Custom activators are still resolved from DI on every call. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The fast path can permanently bypass a scoped factory after it first returns the default activator, preventing later scopes from using their custom activator’s Create/Release methods.
1 open issue
What changed in this PR
This PR reduces SignalR hub-dispatch overhead by constructing the built-in hub activator directly after its first resolution, avoiding repeated scoped DI lookups.
Changes:
- Adds cached activator selection for hub invocations and lifecycle callbacks.
- Adds a regression test checking custom activator use across connection, invocation, and disconnection handling.
| File | Description |
|---|---|
| src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.cs | Tests custom activator release counts across lifecycle callbacks and repeated invocations. |
| src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs | Centralizes activator selection and adds a direct-construction fast path for the default activator. |
Review effort: Balanced
Gate the DefaultHubActivator fast path on SignalR's own open generic descriptor still being the effective registration, so factory or closed registrations keep being resolved from DI per scope. Co-authored-by: Copilot <[email protected]>
|
Compatibility audit (no public API or behavior changes intended)
🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
/review |
There was a problem hiding this comment.
Source-only review of frozen head 1dccf07c97ff5ea8673617756f4f9792be5178f1 found one new P2 issue: an external container's varying scoped IHubActivator<THub> registration can be bypassed after its first scope returns the built-in activator.
The routed docs/CrossCuttingGuidance.md review completed with no unresolved candidates or required-input failures. Existing feedback #discussion_r4428822932 correctly covered the in-collection scoped-factory variant and its fix; this comment covers the remaining out-of-collection container path. Security assessment and target-code/test/CI/browser execution were excluded by the prepared guidance. No SignalR-specific guide was routed, so that domain remains uncovered beyond the cross-cutting review.
Provenance: immutable version-2 bundle for dotnet/aspnetcore#70141, merge base/base tip 4f556f73a610b386e5ec29355a300e493d30c4c5, trusted guidance commit 4f556f73a610b386e5ec29355a300e493d30c4c5. Proof is source-only; bundled tracked Git bytes cannot establish symlink, submodule, LFS target, runtime, or external-container implementation behavior.
Generated by ASP.NET Core Pull Request Review for #70141 · copilot · gpt56 · 143.4 AIC · ⌖ 11.5 AIC · ⊞ 20.2K
Comment /review to run again
…ncyInjection scopes Other containers can supply IHubActivator<THub> outside the IServiceCollection or vary it per scope, so keep resolving it per invocation for them. Co-authored-by: Copilot <[email protected]>
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Merge assessmentVerdict: Hold; a SignalR reviewer requested changes, favoring a simpler first-resolved activator type check over retaining the
Reasons to merge
Reasons not to merge yet
What would change this verdict: A SignalR owner would need to choose whether any version is worth merging, address the changes-requested review, and remeasure on current main before revisiting the merge case. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
halter73
left a comment
There was a problem hiding this comment.
Wouldn't this be 100x simpler if we went back to checking whether the first resolved activator is DefaultHubActivator<THub>? That type is internal and sealed. An open-generic IHubActivator<> registration always constructs its ImplementationType, so with MS.DI the check can't pass in one scope and fail in another. You'd need a per-hub closed factory deliberately built around our internal type, or a third-party container decorator that conditionally returns the inner activator. I'm fine not supporting either. I don't think they justify retaining the IServiceCollection in a singleton and detecting the DI container by assembly name.
That said, this whole thing still seems pretty hacky. Even with the simpler type check, I'd lean towards closing this, since I don't think the complexity is worth the ~50ns speedup.

Avoid resolving the default IHubActivator from DI on every hub invocation
Skip DI resolution of the default
IHubActivator<THub>on each hub invocation.Description
DefaultHubDispatcherresolves the scopedIHubActivator<THub>from a new service scope for every invocation, plusOnConnectedAsync,OnDisconnectedAsyncandOnAuthenticationRefreshedAsync. When the scope resolves its first scoped service, its resolved-services dictionary grows. That growth and the DI lookup together cost about 184 B and 55 ns per invocation. I measured this separately: the scope alone is 128 B, the scope plus the activator is 344 B, and the activator itself is only a 32 B object.The built-in
DefaultHubActivator<THub>is internal, sealed and not disposable. Its only state is the scopedIServiceProvider. With this change, the dispatcher resolves the activator from DI the first time, as before, and records whether the container producedDefaultHubActivator<THub>. If it did, later calls constructnew DefaultHubActivator<THub>(scope.ServiceProvider)directly. The scoped provider is the same one, so hub construction, scoped dependencies,Releaseand disposal semantics don't change. Custom activators are still resolved from DI on every call. Because the default type is internal, user registrations can't produce it.I added a test that registers a custom activator and checks that it's used for
OnConnectedAsync, for every one of several invocations and forOnDisconnectedAsync.Benchmarks
DefaultHubDispatcherBenchmark, BenchmarkDotNet in-process (--config perflab), 2 vCPU AMD EPYC 9V74 VM. Base and PR builds were run in interleaved order for 3 rounds; the table shows the median of those rounds.Per-round means for
Invocation: base 269.2 / 266.4 / 273.0 ns; PR 213.9 / 211.4 / 211.5 ns.Tests
Microsoft.AspNetCore.SignalR.Tests: 496 passed, 1 skipped, 0 failed. This includes the newCustomHubActivatorIsUsedForEveryInvocationtest.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)