Skip to content

Commit ffe960e

Browse files
Refactor LINQ chains to use Count/Sum selector overloads
Simplified LINQ expressions by replacing Where+Count and Select+Sum chains with the corresponding Count and Sum overloads that accept a predicate or selector. This reduces intermediate enumerations and may improve performance.
1 parent 0082af4 commit ffe960e

2 files changed

Lines changed: 3 additions & 6 deletions

File tree

Benchmarks/AllocationBaselineBenchmark.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,13 @@ public int Medium_ForeachOnly()
8383
public int Medium_LinqWhere()
8484
=> MediumString.AsSegment()
8585
.SplitAsSegments(',')
86-
.Where(s => s.Length > 5)
87-
.Count();
86+
.Count(s => s.Length > 5);
8887

8988
[Benchmark(Description = "Medium - LINQ Select")]
9089
public int Medium_LinqSelect()
9190
=> MediumString.AsSegment()
9291
.SplitAsSegments(',')
93-
.Select(s => s.Length)
94-
.Sum();
92+
.Sum(s => s.Length);
9593

9694
// ========== Large String Tests ==========
9795

Benchmarks/ZLinqImprovementsBenchmark.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ public int LinqChain_BCL()
190190
public int LinqChain_ZLinq()
191191
=> SmallCsv.SplitAsSegments(',')
192192
.Where(s => s.Length > 4)
193-
.Select(s => s.Length)
194-
.Sum();
193+
.Sum(s => s.Length);
195194

196195
// =====================================================================
197196
// CATEGORY: ToArray Materialization

0 commit comments

Comments
 (0)