Improve Annotation and SpanContext performance. (dotnet/aspnetcore-tooling#1881)

The razor perf test shows about 70 ms CPU of WithSpanContext is in allocation. GetAnnotation similarly is showing about 60 ms in allocation (of which this only partly improves)\n\nCommit migrated from a060f129ff
This commit is contained in:
Todd Grunke 2020-05-11 10:27:40 -07:00 committed by GitHub
parent d1e7d8e466
commit 485924edd2
2 changed files with 25 additions and 8 deletions

View File

@ -67,17 +67,29 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
var newAnnotation = new SyntaxAnnotation(SyntaxConstants.SpanContextKind, spanContext);
var newAnnotations = new List<SyntaxAnnotation>();
newAnnotations.Add(newAnnotation);
foreach (var annotation in node.GetAnnotations())
{
if (annotation.Kind != newAnnotation.Kind)
{
newAnnotations.Add(annotation);
List<SyntaxAnnotation> newAnnotations = null;
if (node.ContainsAnnotations)
{
var existingNodeAnnotations = node.GetAnnotations();
for (int i = 0; i < existingNodeAnnotations.Length; i++)
{
var annotation = existingNodeAnnotations[i];
if (annotation.Kind != newAnnotation.Kind)
{
if (newAnnotations == null)
{
newAnnotations = new List<SyntaxAnnotation>();
newAnnotations.Add(newAnnotation);
}
newAnnotations.Add(annotation);
}
}
}
var newAnnotationsArray = newAnnotations == null ? new[] { newAnnotation } : newAnnotations.ToArray();
return node.WithAnnotations(newAnnotations.ToArray());
return node.WithAnnotations(newAnnotationsArray);
}
public static SyntaxNode LocateOwner(this SyntaxNode node, SourceChange change)

View File

@ -28,6 +28,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Syntax
throw new ArgumentNullException(nameof(node));
}
if (!node.ContainsAnnotations)
{
return null;
}
var annotation = node.GetAnnotations().FirstOrDefault(n => n.Kind == key);
return annotation?.Data;
}