Skip to content

Commit b47f0f9

Browse files
authored
Reduce allocations in calls to WithAnnotationsGreen (#11927)
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) ![image](https://github.com/user-attachments/assets/8554dec3-31be-4a95-92c7-c69b75768789) ... ![image](https://github.com/user-attachments/assets/02f659ee-1374-4ba8-a3df-b33c6b62c30e)
2 parents 34cdd3b + de5ae11 commit b47f0f9

File tree

8 files changed

+8
-70
lines changed

8 files changed

+8
-70
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#nullable disable
55

6-
using System.Collections.Generic;
7-
86
namespace Microsoft.AspNetCore.Razor.Language.Syntax;
97

108
internal sealed partial class MarkupMinimizedTagHelperAttributeSyntax
@@ -22,12 +20,7 @@ public TagHelperAttributeInfo TagHelperAttributeInfo
2220

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

3225
return (MarkupMinimizedTagHelperAttributeSyntax)newGreen.CreateRed(Parent, Position);
3326
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#nullable disable
55

6-
using System.Collections.Generic;
7-
86
namespace Microsoft.AspNetCore.Razor.Language.Syntax;
97

108
internal sealed partial class MarkupMinimizedTagHelperDirectiveAttributeSyntax
@@ -35,12 +33,7 @@ public string FullName
3533

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

4538
return (MarkupMinimizedTagHelperDirectiveAttributeSyntax)newGreen.CreateRed(Parent, Position);
4639
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#nullable disable
55

6-
using System.Collections.Generic;
7-
86
namespace Microsoft.AspNetCore.Razor.Language.Syntax;
97

108
internal sealed partial class MarkupTagHelperAttributeSyntax
@@ -22,12 +20,7 @@ public TagHelperAttributeInfo TagHelperAttributeInfo
2220

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

3225
return (MarkupTagHelperAttributeSyntax)newGreen.CreateRed(Parent, Position);
3326
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#nullable disable
55

6-
using System.Collections.Generic;
7-
86
namespace Microsoft.AspNetCore.Razor.Language.Syntax;
97

108
internal sealed partial class MarkupTagHelperDirectiveAttributeSyntax
@@ -35,12 +33,7 @@ public string FullName
3533

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

4538
return (MarkupTagHelperDirectiveAttributeSyntax)newGreen.CreateRed(Parent, Position);
4639
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#nullable disable
55

6-
using System.Collections.Generic;
7-
86
namespace Microsoft.AspNetCore.Razor.Language.Syntax;
97

108
internal sealed partial class RazorDirectiveSyntax
@@ -22,12 +20,7 @@ public DirectiveDescriptor DirectiveDescriptor
2220

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

3225
return (RazorDirectiveSyntax)newGreen.CreateRed(Parent, Position);
3326
}

0 commit comments

Comments
 (0)