Skip to content

Commit bb2fa34

Browse files
perf: use ValueListBuilder for BaseMethodDeclaration
1 parent b3b3acb commit bb2fa34

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/BaseMethodDeclaration.cs

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ public static Doc Print(CSharpSyntaxNode node, PrintingContext context)
8080
semicolonToken = localFunctionStatementSyntax.SemicolonToken;
8181
}
8282

83-
var docs = new List<Doc>();
84-
var declarationGroup = new List<Doc>();
83+
var docs = new ValueListBuilder<Doc>([null, null, null, null, null, null, null, null]);
8584

8685
if (node is LocalFunctionStatementSyntax)
8786
{
88-
docs.Add(ExtraNewLines.Print(node));
87+
docs.Append(ExtraNewLines.Print(node));
8988
}
9089

9190
if (attributeLists is { Count: > 0 })
9291
{
93-
docs.Add(AttributeLists.Print(node, attributeLists.Value, context));
92+
docs.Append(AttributeLists.Print(node, attributeLists.Value, context));
9493

95-
void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
94+
void PrintMethodUnformattedWithoutAttributes(
95+
SyntaxTriviaList syntaxTriviaList,
96+
ref ValueListBuilder<Doc> docs
97+
)
9698
{
9799
var attributeStart = attributeLists
98100
.Value[0]
@@ -110,7 +112,7 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
110112
.ToString()
111113
.Trim();
112114

113-
docs.Add(
115+
docs.Append(
114116
RemoveWhiteSpaceLineEndingsRegex.Replace(
115117
methodWithoutAttributes,
116118
context.Options.LineEnding
@@ -122,21 +124,32 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
122124
{
123125
if (CSharpierIgnore.HasIgnoreComment(modifiers.Value[0]))
124126
{
125-
PrintMethodUnformattedWithoutAttributes(modifiers.Value[0].LeadingTrivia);
126-
return Doc.Group(docs);
127+
PrintMethodUnformattedWithoutAttributes(
128+
modifiers.Value[0].LeadingTrivia,
129+
ref docs
130+
);
131+
var returnDoc = Doc.Group(ref docs);
132+
docs.Dispose();
133+
return returnDoc;
127134
}
128135
}
129136
else if (returnType is not null && CSharpierIgnore.HasIgnoreComment(returnType))
130137
{
131-
PrintMethodUnformattedWithoutAttributes(returnType.GetLeadingTrivia());
132-
return Doc.Group(docs);
138+
PrintMethodUnformattedWithoutAttributes(returnType.GetLeadingTrivia(), ref docs);
139+
var returnDoc = Doc.Group(ref docs);
140+
docs.Dispose();
141+
return returnDoc;
133142
}
134143
}
135144

145+
var declarationGroup = new ValueListBuilder<Doc>(
146+
[null, null, null, null, null, null, null, null]
147+
);
148+
136149
if (modifiers is { Count: > 0 })
137150
{
138-
docs.Add(Token.PrintLeadingTrivia(modifiers.Value[0], context));
139-
declarationGroup.Add(
151+
docs.Append(Token.PrintLeadingTrivia(modifiers.Value[0], context));
152+
declarationGroup.Append(
140153
Modifiers.PrintSorterWithoutLeadingTrivia(modifiers.Value, context)
141154
);
142155
}
@@ -145,30 +158,30 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
145158
{
146159
if (modifiers is not { Count: > 0 })
147160
{
148-
docs.Add(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia(), context));
161+
docs.Append(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia(), context));
149162
context.State.SkipNextLeadingTrivia = true;
150163
}
151164

152-
declarationGroup.Add(Node.Print(returnType, context), " ");
165+
declarationGroup.Append(Node.Print(returnType, context), " ");
153166
context.State.SkipNextLeadingTrivia = false;
154167
}
155168

156169
if (explicitInterfaceSpecifier != null)
157170
{
158-
declarationGroup.Add(
171+
declarationGroup.Append(
159172
Node.Print(explicitInterfaceSpecifier.Name, context),
160173
Token.Print(explicitInterfaceSpecifier.DotToken, context)
161174
);
162175
}
163176

164177
if (identifier != null)
165178
{
166-
declarationGroup.Add(identifier());
179+
declarationGroup.Append(identifier());
167180
}
168181

169182
if (node is ConversionOperatorDeclarationSyntax conversionOperatorDeclarationSyntax)
170183
{
171-
declarationGroup.Add(
184+
declarationGroup.Append(
172185
Token.PrintWithSuffix(
173186
conversionOperatorDeclarationSyntax.ImplicitOrExplicitKeyword,
174187
" ",
@@ -189,7 +202,7 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
189202
}
190203
else if (node is OperatorDeclarationSyntax operatorDeclarationSyntax)
191204
{
192-
declarationGroup.Add(
205+
declarationGroup.Append(
193206
Node.Print(operatorDeclarationSyntax.ReturnType, context),
194207
" ",
195208
operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
@@ -203,24 +216,24 @@ operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
203216

204217
if (typeParameterList != null && typeParameterList.Parameters.Any())
205218
{
206-
declarationGroup.Add(TypeParameterList.Print(typeParameterList, context));
219+
declarationGroup.Append(TypeParameterList.Print(typeParameterList, context));
207220
}
208221

209222
if (parameterList != null)
210223
{
211224
if (parameterList.Parameters.Any())
212225
{
213-
declarationGroup.Add(ParameterList.Print(parameterList, context));
226+
declarationGroup.Append(ParameterList.Print(parameterList, context));
214227
}
215228
else
216229
{
217-
declarationGroup.Add(
230+
declarationGroup.Append(
218231
Token.Print(parameterList.OpenParenToken, context),
219232
Token.Print(parameterList.CloseParenToken, context)
220233
);
221234
}
222235

223-
declarationGroup.Add(Doc.IfBreak(Doc.Null, Doc.SoftLine));
236+
declarationGroup.Append(Doc.IfBreak(Doc.Null, Doc.SoftLine));
224237
}
225238

226239
if (constructorInitializer != null)
@@ -230,7 +243,7 @@ operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
230243
ArgumentList.Print(constructorInitializer.ArgumentList, context)
231244
);
232245

233-
declarationGroup.Add(
246+
declarationGroup.Append(
234247
Doc.Group(
235248
Doc.Indent(Doc.HardLine),
236249
Doc.Indent(colonToken),
@@ -240,30 +253,31 @@ operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
240253
);
241254
}
242255

243-
docs.Add(Doc.Group(declarationGroup));
256+
docs.Append(Doc.Group(ref declarationGroup));
257+
declarationGroup.Dispose();
244258

245259
if (constraintClauses != null)
246260
{
247-
docs.Add(ConstraintClauses.Print(constraintClauses.Value, context));
261+
docs.Append(ConstraintClauses.Print(constraintClauses.Value, context));
248262
}
249263

250264
if (body != null)
251265
{
252-
docs.Add(Block.Print(body, context));
266+
docs.Append(Block.Print(body, context));
253267
}
254268
else
255269
{
256270
if (expressionBody != null)
257271
{
258-
docs.Add(ArrowExpressionClause.Print(expressionBody, context));
272+
docs.Append(ArrowExpressionClause.Print(expressionBody, context));
259273
}
260274
}
261275

262276
if (semicolonToken.HasValue)
263277
{
264-
docs.Add(Token.Print(semicolonToken.Value, context));
278+
docs.Append(Token.Print(semicolonToken.Value, context));
265279
}
266280

267-
return Doc.Group(docs);
281+
return Doc.Group(ref docs);
268282
}
269283
}

Src/CSharpier.Core/DocTypes/Doc.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public static Group GroupWithId(string groupId, params Doc[] contents)
129129

130130
public static Group Group(params Doc[] contents) => new() { Contents = Concat(contents) };
131131

132+
public static Group Group(ref ValueListBuilder<Doc> contents) =>
133+
new() { Contents = Concat(ref contents) };
134+
132135
// prevents allocating an array if there is only a single parameter
133136
public static IndentDoc Indent(Doc contents) => new() { Contents = contents };
134137

0 commit comments

Comments
 (0)