Skip to content

Add back MinInteger/MaxInteger paths for IEnumerables in LINQ#130138

Merged
MihaZupan merged 1 commit into
dotnet:mainfrom
MihaZupan:minMaxEnumerable
Jul 2, 2026
Merged

Add back MinInteger/MaxInteger paths for IEnumerables in LINQ#130138
MihaZupan merged 1 commit into
dotnet:mainfrom
MihaZupan:minMaxEnumerable

Conversation

@MihaZupan

Copy link
Copy Markdown
Member

Fixes #130047 - perf regression from #128306 for IEnumerable inputs where we can't extract the span, and the T is an integer type. #128306 removed the loop that'll go over the IEnumerable and compare results via < instead of going through IComparer. This PR adds that logic back.

MihuBot/runtime-utils#2066

Method Toolchain input Mean Error Ratio Allocated Alloc Ratio
Min Main IEnumerable 102.14 ns 0.418 ns 1.00 32 B 1.00
Min PR IEnumerable 44.23 ns 1.275 ns 0.43 - 0.00
Max Main IEnumerable 107.91 ns 0.047 ns 1.00 32 B 1.00
Max PR IEnumerable 56.38 ns 0.198 ns 0.52 - 0.00

The intention before merging that PR was to avoid such regressions, but looks like my benchmark in #128306 (comment) didn't catch it EgorBot/Benchmarks#208 (comment).

@MihaZupan MihaZupan added this to the 11.0.0 milestone Jul 2, 2026
@MihaZupan MihaZupan self-assigned this Jul 2, 2026
Copilot AI review requested due to automatic review settings July 2, 2026 18:23
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reintroduces specialized Min/Max fast paths in System.Linq.Enumerable for non-span IEnumerable<T> inputs when T is an integer type and the caller uses the default comparer (or null comparer), avoiding per-element Comparer<T>.Default.Compare overhead.

Changes:

  • Added MinIntegerEnumerator<T> / MaxIntegerEnumerator<T> helpers that compute min/max via direct < / > comparisons for IBinaryInteger<T> types.
  • Updated the generic Min<TSource>(IEnumerable<TSource>, IComparer<TSource>?) and Max<TSource>(...) implementations to detect common integer types and route to the new enumerator helpers when the comparer is default or null.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Linq/src/System/Linq/Min.cs Adds an integer enumerator-based min helper and routes the generic Min<TSource> to it for non-span integer sequences with default/null comparer.
src/libraries/System.Linq/src/System/Linq/Max.cs Adds an integer enumerator-based max helper and routes the generic Max<TSource> to it for non-span integer sequences with default/null comparer.

@MihaZupan
MihaZupan enabled auto-merge (squash) July 2, 2026 18:46
@MihaZupan
MihaZupan merged commit 737bb13 into dotnet:main Jul 2, 2026
96 of 98 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf] Windows/x64: 7 Regressions on 6/24/2026 8:46:56 PM +00:00

3 participants