Repository navigation
Conversation
Build the validator lists at their final size and enumerate the token provider map once instead of re-looking each key up. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
davidfowl
requested review from
Youssef1313,
cincuranet,
davpetr and
rokonec
as code owners
November 1, 2026 15:11
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Validator collections are now null when the constructor invokes the virtual Logger setter, causing derived setters that access them to throw NullReferenceException during construction.
1 open issue
What changed in this PR
Reduces per-scope UserManager<TUser> construction overhead in Identity, complementing the metrics and logger caching changes in #70174 and #70175.
Changes:
- Constructs validator lists directly from injected collections instead of adding items individually.
- Enumerates token provider key/value pairs to avoid separate dictionary lookups.
| File | Description |
|---|---|
src/Identity/Extensions.Core/src/UserManager.cs |
Optimizes validator collection initialization and token provider enumeration. |
Review effort: Balanced
Initialize the lists in field initializers and bulk-add the injected validators with an exact capacity, so derived Logger setters still see non-null collections. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
|
/review |
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Member
Author
|
Tiny gain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Reduce UserManager construction cost
Reduce allocations and work when constructing
UserManager<TUser>Description
Make
UserManager<TUser>cheaper to construct. It's scoped and gets constructed on every request that resolvesSignInManager/ISecurityStampValidator, which includes every cookie-authenticated request throughSecurityStampValidator.ValidatePrincipalAsync.UserValidatorsandPasswordValidatorsare added with oneAddRangeinto lists whose capacity is set to the collection count, which is known when DI injects an array. Before, aforeachoverIEnumerable<T>boxed an enumerator and grew each list to the default capacity of 4 throughAdd. The lists are still created by field initializers, so they exist before the constructor's virtualLoggersetter runs.ProviderMapis enumerated once as key/value pairs. Before, the code enumeratedKeysand then looked each key up with the indexer.There is no behavior change. Validator order, contents, mutability (
IList<T>backed byList<T>), the null-collection handling and token provider registration order are all unchanged. There are no public API changes.Benchmark
Local BenchmarkDotNet run (MemoryDiagnoser, in-process). The baseline and PR builds of
Microsoft.Extensions.Identity.Core.dllran in separate shared-framework copies, interleaved A, B, A, B over 3 rounds. The benchmark resolves the service in a new DI scope withAddIdentityCore<User>().AddSignInManager().AddDefaultTokenProviders(), a no-op store andAddLogging().UserManager<TUser>UserManager<TUser>UserManager<TUser>ISecurityStampValidatorISecurityStampValidatorISecurityStampValidatorEvery resolve that constructs a
UserManagerallocates about 104 B less.UserManagerresolve time was about 5% lower in two rounds and about 1% lower in the third. TheISecurityStampValidatorresolve times are within noise; only its allocation goes down. In an isolated benchmark of the token provider loop with 4 default providers, the loop alone took 56.1 ns on main and 34.5 ns with this PR.This complements #70174, which caches the metrics objects, and #70175, which caches the logger. Those PRs touch different lines, and this one does not overlap them.
Tests
I added
UserManagerTest.ConstructorCopiesValidatorsInOrderAndCollectionsAreAvailableToVirtualMembers, which covers validator order, mutability, null inputs and a derivedLoggersetter that reads the collections. With the final commit, the UserManager, SignInManager, SecurityStamp and claims factory tests pass: 208 passed, 1 skipped. On the first commit, the fullMicrosoft.AspNetCore.Identity.Testrun had 565 passed and 1 skipped. The 16 failures were allIdentityUIScriptsTestCDN tests (*_Matches_CDNContent,*_SubresourceIntegrityCheck), which need outbound internet access that isn't available in the environment where I ran them. They don't involve this change.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)