Remove unsafe code from System.Linq.MaxMin#127845
Draft
EgorBo wants to merge 3 commits intodotnet:mainfrom
Draft
Remove unsafe code from System.Linq.MaxMin#127845EgorBo wants to merge 3 commits intodotnet:mainfrom
EgorBo wants to merge 3 commits intodotnet:mainfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-linq |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes explicit Unsafe / MemoryMarshal-based vector loads from System.Linq’s integer min/max implementation, switching to span-based Vector*.Create(ReadOnlySpan<T>) to avoid unsafe code in MaxMin.
Changes:
- Replaced
LoadUnsafe+ ref/Unsafe.Additeration withReadOnlySpan<T>slicing andVector128/256/512.Create(ReadOnlySpan<T>). - Kept the same “first vector + interior vectors + last vector” aggregation pattern while removing address comparisons.
Member
Author
Member
Author
|
Note AI-generated benchmark. Targets only the @EgorBot -arm -amd using System;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
public class Bench
{
private int[] _ints = default!;
private long[] _longs = default!;
private byte[] _bytes = default!;
[Params(32, 256, 4096)]
public int Length { get; set; }
[GlobalSetup]
public void Setup()
{
var rng = new Random(42);
_ints = new int[Length];
_longs = new long[Length];
_bytes = new byte[Length];
for (int i = 0; i < Length; i++)
{
_ints[i] = rng.Next();
_longs[i] = ((long)rng.Next() << 32) | (uint)rng.Next();
_bytes[i] = (byte)rng.Next(256);
}
}
[Benchmark] public int IntMax() => _ints.Max();
[Benchmark] public int IntMin() => _ints.Min();
[Benchmark] public long LongMax() => _longs.Max();
[Benchmark] public byte ByteMax() => _bytes.Max();
} |
This was referenced May 6, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR is AI-generated.
No asm diffs
Also, added
[Intrinsic]on top of non-generics Vector classes. The generic ones do have it already.The intrinsic on a type helps inliner when it runs out of budget, as a hint that these apis (like Vector.Create(Span) or Vector.CopyTo(Span)) are important.