Skip to content

[MVC] Account for CacheTagHelper vary-by key size - #69232

Merged
javiercn merged 2 commits into
mainfrom
javiercn/cachetaghelper-size-accounting
Sep 28, 2026
Merged

javiercn merged 2 commits into
mainfrom
javiercn/cachetaghelper-size-accounting

Conversation

@javiercn

@javiercn javiercn commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #66723

Overview

CacheTagHelper previously counted only rendered HTML toward MemoryCacheEntryOptions.Size, omitting the retained CacheTagKey strings. This change adds UTF-16 payload accounting for the key to both the temporary placeholder and final cache entries, so CacheTagHelperOptions.SizeLimit better reflects retained memory. The change is limited to four Mvc.TagHelpers implementation/test files and preserves the existing 100 MB default.

Design

None. There is no public API, configuration, or default-value change.

The estimate intentionally counts retained string payload rather than attempting to model runtime-specific object and collection overhead. This matches the existing rendered-content accounting, which also counts payload bytes rather than the exact managed object graph.

Implementation

// src/Mvc/Mvc.TagHelpers/src/Cache/CacheTagKey.cs
// Count every dynamic string retained by the in-memory key. Collection entries cover
// cookie, header, query, and route names and values through the same helper.
internal long GetEstimatedSize()
{
    var characterCount = GetStringLength(_prefix) +
        GetStringLength(Key) +
        GetStringLength(_varyBy) +
        GetStringLength(_username) +
        GetStringLength(_requestCulture?.Name) +
        GetStringLength(_requestUICulture?.Name) +
        GetStringCollectionLength(_cookies) +
        GetStringCollectionLength(_headers) +
        GetStringCollectionLength(_queries) +
        GetStringCollectionLength(_routeValues);

    return characterCount * sizeof(char);
}
// src/Mvc/Mvc.TagHelpers/src/CacheTagHelper.cs
// The same immutable key is retained by both cache entries, so both sizes include it.
var cacheKeySize = cacheKey.GetEstimatedSize();
options.SetSize(PlaceholderSize + cacheKeySize);

// (render child content and create the final entry — omitted)
options.SetSize(GetSize(content) + cacheKeySize);

Focused tests cover the two distinct behaviors: CacheTagKeyTest verifies the aggregate estimate across vary-by, user, culture, cookie, header, query, and route strings; CacheTagHelperTest verifies that a concrete header name/value contributes to both actual ICacheEntry.Size assignments.

Outcome

Before the implementation, the faithful mocked IMemoryCache reproduction reached the cache-entry boundary and failed as expected:

Expected placeholder size: 178
Actual placeholder size:   64

The review follow-up test for culture-varying keys also failed before its fix as expected:

Expected aggregate key size: 280
Actual aggregate key size:   260

After the implementation, focused size tests passed:

. .\activate.ps1
.\eng\build.cmd -projects <Mvc.TagHelpers.csproj> -NoBuildDeps -NoRestore -NoBuildNodeJS -NoBuildJava -NoBuildNative
.\eng\build.cmd -test -projects <Mvc.TagHelpers.Test.csproj> -NoBuildDeps -NoRestore -NoBuildNodeJS -NoBuildJava -NoBuildNative /p:TestFilter=Size

Build succeeded. 0 Warning(s), 0 Error(s).

The complete affected unit-test project also passed after the review fix:

. .\activate.ps1
.\eng\build.cmd -test -projects <Mvc.TagHelpers.Test.csproj> -NoBuildDeps -NoRestore -NoBuildNodeJS -NoBuildJava -NoBuildNative

Build succeeded. 0 Warning(s), 0 Error(s).

src/Mvc/build.sh is unsupported on this Windows host (mingw64_nt), so validation used the repository's Windows build.cmd pipeline. A broader MVC-area build is not claimed: its prerequisite traversal required unrelated generated assets and an uninitialized MessagePack-CSharp submodule.

@github-actions github-actions Bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 11, 2026
@javiercn javiercn changed the title Account for CacheTagHelper vary-by key size [MVC] Account for CacheTagHelper vary-by key size Sep 11, 2026
@javiercn
javiercn marked this pull request as ready for review September 14, 2026 10:05
Copilot AI lite review requested due to automatic review settings September 14, 2026 10:05

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.

🟡 Changes recommended

Culture-varying keys omit retained culture-string sizes from the estimate.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Updates MVC CacheTagHelper memory accounting to include retained cache-key string payloads in placeholder and final cache entries.

Changes:

  • Adds estimated UTF-16 key-size calculation.
  • Applies key size to both cache entries.
  • Adds focused unit tests.
File summaries
File Summary
src/Mvc/Mvc.TagHelpers/test/CacheTagKeyTest.cs Tests aggregate key-size calculation.
src/Mvc/Mvc.TagHelpers/test/CacheTagHelperTest.cs Tests placeholder and final entry sizes.
src/Mvc/Mvc.TagHelpers/src/CacheTagHelper.cs Includes key size in cache entry sizes.
src/Mvc/Mvc.TagHelpers/src/Cache/CacheTagKey.cs Estimates retained key-string memory.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@javiercn

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-ci

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@javiercn
javiercn merged commit d699f0f into main Sep 28, 2026
29 checks passed
@javiercn
javiercn deleted the javiercn/cachetaghelper-size-accounting branch September 28, 2026 16:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 1, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CacheTagHelper: MemoryCacheEntryOptions.Size omits vary-by key memory

3 participants