Vectorize SearchValues.Create min/max scan using Vector128#124667
Vectorize SearchValues.Create min/max scan using Vector128#124667
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-buffers |
Co-authored-by: MihaZupan <[email protected]>
Co-authored-by: MihaZupan <[email protected]>
|
@copilot Use the benchmark skill to check the performance difference of |
@EgorBot -linux_amd -osx_arm64 using System.Buffers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
public class Bench
{
// Standard Base64 alphabet (64 chars)
private const string Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
[Benchmark]
public SearchValues<char> Create_Base64Alphabet() =>
SearchValues.Create(Base64Chars);
} |
EgorBot Benchmark AnalysisLinux AMD64 — AMD EPYC 9V74 (Genoa)
macOS ARM64 — Apple M1
Summary: The vectorized |
SearchValues.Createwalks input values to find min/max (used to select the optimal search implementation) using a scalar loop. This vectorizes that scan to process multiple elements per iteration.Description
SearchValues.cs— vectorizedTryGetSingleRangeAdded a
GetMinMax<T>helper that computes min and max in a single vectorized pass:Vector128.Min/Vector128.Maxto processVector128<T>.Countelements per iteration (8ushorts or 16bytes at once)charis not a validVector128element type — the span is cast toReadOnlySpan<ushort>, and the results are converted back withUnsafe.BitCast<ushort, T>Testing
Performance was validated using @EgorBot on Linux/AMD64 and macOS/ARM64 with
SearchValues.Createon the 64-character Base64 alphabet as the benchmark input.Risk
Low —
GetMinMaxis only called duringSearchValues.Create, not on hot search paths. The scalar fallback is identical to the original code.🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.