Skip to content

Commit 2ff5d49

Browse files
perf: prevent closure creation in Modifier
1 parent a06a9f8 commit 2ff5d49

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,39 @@ internal static class Modifiers
77
{
88
private class DefaultOrder : IComparer<SyntaxToken>
99
{
10-
// use the default order from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0036
11-
private static readonly string[] DefaultOrdered =
12-
[
13-
"public",
14-
"private",
15-
"protected",
16-
"internal",
17-
"file",
18-
"static",
19-
"extern",
20-
"new",
21-
"virtual",
22-
"abstract",
23-
"sealed",
24-
"override",
25-
"readonly",
26-
"unsafe",
27-
"required",
28-
"volatile",
29-
"async",
30-
];
31-
3210
public int Compare(SyntaxToken x, SyntaxToken y)
3311
{
3412
return GetIndex(x.Text) - GetIndex(y.Text);
3513
}
3614

3715
private static int GetIndex(string? value)
3816
{
39-
var result = Array.IndexOf(DefaultOrdered, value);
40-
return result == -1 ? int.MaxValue : result;
17+
// use the default order from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0036
18+
return value switch
19+
{
20+
"public" => 0,
21+
"private" => 1,
22+
"protected" => 2,
23+
"internal" => 3,
24+
"file" => 4,
25+
"static" => 5,
26+
"extern" => 6,
27+
"new" => 7,
28+
"virtual" => 8,
29+
"abstract" => 9,
30+
"sealed" => 10,
31+
"override" => 11,
32+
"readonly" => 12,
33+
"unsafe" => 13,
34+
"required" => 14,
35+
"volatile" => 15,
36+
"async" => 16,
37+
_ => int.MaxValue,
38+
};
4139
}
4240
}
4341

44-
private static readonly DefaultOrder Comparer = new();
42+
private static readonly Comparison<SyntaxToken> Comparer = new DefaultOrder().Compare;
4543

4644
public static Doc Print(SyntaxTokenList modifiers, PrintingContext context)
4745
{
@@ -58,7 +56,7 @@ public static Doc PrintSorted(SyntaxTokenList modifiers, PrintingContext context
5856
return PrintWithSortedModifiers(
5957
modifiers,
6058
context,
61-
sortedModifiers =>
59+
static (sortedModifiers, context) =>
6260
Doc.Group(Doc.Join(" ", sortedModifiers.Select(o => Token.Print(o, context))), " ")
6361
);
6462
}
@@ -71,7 +69,7 @@ PrintingContext context
7169
return PrintWithSortedModifiers(
7270
modifiers,
7371
context,
74-
sortedModifiers =>
72+
static (sortedModifiers, context) =>
7573
Doc.Group(
7674
Token.PrintWithoutLeadingTrivia(sortedModifiers[0], context),
7775
" ",
@@ -90,7 +88,7 @@ PrintingContext context
9088
private static Doc PrintWithSortedModifiers(
9189
in SyntaxTokenList modifiers,
9290
PrintingContext context,
93-
Func<IReadOnlyList<SyntaxToken>, Doc> print
91+
Func<IReadOnlyList<SyntaxToken>, PrintingContext, Doc> print
9492
)
9593
{
9694
if (modifiers.Count == 0)
@@ -114,6 +112,6 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
114112
context.State.ReorderedModifiers = true;
115113
}
116114

117-
return print(sortedModifiers);
115+
return print(sortedModifiers, context);
118116
}
119117
}

0 commit comments

Comments
 (0)