Skip to content

Commit 35a939e

Browse files
perf: remove unnecessary ToList call and convert linq to code (#1523)
- Remove unnecessary `ToList` call - 0.15 MB - Extract a linq call and convert it to code - 0.8 MB ### Benchmarks (timing is inaccurate) #### Before | Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated | |---------------------- |---------:|--------:|---------:|----------:|----------:|----------:| | Default_CodeFormatter | 191.4 ms | 4.05 ms | 11.81 ms | 4000.0000 | 2000.0000 | 42.24 MB | #### After | Method | Mean | Error | StdDev | Median | Gen0 | Gen1 | Allocated | |---------------------- |---------:|---------:|---------:|---------:|----------:|----------:|----------:| | Default_CodeFormatter | 157.3 ms | 11.59 ms | 34.18 ms | 138.8 ms | 4000.0000 | 2000.0000 | 41.29 MB |
1 parent 74ad911 commit 35a939e

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

Src/CSharpier/DocTypes/Doc.cs

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

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

59-
public static Doc Join(Doc separator, IEnumerable<Doc> array)
59+
public static Doc Join(Doc separator, IEnumerable<Doc> enumerable)
6060
{
6161
var docs = new List<Doc>();
6262

63-
var list = array.ToList();
64-
65-
if (list.Count == 1)
66-
{
67-
return list[0];
68-
}
69-
70-
for (var x = 0; x < list.Count; x++)
63+
var x = 0;
64+
foreach (var doc in enumerable)
7165
{
7266
if (x != 0)
7367
{
7468
docs.Add(separator);
7569
}
7670

77-
docs.Add(list[x]);
71+
docs.Add(doc);
72+
x++;
7873
}
7974

80-
return Concat(docs);
75+
return docs.Count == 1 ? docs[0] : Concat(docs);
8176
}
8277

8378
public static ForceFlat ForceFlat(List<Doc> contents) =>

Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
13
namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters;
24

35
internal record PrintedNode(CSharpSyntaxNode Node, Doc Doc);
@@ -27,7 +29,7 @@ public static Doc PrintMemberChain(ExpressionSyntax node, PrintingContext contex
2729
? GroupPrintedNodesPrettierStyle(printedNodes)
2830
: GroupPrintedNodesOnLines(printedNodes);
2931

30-
var oneLine = groups.SelectMany(o => o).Select(o => o.Doc).ToArray();
32+
var oneLine = SelectManyDocsToArray(groups);
3133

3234
var shouldMergeFirstTwoGroups = ShouldMergeFirstTwoGroups(groups, parent);
3335

@@ -336,6 +338,30 @@ or ConditionalAccessExpressionSyntax
336338
return groups;
337339
}
338340

341+
[SuppressMessage("ReSharper", "ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator")]
342+
private static Doc[] SelectManyDocsToArray(List<List<PrintedNode>> groups)
343+
{
344+
var arrayLength = 0;
345+
foreach (var group in groups)
346+
{
347+
arrayLength += group.Count;
348+
}
349+
350+
var outputArray = new Doc[arrayLength];
351+
352+
var pos = 0;
353+
foreach (var group in groups)
354+
{
355+
foreach (var node in group)
356+
{
357+
outputArray[pos] = node.Doc;
358+
pos++;
359+
}
360+
}
361+
362+
return outputArray;
363+
}
364+
339365
private static Doc PrintIndentedGroup(List<List<PrintedNode>> groups)
340366
{
341367
if (groups.Count == 0)

0 commit comments

Comments
 (0)