Repository navigation
Conversation
UserManager and SignInManager are scoped and constructed on every request that uses Identity (e.g. SecurityStampValidator). Each construction created a new metrics object that re-resolved the meter from the factory and re-created all instruments. Meters and instruments are already cached by the meter factory and meter, so cache the metrics wrapper per IMeterFactory in a private ConditionalWeakTable. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the per-factory metrics caching change.
No open issues 🎉
What changed in this PR
Reduces repeated metrics setup during scoped Identity manager construction by caching metrics instances per IMeterFactory, without changing public APIs.
Changes:
- Adds weak-key caches for
UserManagerMetricsandSignInManagerMetrics. - Updates both managers to reuse cached metrics instances.
- Adds tests for recording through shared factories and isolating UserManager metrics across different factories.
| File | Description |
|---|---|
src/Identity/test/Identity.Test/UserManagerTest.cs |
Tests shared-factory recording and separate-factory isolation. |
src/Identity/test/Identity.Test/SignInManagerTest.cs |
Tests recording from multiple managers sharing a factory. |
src/Identity/Extensions.Core/src/UserManagerMetrics.cs |
Adds a weak-key, per-factory metrics cache. |
src/Identity/Extensions.Core/src/UserManager.cs |
Uses the cached UserManager metrics instance. |
src/Identity/Core/src/SignInManagerMetrics.cs |
Adds a weak-key, per-factory metrics cache. |
src/Identity/Core/src/SignInManager.cs |
Uses the cached SignInManager metrics instance. |
Review effort: Balanced
|
/review |
There was a problem hiding this comment.
Source-only review of frozen head 5c5d461e4067b8a47e88e2b6b80d7112020d8805 found one new P3 test-coverage issue: the added shared-factory tests do not distinguish cached metrics wrappers from the previous per-manager construction.
The prepared existing feedback contained no equivalent finding; no candidates remain unresolved. docs/CrossCuttingGuidance.md completed with all in-scope checks assessed by the required independent gpt-5.6-sol worker. Runtime/test execution and security assessment were explicitly excluded. No Identity-specific specialist guide was routed, so that domain remains uncovered beyond cross-cutting review.
Provenance: PR 70174, merge base/base tip 4f556f73a610b386e5ec29355a300e493d30c4c5, immutable reviewer guidance from 4f556f73a610b386e5ec29355a300e493d30c4c5. Proof is source-only; target code, tests, CI, post-preparation feedback, and symlink/submodule/LFS targets were not observed.
Generated by ASP.NET Core Pull Request Review for #70174 · copilot · gpt56 · 140.5 AIC · ⌖ 11.4 AIC · ⊞ 20.2K
Comment /review to run again
…per factory Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Retriggered CI with an empty commit (3385859) because the only failure on the previous run looks like an unrelated flake.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
# Conflicts: # src/Security/perf/Microbenchmarks/Microsoft.AspNetCore.Security.Microbenchmarks.csproj
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 assessmentVerdict: Lean merge. 20 production lines remove ~1 µs and 2,160 B from every scope that builds
Reasons to merge
Reasons not to merge yet
What would change this verdict: evidence that an 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Cache Identity UserManager/SignInManager metrics per IMeterFactory
Avoid rebuilding Identity metrics per scoped UserManager/SignInManager
Description
Avoid rebuilding the Identity metrics objects on every scoped
UserManager/SignInManagerconstruction.UserManager<TUser>andSignInManager<TUser>are registered as scoped, so they are constructed per request whenever Identity is used. That includes every cookie-authenticated request that reachesSecurityStampValidator. Each constructor created a newUserManagerMetrics/SignInManagerMetrics, which:IMeterFactory.Create(...), which allocatesMeterOptionsand does a lookup in the factory's cache, andMeter.Create*looks up and returns the already-existing instrument, but it still allocates advice and other options objects each time.Both the meter (per factory) and the instruments (per meter) are already cached, so every instance built from the same
IMeterFactoryis functionally identical.Change: each metrics class gets a static
GetOrCreate(IMeterFactory)backed by aprivate static ConditionalWeakTable<IMeterFactory, T>.Benchmark
Local BenchmarkDotNet run, A/B against
main, 3 rounds. The benchmark resolves the manager in a new DI scope withAddIdentityCore<object>().AddSignInManager().AddDefaultTokenProviders()and a no-op store.That's about −37% time and −34% allocations for UserManager, and about −45% time and −41% allocations (−2,160 B per scope) for SignInManager.
Benchmark code
Tests
MultipleUserManagersWithSameMeterFactory_RecordMetricsandUserManagersWithDifferentMeterFactories_RecordMetricsToOwnFactoryinUserManagerTest.MultipleSignInManagersWithSameMeterFactory_RecordMetricsinSignInManagerTest.Microsoft.AspNetCore.Identity.Test: 568 passed. The only failures were the 16IdentityUIScriptsTestCDN tests, which need network access that the local sandbox doesn't have.Microsoft.AspNetCore.Identity.FunctionalTests(MapIdentityApiTests, including the metrics tests): 81/81 passed.Benchmark (3 interleaved rounds)
New
src/Security/perf/Microbenchmarks/IdentityManagerResolveBenchmark.cs(same setup as the code above). Release builds.Averaged over rounds: about −34% time / −1,472 B for
UserManagerand −42% time / −2,160 B forSignInManager.Machine: AMD EPYC 9V74, 2 cores, Linux; .NET SDK 12.0.100-alpha.1.26521.103, .NET 12.0.0 (12.0.26.52911) X64 RyuJIT, Server GC. BenchmarkDotNet in-process toolchain,
--warmupCount 3 --iterationCount 10,[MemoryDiagnoser]. "main" is this PR's merge base (4f556f7); both builds use identical benchmark source. Runs were interleaved: main, PR, main, PR, main, PR.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)