Skip to content

Commit 92ada39

Browse files
perf: prevent boxing SyntaxTokenList by calling ToArray earlier
1 parent 7d2623f commit 92ada39

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,24 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
9999
return Doc.Null;
100100
}
101101

102+
var modifierArray = modifiers.ToArray();
103+
102104
// reordering modifiers inside of #ifs can lead to code that doesn't compile
103105
var willReorderModifiers =
104-
modifiers.Count > 1
105-
&& !modifiers.Skip(1).Any(o => o.LeadingTrivia.Any(p => p.IsDirective || p.IsComment()))
106-
&& !modifiers[0].LeadingTrivia.Any(p => p.IsDirective);
106+
modifierArray.Length > 1
107+
&& !modifierArray
108+
.Skip(1)
109+
.Any(o => o.LeadingTrivia.Any(p => p.IsDirective || p.IsComment()))
110+
&& !modifierArray[0].LeadingTrivia.Any(p => p.IsDirective);
107111

108-
var sortedModifiers = modifiers.ToArray();
109-
var leadingToken = sortedModifiers.FirstOrDefault();
112+
var leadingToken = modifierArray.FirstOrDefault();
110113
if (willReorderModifiers)
111114
{
112-
Array.Sort(sortedModifiers, Comparer);
115+
Array.Sort(modifierArray, Comparer);
113116
}
114117

118+
var sortedModifiers = modifierArray;
119+
115120
if (willReorderModifiers && !sortedModifiers.SequenceEqual(modifiers))
116121
{
117122
context.State.ReorderedModifiers = true;

0 commit comments

Comments
 (0)