Skip to content

Commit 7c5ef07

Browse files
perf: change RemoveAt to set Contents to Doc.Null
1 parent 9c0994b commit 7c5ef07

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

Src/CSharpier/DocTypes/Doc.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ public static Doc Concat(List<Doc> contents) =>
5656

5757
public static Doc Concat(params Doc[] contents) => new Concat(contents);
5858

59+
public static Doc Concat(ref ValueListBuilder<Doc> contents)
60+
{
61+
return contents.Length switch
62+
{
63+
0 => Null,
64+
1 => contents[0],
65+
_ => Concat(contents.AsSpan().ToArray()),
66+
};
67+
}
68+
5969
public static Doc Join(Doc separator, IEnumerable<Doc> enumerable)
6070
{
6171
var docs = new List<Doc>();

Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CompilationUnit.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ public static Doc Print(CompilationUnitSyntax node, PrintingContext context)
2525
&& previousList.Contents[^2] is HardLine
2626
)
2727
{
28-
while (list.Contents[0] is HardLine { SkipBreakIfFirstInGroup: true })
28+
for (
29+
var i = 0;
30+
i < list.Contents.Count
31+
&& list.Contents[0] is HardLine { SkipBreakIfFirstInGroup: true };
32+
i++
33+
)
2934
{
30-
list.Contents.RemoveAt(0);
35+
list.Contents[i] = Doc.Null;
3136
}
3237

3338
docs.Add(finalTrivia);

Src/CSharpier/SyntaxPrinter/Token.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private static Doc PrivatePrintLeadingTrivia(
253253

254254
if (
255255
!(
256-
docs.Count > 1
256+
docs.Length > 1
257257
&& docs[^1] == Doc.HardLineSkipBreakIfFirstInGroup
258258
&& docs[^2] is LeadingComment { Type: CommentType.SingleLine }
259259
)
@@ -374,7 +374,7 @@ void AddLeadingComment(CommentType commentType, ref ValueListBuilder<Doc> docs)
374374
context.State.NextTriviaNeedsLine = false;
375375
}
376376

377-
return docs.Length > 0 ? Doc.Concat(ref docs) : Doc.Null;
377+
return Doc.Concat(ref docs);
378378
}
379379

380380
private static bool IsSingleLineComment(SyntaxKind kind) =>

Src/CSharpier/Utilities/ValueListBuilder.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,6 @@ public ReadOnlySpan<T> AsSpan()
154154
return _span.Slice(0, _pos);
155155
}
156156

157-
public List<T> ToList()
158-
{
159-
var list = new List<T>(_pos);
160-
#if NETSTANDARD2_0
161-
foreach (var item in _span[.._pos])
162-
{
163-
list.Add(item);
164-
}
165-
#else
166-
list.AddRange(_span[.._pos]);
167-
#endif
168-
169-
return list;
170-
}
171-
172157
public bool TryCopyTo(Span<T> destination, out int itemsWritten)
173158
{
174159
if (_span.Slice(0, _pos).TryCopyTo(destination))

0 commit comments

Comments
 (0)