Repository navigation
Reduce per-call overhead in UserValidator - #70224
Conversation
Check allowed user name characters with a SearchValues<char> cached by the configured string instead of a LINQ Any + string.Contains per character, and reuse a single EmailAddressAttribute instance. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The required frozen review bundle was not supplied, preventing verification of the head/base revision identities and existing-feedback snapshot.
No open issues 🎉
What changed in this PR
Reduces per-call overhead in Identity’s UserValidator<TUser> while preserving existing username and email validation behavior.
Changes:
- Caches
SearchValues<char>for allowed username characters on .NET 8+, with a loop-based fallback for older targets. - Reuses a single
EmailAddressAttributeinstance. - Adds coverage for changes to allowed username characters, including non-ASCII characters.
| File | Description |
|---|---|
| src/Identity/test/Identity.Test/UserValidatorTest.cs | Tests that validation honors updated allowed-character settings. |
| src/Identity/Extensions.Core/src/UserValidator.cs | Optimizes character checks and reuses the email validation attribute. |
Review effort: Balanced
|
/review |
Co-authored-by: Copilot <[email protected]>
|
The Failing tests (Windows.Amd64.VS2026.Open, same exception):
Why it's unrelated
I didn't retrigger CI because main E2E is currently flaky. A maintainer can rerun the failed job if needed. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Retriggered CI with an empty commit, so the new head is 0f8298f. The PR diff is unchanged. Failing tests (aspnetcore-ci build 1667824 on e983d13):
Why they're unrelated: This PR only touches Identity's Cross-PR evidence: 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
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, low priority. The change is small and behavior-preserving, but
Reasons to merge
Reasons not to merge yet
What would change this verdict: an Identity owner preferring to keep the simpler code would make this "Don't merge: cold path". 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Reduce per-call overhead in UserValidator
Cache
SearchValuesfor user name checks; reuseEmailAddressAttributeDescription
UserValidator<TUser>.ValidateAsyncruns on everyCreateAsync/UpdateAsyncand on user name and email changes. It had two avoidable costs:userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c)). This allocates a closure and delegate per call. It also re-reads the options and does a linearstring.Containsover the 65-character default set for every character of the user name.new EmailAddressAttribute()was constructed on every call whenRequireUniqueEmailis enabled. The attribute's constructor does a resource lookup for its default error message.Change:
ContainsAnyExcept(SearchValues<char>). TheSearchValuesis cached in a static field, keyed by the configuredAllowedUserNameCharactersstring with an ordinal comparison. If the option changes, the cache is rebuilt, and a new test covers this. The cache is static becauseUserValidatoris registered as scoped. It only holds data derived from configuration, never user input, and isn't reachable by callers. On netstandard2.0/net462, a plain loop withstring.IndexOf(char)replaces the LINQ closure.EmailAddressAttributeis now astatic readonlyinstance.EmailAddressAttribute.IsValidis stateless.AllowedUserNameCharactersstill disables the check.Benchmark
Interleaved A/B on the same machine, 3 rounds. Each round runs
mainthen this PR, using Release builds ofMicrosoft.Extensions.Identity.Core(net12.0) and BenchmarkDotNet's in-process toolchain. The benchmark calls the realUserValidator<TUser>.ValidateAsyncwith aUserManagerover a no-op store whoseFindByName/FindByEmailreturn null. The user name/email is[email protected].That's about −42% time with default options and −40% with
RequireUniqueEmail. Most of the remaining time and allocation is the async state machine and store calls.In isolation on .NET 10, the allowed-characters check for this user name drops from 52.7 ns to 2.5 ns. Constructing and running
EmailAddressAttributedrops from 60 ns / 88 B to 6.7 ns / 0 B. I also measured an uncachedIndexOfAnyExcept(string): it was slower thanmain(79 ns) because it rebuilds a probabilistic map for the 65-character set on every call, which is why theSearchValuesis cached.Benchmark code
Tests
UsesCurrentAllowedUserNameCharacterstest. It changesAllowedUserNameCharactersbetween calls on the same validator, including non-ASCII characters, and checks that the current value is always honored.Microsoft.AspNetCore.Identity.Test: 566 passed, 0 failed, 1 skipped. The 16IdentityUIScriptsTestCDN tests were excluded locally because they need network access the sandbox does not have.Microsoft.AspNetCore.Identity.InMemory.Test(user manager specification tests): 226/226 passed.Microsoft.Extensions.Identity.Corebuilds cleanly for net12.0, netstandard2.0 and net462.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)