Skip to content

Add MemoryExtensions.Min/Max#128306

Merged
MihaZupan merged 29 commits into
dotnet:mainfrom
manandre:span-min-max
Jun 24, 2026
Merged

Add MemoryExtensions.Min/Max#128306
MihaZupan merged 29 commits into
dotnet:mainfrom
manandre:span-min-max

Conversation

@manandre

Copy link
Copy Markdown
Contributor

This pull request adds new Min and Max extension methods for ReadOnlySpan<T> to the System.Memory library, allowing users to efficiently find the minimum and maximum values in a span, optionally using a custom comparer. Comprehensive unit tests are included to verify correct behavior for various data types and edge cases.

New Min and Max methods for ReadOnlySpan<T>:

  • Added Min<T> and Max<T> extension methods to MemoryExtensions for ReadOnlySpan<T>, supporting both default and custom comparers, and handling nullable and reference types gracefully. These methods throw an exception on empty spans of non-nullable value types and return null for empty spans of reference or nullable types. [1] [2]

Unit tests:

  • Introduced comprehensive tests in ReadOnlySpan/MinMax.cs to cover empty spans, all-null values, custom comparers, and typical scenarios for both value and reference types.
  • Registered the new test file in the project file System.Memory.Tests.csproj to ensure the tests are run as part of the test suite.

Fixes #127083

Copilot AI review requested due to automatic review settings May 17, 2026 21:55
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 17, 2026
@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

Note

Copilot was unable to run its full agentic suite in this review.

Adds Min and Max extension methods on ReadOnlySpan<T> (with optional IComparer<T>), mirroring LINQ's Enumerable.Min/Max semantics for spans. Includes ref assembly updates and tests.

Changes:

  • New Min/Max ReadOnlySpan extension methods with empty-span and null-element handling.
  • Reference assembly exposes the four new API overloads.
  • New test file covering empty, all-null, mixed-null, default comparer, and custom comparer scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Implements Min/Max with a generic comparer fast path and null-skipping inner core.
src/libraries/System.Memory/ref/System.Memory.cs Adds public surface for the four new overloads.
src/libraries/System.Memory/tests/System.Memory.Tests.csproj Includes the new test file.
src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs Tests for empty/null/normal/custom-comparer behaviors.

Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs
Comment thread src/libraries/System.Memory/ref/System.Memory.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
@MihaZupan

Copy link
Copy Markdown
Member

Enumerable's Min/Max already have vectorized logic implementing this. We should move that logic into these new methods, then switch LINQ to call them instead

Copilot AI review requested due to automatic review settings May 18, 2026 22:00

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Comment thread src/libraries/System.Private.CoreLib/src/Resources/Strings.resx Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Memory/ref/System.Memory.cs Outdated
Comment thread src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs Outdated
Comment thread src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs Outdated
Copilot AI review requested due to automatic review settings May 18, 2026 22:55

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs
Comment thread src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Copilot AI review requested due to automatic review settings May 19, 2026 21:49

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Copilot AI review requested due to automatic review settings May 19, 2026 22:21

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs Outdated
Comment thread src/libraries/System.Memory/tests/ReadOnlySpan/MinMax.cs
Copilot AI review requested due to automatic review settings May 21, 2026 21:24

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Comment thread src/libraries/System.Linq/src/System/Linq/Min.cs
Comment thread src/libraries/System.Linq/src/System/Linq/Max.cs
Comment thread src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Comment thread src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.MinMax.cs Outdated
Copilot AI review requested due to automatic review settings June 23, 2026 21:18

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Comment thread src/libraries/System.Memory/ref/System.Memory.cs Outdated
Comment thread src/libraries/System.Memory/ref/System.Memory.cs Outdated
@MihaZupan MihaZupan added this to the 11.0.0 milestone Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Memory community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: MemoryExtensions.Min/Max

5 participants