-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LINQ - use TryGetSpan in Enumerable.Count with predicate #102696
Comments
Tagging subscribers to this area: @dotnet/area-system-linq |
Yes, we've experimented with this multiple times, eg main...stephentoub:runtime:fasterlinqenumeration, but haven't yet moved ahead with it as for some scenarios it makes things worse. |
@stephentoub can you share the benchmark results? |
@stephentoub is there a tracking issue for this, or should we just keep this open in lieu of one? |
Description
similar how Max.cs & Min try to convert to span
runtime/src/libraries/System.Linq/src/System/Linq/Max.cs
Line 107 in 35e4aad
runtime/src/libraries/System.Linq/src/System/Linq/Min.cs
Line 210 in 35e4aad
Iterating over a span when possible would be faster then iterating over enumerable.
Configuration
// * Summary *
BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3593/23H2/2023Update/SunValley3)
11th Gen Intel Core i9-11950H 2.60GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.205
[Host] : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
ShortRun : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
Job=ShortRun IterationCount=3 LaunchCount=1
WarmupCount=3
// * Legends *
count : Value of the 'count' parameter
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
// * Diagnostic Output - MemoryDiagnoser *
// ***** BenchmarkRunner: End *****
Run time: 00:01:54 (114.04 sec), executed benchmarks: 18
Global total time: 00:02:04 (124.39 sec), executed benchmarks: 18
Regression?
No
Analysis
runtime/src/libraries/System.Linq/src/System/Linq/Count.cs
Line 62 in 35e4aad
source.TryGetSpan(out ReadOnlySpan<TSource> span)
and then a dedicated foreach loop for the read-only-spanThe text was updated successfully, but these errors were encountered: