Skip to content

Lazily allocate TemplateInfo visited objects set - #70130

Merged
davidfowl merged 2 commits into
mainfrom
perf/templateinfo-lazy
Nov 16, 2026
Merged

davidfowl merged 2 commits into
mainfrom
perf/templateinfo-lazy

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Lazily allocate TemplateInfo visited objects set

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable. (Existing tests cover these paths; behavior is unchanged.)
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Allocate TemplateInfo's visited-objects set only when rendering templates

Description

Each ViewDataDictionary creates a TemplateInfo, and there's one dictionary per controller instance, view, partial and template. TemplateInfo allocated a HashSet<object> up front, and its copy constructor allocated another for each nested ViewDataDictionary. The set only stops recursion while display and editor templates render, so most instances never use it.

With this change, AddVisited creates the set on its first call. The copy constructor copies the set only when the original has entries, and TemplateDepth and Visited handle 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.

Benchmark Before After
Partial view benchmarks (alloc) 176 KB/op 163 KB/op (−7.4%)
ViewResult (alloc) −130 B/op

Timings didn't change beyond noise.

Tests

  • Microsoft.AspNetCore.Mvc.ViewFeatures.Test: 2592 passed, 0 failed

No issue exists for this. It came out of a perf review of the MVC request pipeline.

Benchmark (3 interleaved rounds)

This PR now adds RazorViewRenderBenchmark to src/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 nested ViewDataDictionary and TemplateInfo.

Round Benchmark main Mean PR Mean Δ time main Allocated PR Allocated
1 RenderView [View=HelperPartialAsync] 90.74 µs 90.50 µs -0.3% 177,409 B 164,480 B
1 RenderView [View=HelperPartialTagHelper] 128.47 µs 123.36 µs -4.0% 212,737 B 199,809 B
1 RenderView [View=HelperTyped] 12.42 µs 12.14 µs -2.3% 20,536 B 20,408 B
2 RenderView [View=HelperPartialAsync] 92.09 µs 92.68 µs +0.6% 177,408 B 164,480 B
2 RenderView [View=HelperPartialTagHelper] 124.20 µs 127.68 µs +2.8% 212,737 B 199,809 B
2 RenderView [View=HelperTyped] 12.48 µs 12.08 µs -3.2% 20,536 B 20,408 B
3 RenderView [View=HelperPartialAsync] 94.73 µs 86.41 µs -8.8% 177,408 B 164,480 B
3 RenderView [View=HelperPartialTagHelper] 126.67 µs 123.38 µs -2.6% 212,736 B 199,809 B
3 RenderView [View=HelperTyped] 12.25 µs 12.01 µs -2.0% 20,536 B 20,408 B

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). main is the PR's merge base 4f556f73a6. PR is the same tree with only this PR's src/**/src changes 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)

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>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 21:00
@github-actions github-actions Bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 AddVisited call.
  • Copies the set only when the original contains entries.
  • Handles an unallocated set in TemplateDepth and Visited.
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

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
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>
@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026
@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Nov 13, 2026
@davidfowl
davidfowl merged commit b403943 into main Nov 16, 2026
50 checks passed
@davidfowl
davidfowl deleted the perf/templateinfo-lazy branch November 16, 2026 23:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants