Skip to content

Commit 066a146

Browse files
committed
Reduce allocations in calls to WithAnnotationsGreen
Each caller changed was allocating more than necessary: 1) List object created 2) If GetAnnotations isn't empty, there was an array allocated in the list 3) The Add calls would always allocate (even when constructed with a non-empty GetAnnotations result) 4) ToArray allocation Instead, there is just a single array allocation. I don't expect this to have a huge effect, but there are 35 MB of List/array of SyntaxAnnotations allocated in the speedometer trace I'm looking at during the typing scenario (about 0.5% of allocations)
1 parent 825b547 commit 066a146

File tree

8 files changed

+8
-60
lines changed

8 files changed

+8
-60
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/MarkupEndTagSyntax.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,5 @@ public bool IsMarkupTransition
2222
}
2323

2424
public MarkupEndTagSyntax AsMarkupTransition()
25-
{
26-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
27-
{
28-
new SyntaxAnnotation(MarkupTransitionKey, new object())
29-
};
30-
31-
var newGreen = this.WithAnnotationsGreen(annotations.ToArray());
32-
33-
return newGreen;
34-
}
25+
=> this.WithAnnotationsGreen([.. GetAnnotations(), new(MarkupTransitionKey, new object())]);
3526
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/MarkupStartTagSyntax.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,5 @@ public bool IsMarkupTransition
2222
}
2323

2424
public MarkupStartTagSyntax AsMarkupTransition()
25-
{
26-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
27-
{
28-
new SyntaxAnnotation(MarkupTransitionKey, new object())
29-
};
30-
31-
var newGreen = this.WithAnnotationsGreen(annotations.ToArray());
32-
33-
return newGreen;
34-
}
25+
=> this.WithAnnotationsGreen([.. GetAnnotations(), new(MarkupTransitionKey, new object())]);
3526
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/RazorDirectiveSyntax.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,5 @@ public DirectiveDescriptor DirectiveDescriptor
2222
}
2323

2424
public RazorDirectiveSyntax WithDirectiveDescriptor(DirectiveDescriptor descriptor)
25-
{
26-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
27-
{
28-
new SyntaxAnnotation(DirectiveDescriptorKey, descriptor)
29-
};
30-
31-
var newGreen = this.WithAnnotationsGreen(annotations.ToArray());
32-
33-
return newGreen;
34-
}
25+
=> this.WithAnnotationsGreen([.. GetAnnotations(), new(DirectiveDescriptorKey, descriptor)]);
3526
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/MarkupMinimizedTagHelperAttributeSyntax.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public TagHelperAttributeInfo TagHelperAttributeInfo
2222

2323
public MarkupMinimizedTagHelperAttributeSyntax WithTagHelperAttributeInfo(TagHelperAttributeInfo info)
2424
{
25-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
26-
{
27-
new SyntaxAnnotation(TagHelperAttributeInfoKey, info)
28-
};
29-
30-
var newGreen = Green.WithAnnotationsGreen(annotations.ToArray());
25+
var newGreen = Green.WithAnnotationsGreen([.. GetAnnotations(), new(TagHelperAttributeInfoKey, info)]);
3126

3227
return (MarkupMinimizedTagHelperAttributeSyntax)newGreen.CreateRed(Parent, Position);
3328
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/MarkupMinimizedTagHelperDirectiveAttributeSyntax.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ public string FullName
3535

3636
public MarkupMinimizedTagHelperDirectiveAttributeSyntax WithTagHelperAttributeInfo(TagHelperAttributeInfo info)
3737
{
38-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
39-
{
40-
new SyntaxAnnotation(TagHelperAttributeInfoKey, info)
41-
};
42-
43-
var newGreen = Green.WithAnnotationsGreen(annotations.ToArray());
38+
var newGreen = Green.WithAnnotationsGreen([.. GetAnnotations(), new(TagHelperAttributeInfoKey, info)]);
4439

4540
return (MarkupMinimizedTagHelperDirectiveAttributeSyntax)newGreen.CreateRed(Parent, Position);
4641
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/MarkupTagHelperAttributeSyntax.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public TagHelperAttributeInfo TagHelperAttributeInfo
2222

2323
public MarkupTagHelperAttributeSyntax WithTagHelperAttributeInfo(TagHelperAttributeInfo info)
2424
{
25-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
26-
{
27-
new SyntaxAnnotation(TagHelperAttributeInfoKey, info)
28-
};
29-
30-
var newGreen = Green.WithAnnotationsGreen(annotations.ToArray());
25+
var newGreen = Green.WithAnnotationsGreen([.. GetAnnotations(), new(TagHelperAttributeInfoKey, info)]);
3126

3227
return (MarkupTagHelperAttributeSyntax)newGreen.CreateRed(Parent, Position);
3328
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/MarkupTagHelperDirectiveAttributeSyntax.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ public string FullName
3535

3636
public MarkupTagHelperDirectiveAttributeSyntax WithTagHelperAttributeInfo(TagHelperAttributeInfo info)
3737
{
38-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
39-
{
40-
new SyntaxAnnotation(TagHelperAttributeInfoKey, info)
41-
};
42-
43-
var newGreen = Green.WithAnnotationsGreen(annotations.ToArray());
38+
var newGreen = Green.WithAnnotationsGreen([.. GetAnnotations(), new(TagHelperAttributeInfoKey, info)]);
4439

4540
return (MarkupTagHelperDirectiveAttributeSyntax)newGreen.CreateRed(Parent, Position);
4641
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/RazorDirectiveSyntax.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public DirectiveDescriptor DirectiveDescriptor
2222

2323
public RazorDirectiveSyntax WithDirectiveDescriptor(DirectiveDescriptor descriptor)
2424
{
25-
var annotations = new List<SyntaxAnnotation>(GetAnnotations())
26-
{
27-
new SyntaxAnnotation(DirectiveDescriptorKey, descriptor)
28-
};
29-
30-
var newGreen = Green.WithAnnotationsGreen(annotations.ToArray());
25+
var newGreen = Green.WithAnnotationsGreen([.. GetAnnotations(), new(DirectiveDescriptorKey, descriptor)]);
3126

3227
return (RazorDirectiveSyntax)newGreen.CreateRed(Parent, Position);
3328
}

0 commit comments

Comments
 (0)