Skip to content

Commit 217f9e9

Browse files
perf: replace ManualSkipAny with Span extensions
1 parent b34b5c2 commit 217f9e9

File tree

2 files changed

+12
-35
lines changed

2 files changed

+12
-35
lines changed

Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public static Doc PrintMemberChain(ExpressionSyntax node, PrintingContext contex
6161
(
6262
groups.Length <= cutoff
6363
&& (
64-
groups.ManualSkipAny(
65-
shouldMergeFirstTwoGroups ? 1 : 0,
66-
o =>
64+
groups
65+
.AsSpan()
66+
.Skip(shouldMergeFirstTwoGroups ? 1 : 0)
67+
.Any(o =>
6768
o.Last().Node
6869
is not (
6970
InvocationExpressionSyntax
@@ -73,7 +74,7 @@ or PostfixUnaryExpressionSyntax
7374
Operand: InvocationExpressionSyntax
7475
}
7576
)
76-
)
77+
)
7778
// if the last group contains just a !, make sure it doesn't end up on a new line
7879
|| (
7980
groups[^1].Length == 1 && groups[^1][0].Node is PostfixUnaryExpressionSyntax
@@ -120,7 +121,7 @@ is LiteralExpressionSyntax
120121
);
121122

122123
return
123-
oneLine.ManualSkipAny(1, DocUtilities.ContainsBreak)
124+
oneLine.Skip(1).Any(DocUtilities.ContainsBreak)
124125
|| groups[0]
125126
.Any(o =>
126127
o.Node

Src/CSharpier/Utilities/ListExtensions.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,20 @@ public static void AddIfNotNull(this List<Doc> value, Doc doc)
2727
}
2828
}
2929

30-
public static bool ManualSkipAny<T>(
31-
ref this ValueListBuilder<T> collection,
32-
int count,
33-
Func<T, bool> predicate
34-
)
30+
public static bool Any<T>(this ReadOnlySpan<T> span, Func<T, bool> predicate)
3531
{
36-
if (predicate == null || count < 0)
37-
throw new ArgumentException("Invalid arguments");
38-
39-
int skipped = 0;
40-
foreach (var item in collection.AsSpan())
32+
foreach (var item in span)
4133
{
42-
if (skipped++ < count)
43-
continue; // Skip the first 'count' items
44-
4534
if (predicate(item))
46-
return true; // If any item satisfies the predicate, return true
35+
return true;
4736
}
4837

49-
return false; // No match found after skipping 'count' items
38+
return false;
5039
}
5140

52-
public static bool ManualSkipAny<T>(this T[] collection, int count, Func<T, bool> predicate)
41+
public static ReadOnlySpan<T> Skip<T>(this ReadOnlySpan<T> span, int count)
5342
{
54-
if (collection == null || predicate == null || count < 0)
55-
throw new ArgumentException("Invalid arguments");
56-
57-
int skipped = 0;
58-
foreach (var item in collection)
59-
{
60-
if (skipped++ < count)
61-
continue; // Skip the first 'count' items
62-
63-
if (predicate(item))
64-
return true; // If any item satisfies the predicate, return true
65-
}
66-
67-
return false; // No match found after skipping 'count' items
43+
return count > span.Length ? [] : span[count..];
6844
}
6945

7046
// Overload for Any to prevent unnecessary allocations of EnumeratorImpl

0 commit comments

Comments
 (0)