Skip to content

Commit 2412841

Browse files
perf: add Any overload for SyntaxTokenList (#1534)
Add `Any` overload for `SyntaxTokenList`, saves 0.5 MB on the complex code bencmark. ### Benchmarks #### Before | Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated | |------------------------------ |---------:|--------:|---------:|----------:|----------:|----------:|----------:| | Default_CodeFormatter_Complex | 241.7 ms | 7.22 ms | 21.17 ms | 7000.0000 | 4000.0000 | 1000.0000 | 61.61 MB | #### After | Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated | |------------------------------ |---------:|--------:|--------:|----------:|----------:|----------:|----------:| | Default_CodeFormatter_Complex | 234.2 ms | 4.65 ms | 6.82 ms | 7000.0000 | 4000.0000 | 1000.0000 | 61.09 MB | Co-authored-by: Bela VanderVoort <[email protected]>
1 parent 35a939e commit 2412841

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Src/CSharpier/Utilities/ListExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public static bool Any(this in SyntaxTriviaList triviaList, Func<SyntaxTrivia, b
3434
return false;
3535
}
3636

37+
// Overload for Any to prevent unnecessary allocations of EnumeratorImpl
38+
public static bool Any(this in SyntaxTokenList tokenList, Func<SyntaxToken, bool> predicate)
39+
{
40+
// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
41+
foreach (var token in tokenList)
42+
{
43+
if (predicate(token))
44+
{
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
3752
public static SyntaxTrivia FirstOrDefault(
3853
this in SyntaxTriviaList source,
3954
Func<SyntaxTrivia, bool> predicate

0 commit comments

Comments
 (0)