Repository navigation
Conversation
When every registered view location expander is known not to populate values (the default PageViewLocationExpander), the expander values are not part of the cache key, so the expander context and its values dictionary are only created on a cache miss. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The change introduces a cross-assembly dependency on an internal Mvc.Razor type via a friend-assembly grant, requiring an assembly-local implementation or a supported public contract.
1 open issue
What changed in this PR
This PR reduces allocations during Razor view-location cache hits by deferring expander context creation when no registered expander contributes cache-key values.
Changes:
- Adds an internal capability marker and opts in the exact
PageViewLocationExpandertype. - Defers context allocation until a cache miss while preserving value population for other expanders.
- Adds targeted tests and a warmed-cache partial-view benchmark.
| File | Description |
|---|---|
src/Mvc/perf/Microbenchmarks/Microsoft.AspNetCore.Mvc/ViewEngineBenchmark.cs |
Benchmarks cached partial-view lookups with default MVC services. |
src/Mvc/perf/Microbenchmarks/Microsoft.AspNetCore.Mvc.Views/Views/Shared/_ViewEngineBenchmarkPartial.cshtml |
Supplies the benchmark’s partial view. |
src/Mvc/Mvc.RazorPages/test/Infrastructure/PageViewLocationExpanderTest.cs |
Tests exact-type opt-in and derived-type exclusion. |
src/Mvc/Mvc.RazorPages/src/Infrastructure/PageViewLocationExpander.cs |
Declares the exact type as contributing no expander values. |
src/Mvc/Mvc.Razor/test/RazorViewEngineTest.cs |
Tests valueless and mixed-expander cache behavior. |
src/Mvc/Mvc.Razor/src/RazorViewEngine.cs |
Defers context creation for eligible cache lookups. |
src/Mvc/Mvc.Razor/src/IViewLocationExpanderWithoutValues.cs |
Defines the internal capability contract. |
Review effort: Balanced
| /// An <see cref="IViewLocationExpander"/> for PageView. | ||
| /// </summary> | ||
| public class PageViewLocationExpander : IViewLocationExpander | ||
| public class PageViewLocationExpander : IViewLocationExpander, IViewLocationExpanderWithoutValues |
There was a problem hiding this comment.
Thanks for the review. I looked into restructuring this, but I'd like to keep the current shape, for these reasons:
- The pattern already ships between these two assemblies.
Mvc.Razordefinesinternal interface IModelTypeProvider(src/Mvc/Mvc.Razor/src/IModelTypeProvider.cs). The public RazorPages typeRazorPageAdapterimplements it (src/Mvc/Mvc.RazorPages/src/Infrastructure/RazorPageAdapter.cs:15), andRazorPageActivator.cs:71type-tests for it. This PR uses the same pattern: an internal capability interface in Mvc.Razor, implemented explicitly by a public RazorPages type and type-tested by Mvc.Razor.Mvc.RazorPagesalso already uses internalMvc.RazortypesRazorPagePropertyActivator,ViewPathandRazorFileHierarchythrough the sameInternalsVisibleTogrant. - There is no version-skew risk. Both projects are
IsAspNetCoreApp=true/IsPackable=falseand ship only together in Microsoft.AspNetCore.App (eng/SharedFramework.Local.props:89-90). - The optimization can't stay inside one assembly without losing the win.
PageViewLocationExpanderlives in RazorPages, and Mvc.Razor can't reference RazorPages. Without a marker,RazorViewEnginemust assume that any registered expander populates values, which is exactly the caseAddMvcandAddRazorPageshit. Keeping the logic in Mvc.Razor alone would only help apps with zero expanders and would drop most of the 152 B per lookup. A public capability contract would mean a new public API (and an API proposal) for a framework-internal optimization, which this PR deliberately avoids.
The marker is behavior-safe: it applies only when GetType() == typeof(PageViewLocationExpander), so a subclass that overrides PopulateValues keeps the existing path (covered by DoesNotPopulateValues_TrueOnlyForExactType).
I'm leaving this thread open for a maintainer to decide. If you'd prefer the in-assembly-only version (or a different internal mechanism), I'm happy to change it.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)
There was a problem hiding this comment.
Thanks for flagging this. This PR is being closed because the approach relies on a new cross-assembly internal (InternalsVisibleTo) dependency, which the repo's copilot-instructions forbid, and a version without it showed no measurable gain.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/review |
|
Closing this PR. The win depends on a new internal interface in Mvc.Razor that Mvc.RazorPages implements through InternalsVisibleTo, and the repo's copilot-instructions forbid adding new cross-assembly internal dependencies. A version without that dependency showed no measurable gain (about 201–210 ns and 856 B for both the baseline and the variant), because AddMvc always registers PageViewLocationExpander. Thanks to everyone who reviewed. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |

Summary
RazorViewEngine.LocatePageFromViewLocationsruns for every view, partial, layout and_ViewStartlookup. Before this change, every lookup allocated aViewLocationExpanderContextand aDictionary<string, string?>for expander values, even on a cache hit, so that the expanders could populate values for the cache key.The default
RazorPagesregistration addsPageViewLocationExpander, whosePopulateValuesdoes nothing. With this change:IViewLocationExpanderWithoutValues, identifies expanders that never populate values.PageViewLocationExpanderimplements the marker only whenGetType() == typeof(PageViewLocationExpander). A subclass might overridePopulateValues, so subclasses keep the old behavior.ExpandViewLocations, is then created only on a cache miss.There are no public API changes.
ViewLocationCacheKeyis internal, so the change in its key contents is not observable.Benchmark
ViewEngineBenchmark.FindPartial: a cache-hit partial lookup withAddMvc()defaults, which includePageViewLocationExpander. Three interleaved A/B rounds with BenchmarkDotNet:That is −152 B (−18%) per view, partial or layout lookup. Time is flat to about −5%, within the noise. A typical page render does several lookups (view, layout and each partial), so the saving repeats on each one.
Tests
RazorViewEngineTesttests:PageViewLocationExpanderTesttest: the marker is true only for the exact type, not for derived types.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)