Optimize Enumerable Min/Max final reduction with shuffles#127995
Open
EgorBo wants to merge 4 commits intodotnet:mainfrom
Open
Optimize Enumerable Min/Max final reduction with shuffles#127995EgorBo wants to merge 4 commits intodotnet:mainfrom
EgorBo wants to merge 4 commits intodotnet:mainfrom
Conversation
Replaces the scalar log-N reduction loop on the final 128-bit accumulator with a shuffle-based tree reduction (log2(Vector128<T>.Count) compares), introducing a HorizontalMinMax helper modeled after TensorPrimitives.HorizontalAggregate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-linq |
Member
Author
Member
Author
|
Note Benchmark generated by AI (GitHub Copilot CLI). @EgorBot -linux_amd -linux_intel -linux_arm64 using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
public class Bench
{
[Params(16, 64)]
public int Length;
private byte[] _bytes = default!;
private sbyte[] _sbytes = default!;
private short[] _shorts = default!;
private ushort[] _ushorts = default!;
private int[] _ints = default!;
private uint[] _uints = default!;
private long[] _longs = default!;
private ulong[] _ulongs = default!;
[GlobalSetup]
public void Setup()
{
var rng = new Random(42);
_bytes = new byte[Length];
rng.NextBytes(_bytes);
_sbytes = new sbyte[Length];
for (int i = 0; i < Length; i++) _sbytes[i] = (sbyte)rng.Next(sbyte.MinValue, sbyte.MaxValue + 1);
_shorts = new short[Length];
for (int i = 0; i < Length; i++) _shorts[i] = (short)rng.Next(short.MinValue, short.MaxValue + 1);
_ushorts = new ushort[Length];
for (int i = 0; i < Length; i++) _ushorts[i] = (ushort)rng.Next(0, ushort.MaxValue + 1);
_ints = new int[Length];
for (int i = 0; i < Length; i++) _ints[i] = rng.Next();
_uints = new uint[Length];
for (int i = 0; i < Length; i++) _uints[i] = (uint)rng.Next();
_longs = new long[Length];
for (int i = 0; i < Length; i++) _longs[i] = rng.NextInt64();
_ulongs = new ulong[Length];
for (int i = 0; i < Length; i++) _ulongs[i] = (ulong)rng.NextInt64();
}
[Benchmark] public byte MaxByte() => _bytes.Max();
[Benchmark] public sbyte MaxSByte() => _sbytes.Max();
[Benchmark] public short MaxShort() => _shorts.Max();
[Benchmark] public ushort MaxUShort() => _ushorts.Max();
[Benchmark] public int MaxInt() => _ints.Max();
[Benchmark] public uint MaxUInt() => _uints.Max();
[Benchmark] public long MaxLong() => _longs.Max();
[Benchmark] public ulong MaxULong() => _ulongs.Max();
} |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR changes the final reduction step in System.Linq’s vectorized integer Min/Max implementation to reduce a Vector128<T> accumulator down to a single scalar using shuffle-based horizontal reduction instead of a scalar per-lane loop.
Changes:
- Replaced the per-element
forloop reduction ofbest128with a helper (HorizontalMinMax) that performs log2(lane-count) reductions usingVector128.Shuffle. - Added
HorizontalMinMax<T, TMinMax>to centralize the reduction logic for different element sizes (byte/short/int/long lane counts).
MihaZupan
approved these changes
May 9, 2026
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 was filed by AI (GitHub Copilot CLI).
Inspired by a similar helper in Tensors. I guess we don't want to consume Tensors in Linq due to IL size concerns. It also doesn't look beneficial to share this function via source file due to different generics being used. Maybe Vector128 will get this as public API at some point.
Benchmark
Full benchmark request and raw output: EgorBot/Benchmarks#198
Results
Speed-up =
maintime /PRtime (higher is better). Numbers below are from EgorBot.ARM64 — Neoverse-N2 (
ubuntu24_azure_cobalt100)Intel — Emerald Rapids / AVX-512 (
ubuntu24_azure_emeraldrapids)AMD — EPYC 9V45 (Turin) / AVX-512 (
ubuntu24_azure_turin)