Skip to content

Commit c82e88e

Browse files
authored
Reduce allocations in TagHelperParseTreeRewriter.Rewrite (#11882)
Customer profile is showing a very large number of allocations in the GetAllDiagnostics call, due to the yield state machinery. Add internal method to TagHelperDescriptor that allows callers to pass in a PooledArrayBuilder for the diagnostics to be added directly to, instead of using "return yield". Partial fix for dotnet/roslyn#78639 See PR for more allocation information
1 parent 955c6f1 commit c82e88e

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/TagHelperParseTreeRewriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ public static RazorSyntaxTree Rewrite(RazorSyntaxTree syntaxTree, TagHelperBinde
3636

3737
foreach (var descriptor in binder.Descriptors)
3838
{
39-
foreach (var diagnostic in descriptor.GetAllDiagnostics())
40-
{
41-
builder.Add(diagnostic);
42-
}
39+
descriptor.AppendAllDiagnostics(ref builder.AsRef());
4340
}
4441

4542
var diagnostics = builder.ToImmutableOrderedBy(static d => d.Span.AbsoluteIndex);

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/TagHelperDescriptor.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,35 +173,35 @@ static ImmutableArray<BoundAttributeDescriptor> GetEditorRequiredAttributes(Immu
173173
}
174174

175175
public IEnumerable<RazorDiagnostic> GetAllDiagnostics()
176+
{
177+
using var diagnostics = new PooledArrayBuilder<RazorDiagnostic>();
178+
179+
AppendAllDiagnostics(ref diagnostics.AsRef());
180+
181+
foreach (var diagnostic in diagnostics)
182+
{
183+
yield return diagnostic;
184+
}
185+
}
186+
187+
internal void AppendAllDiagnostics(ref PooledArrayBuilder<RazorDiagnostic> diagnostics)
176188
{
177189
foreach (var allowedChildTag in AllowedChildTags)
178190
{
179-
foreach (var diagnostic in allowedChildTag.Diagnostics)
180-
{
181-
yield return diagnostic;
182-
}
191+
diagnostics.AddRange(allowedChildTag.Diagnostics);
183192
}
184193

185194
foreach (var boundAttribute in BoundAttributes)
186195
{
187-
foreach (var diagnostic in boundAttribute.Diagnostics)
188-
{
189-
yield return diagnostic;
190-
}
196+
diagnostics.AddRange(boundAttribute.Diagnostics);
191197
}
192198

193199
foreach (var tagMatchingRule in TagMatchingRules)
194200
{
195-
foreach (var diagnostic in tagMatchingRule.Diagnostics)
196-
{
197-
yield return diagnostic;
198-
}
201+
diagnostics.AddRange(tagMatchingRule.Diagnostics);
199202
}
200203

201-
foreach (var diagnostic in Diagnostics)
202-
{
203-
yield return diagnostic;
204-
}
204+
diagnostics.AddRange(Diagnostics);
205205
}
206206

207207
public override string ToString()

0 commit comments

Comments
 (0)