Repository navigation
Lazily allocate TemplateInfo visited objects set - #70130
Merged
Merged
Conversation
Every ViewDataDictionary (one per Controller instance, view, partial and template) creates a TemplateInfo, which eagerly allocated a HashSet<object> and copied it for each nested ViewDataDictionary. The set is only used when rendering display/editor templates, so allocate it on first use. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
davidfowl
requested review from
a team,
Youssef1313,
cincuranet,
davpetr and
rokonec
as code owners
October 30, 2026 21:00
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Missing frozen head/base identities and the required prior-feedback bundle prevent establishing review provenance and deduplicating feedback.
No open issues 🎉
What changed in this PR
Reduces MVC view-data allocations by creating TemplateInfo’s recursion-tracking set only when needed, without changing its public API.
Changes:
- Allocates the visited-object set on the first
AddVisitedcall. - Copies the set only when the original contains entries.
- Handles an unallocated set in
TemplateDepthandVisited.
| File | Description |
|---|---|
src/Mvc/Mvc.ViewFeatures/src/TemplateInfo.cs |
Lazily allocates recursion-tracking storage and updates copying and lookups to handle an unallocated set. |
Review effort: Balanced
Member
|
/review |
Renders compiled views (including 100 partials) end to end per operation without [IterationSetup], so time and allocations are stable for A/B comparisons. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
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.
Lazily allocate TemplateInfo visited objects set
Allocate TemplateInfo's visited-objects set only when rendering templates
Description
Each
ViewDataDictionarycreates aTemplateInfo, and there's one dictionary per controller instance, view, partial and template.TemplateInfoallocated aHashSet<object>up front, and its copy constructor allocated another for each nestedViewDataDictionary. The set only stops recursion while display and editor templates render, so most instances never use it.With this change,
AddVisitedcreates the set on its first call. The copy constructor copies the set only when the original has entries, andTemplateDepthandVisitedhandle a null set. Behavior and public API are unchanged.Results
These numbers come from local BenchmarkDotNet runs (MemoryDiagnoser) on a noisy 2-vCPU Linux VM. Baseline and patched builds ran in interleaved A/B rounds.
ViewResult(alloc)Timings didn't change beyond noise.
Tests
Microsoft.AspNetCore.Mvc.ViewFeatures.Test: 2592 passed, 0 failedNo issue exists for this. It came out of a perf review of the MVC request pipeline.
Benchmark (3 interleaved rounds)
This PR now adds
RazorViewRenderBenchmarktosrc/Mvc/perf/Microbenchmarks/Microsoft.AspNetCore.Mvc. It's the same benchmark as in #70128. Each operation resolves and renders a compiled view, and every partial creates a nestedViewDataDictionaryandTemplateInfo.Result: allocations drop in every round, by 12,928 B/op on both 100-partial views (−7.3% and −6.1%) and by 128 B/op on
HelperTyped. Time is within noise (−8.8% to +2.8%, with no consistent direction for the partial views). Treat this as an allocation-only improvement.Setup: BenchmarkDotNet v0.13.0
--config perflab(InProcess, MemoryDiagnoser, 3 warmup + 15 measured iterations); AMD EPYC 9V74, 2 vCPU, Ubuntu 24.04 container; .NET SDK 12.0.100-alpha.1.26521.103, .NET 12.0.0 runtime (X64 RyuJIT).mainis the PR's merge base4f556f73a6.PRis the same tree with only this PR'ssrc/**/srcchanges applied. Both builds use the same benchmark source. Run order: main, PR, main, PR, main, PR.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)