Cache uncached AppContext.TryGetSwitch calls#66513
Draft
pavelsavara wants to merge 2 commits intodotnet:mainfrom
Draft
Cache uncached AppContext.TryGetSwitch calls#66513pavelsavara wants to merge 2 commits intodotnet:mainfrom
pavelsavara wants to merge 2 commits intodotnet:mainfrom
Conversation
Youssef1313
reviewed
Apr 28, 2026
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.
Fixes #66280
AppContext.TryGetSwitchinvolves a dictionary lookup on every call. Several call sites in the repo were not caching the result, causing repeated lookups on hot paths (e.g., per-request in middleware).This PR caches all previously-uncached
AppContext.TryGetSwitchcalls intostatic readonlyfields orstaticauto-property initializers ({ get; } =), so the switch is read once at static initialization time.Breaking change
If someone was calling
AppContext.SetSwitchon the feature switches during execution, this is a breaking change.I think that those features are designed to be configured at compile time and not changed during execution.
I'm advocating to make this breaking change.
Changes
Server-side (no trimmer interaction)
AuthorizationMiddleware— cachedMicrosoft.AspNetCore.Authorization.SuppressUseHttpContextAsAuthorizationResourceinto astatic readonly boolfield. This was the most impactful case since it ran on every HTTP request through the authorization pipeline.SignInManager<TUser>— cachedMicrosoft.AspNetCore.Identity.CheckPasswordSignInAlwaysResetLockoutOnSuccessinto astatic readonly boolfield.DefaultEditorTemplates— cachedSwitch.Microsoft.AspNetCore.Mvc.UsePasswordValueinto astatic readonly boolfield.Components (trimmer/NAOT/WASM-safe)
These properties already had
[FeatureSwitchDefinition]attributes or ILLink substitution entries, which let the trimmer replace the getter with a constant. The change from expression-bodied (=>) to auto-property initializer ({ get; } =) caches the value at runtime (when not trimmed) without affecting trimmer behavior.RemoteNavigationManager._throwNavigationExceptionHttpNavigationManager._throwNavigationExceptionHotReloadManager.IsSupported(shared source, compiled into 6 assemblies)RegexConstraintSupport.IsEnabled