Use shared instances of NodeWriter classes#12362
Merged
ToddGrun merged 5 commits intodotnet:mainfrom Oct 21, 2025
Merged
Conversation
Several of the NodeWriter classes don't have state and can have a shared instance used among callers. These allocations aren't huge in the profiles, but the RazorEditingTests.ScrollingAndTypingInCohosting speedometer test shows about 3 MB of allocating these in the CodeAnalysis process.
davidwengier
approved these changes
Oct 18, 2025
...Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DesignTimeNodeWriterTest.cs
Outdated
Show resolved
Hide resolved
...Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DesignTimeNodeWriterTest.cs
Outdated
Show resolved
Hide resolved
src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/TemplateTargetExtensionTest.cs
Outdated
Show resolved
Hide resolved
...er/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/DesignTimeNodeWriter.cs
Show resolved
Hide resolved
...icrosoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/LiteralRuntimeNodeWriter.cs
Show resolved
Hide resolved
...piler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/RuntimeNodeWriter.cs
Show resolved
Hide resolved
...icrosoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/LiteralRuntimeNodeWriter.cs
Outdated
Show resolved
Hide resolved
...piler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/RuntimeNodeWriter.cs
Outdated
Show resolved
Hide resolved
...alysis.Razor.Compiler/src/Language/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriter.cs
Show resolved
Hide resolved
...alysis.Razor.Compiler/src/Language/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriter.cs
Outdated
Show resolved
Hide resolved
...piler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/RuntimeNodeWriter.cs
Outdated
Show resolved
Hide resolved
2) Make classes that expose static Instance have private (or protected) constructors
3) Change from "{ get; => blah; }" syntax to "=> blah;" syntax.
This was referenced Oct 22, 2025
This was referenced Oct 24, 2025
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.
Several of the NodeWriter classes don't have state and can have a shared instance used among callers. These allocations aren't huge in the profiles, but the RazorEditingTests.ScrollingAndTypingInCohosting speedometer test shows about 3 MB of allocating these in the CodeAnalysis process.
Test insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/680496
Toolset run (commit 5): https://dev.azure.com/dnceng/internal/_build/results?buildId=2820760
*** Highlighted allocations of nodewriters previously (ignore the percentatges, as it's filtered to callstacks containing "NodeWriter")
