Repository navigation
Description
Not sure if these were missed or intentionally left out. It refers to e.g.
runtime/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
Lines 938 to 961 in 8f65ed7
| public static unsafe int IndexOfAnyExcept<T>(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>? | |
| { | |
| if (RuntimeHelpers.IsBitwiseEquatable<T>()) | |
| { | |
| if (sizeof(T) == sizeof(byte)) | |
| { | |
| return SpanHelpers.IndexOfAnyExceptValueType( | |
| ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | |
| Unsafe.BitCast<T, byte>(value0), | |
| Unsafe.BitCast<T, byte>(value1), | |
| span.Length); | |
| } | |
| else if (sizeof(T) == sizeof(short)) | |
| { | |
| return SpanHelpers.IndexOfAnyExceptValueType( | |
| ref Unsafe.As<T, short>(ref MemoryMarshal.GetReference(span)), | |
| Unsafe.BitCast<T, short>(value0), | |
| Unsafe.BitCast<T, short>(value1), | |
| span.Length); | |
| } | |
| } | |
| return SpanHelpers.IndexOfAnyExcept(ref MemoryMarshal.GetReference(span), value0, value1, span.Length); | |
| } |
Description
Several multi-value search methods in MemoryExtensions dispatch to vectorized SpanHelpers helpers for byte and short-sized types, but silently fall through to the slow scalar IEquatable<T>.Equals-based path for int and long-sized types.
The SpanHelpers helpers already support all four sizes generically (T : struct, INumber<T>) — the fix is purely in MemoryExtensions.cs.
The affected methods and their current coverage:
| Method | byte |
short |
int |
long |
|---|---|---|---|---|
IndexOfAny(span, v0, v1) |
✅ | ✅ | ❌ | ❌ |
IndexOfAny(span, v0, v1, comparer) |
✅ | ✅ | ❌ | ❌ |
IndexOfAny(span, v0, v1, v2) |
✅ | ✅ | ❌ | ❌ |
IndexOfAny(span, v0, v1, v2, comparer) |
✅ | ✅ | ❌ | ❌ |
IndexOfAnyExcept(span, v0, v1) |
✅ | ✅ | ❌ | ❌ |
IndexOfAnyExcept(span, v0, v1, comparer) |
✅ | ✅ | ❌ | ❌ |
IndexOfAnyExcept(span, v0, v1, v2) |
✅ | ✅ | ❌ | ❌ |
IndexOfAnyExcept(span, v0, v1, v2, comparer) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAny(span, v0, v1) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAny(span, v0, v1, comparer) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAny(span, v0, v1, v2) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAny(span, v0, v1, v2, comparer) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAnyExcept(span, v0, v1) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAnyExcept(span, v0, v1, comparer) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAnyExcept(span, v0, v1, v2) |
✅ | ✅ | ❌ | ❌ |
LastIndexOfAnyExcept(span, v0, v1, v2, comparer) |
✅ | ✅ | ❌ | ❌ |
Analysis
The SpanHelpers helpers (IndexOfAnyValueType, IndexOfAnyExceptValueType, LastIndexOfAnyValueType, LastIndexOfAnyExceptValueType) are all generic over T : struct, INumber<T> and their implementations are fully vectorized for all four sizes (the Debug.Assert(value0 is byte or short or int or long, ...) inside confirms this). No changes to SpanHelpers are required.
The fix is to add sizeof(int) and sizeof(long) branches to each affected method in MemoryExtensions.cs, e.g. for IndexOfAny(span, v0, v1):
else if (sizeof(T) == sizeof(int))
{
return SpanHelpers.IndexOfAnyValueType(
ref Unsafe.As<T, int>(ref MemoryMarshal.GetReference(span)),
Unsafe.BitCast<T, int>(value0),
Unsafe.BitCast<T, int>(value1),
span.Length);
}
else if (sizeof(T) == sizeof(long))
{
return SpanHelpers.IndexOfAnyValueType(
ref Unsafe.As<T, long>(ref MemoryMarshal.GetReference(span)),
Unsafe.BitCast<T, long>(value0),
Unsafe.BitCast<T, long>(value1),
span.Length);
}
Activity
dotnet-policy-service commentedon Jul 25, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Jul 25, 2026
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
huoyaoyuan commentedon Jul 25, 2026
It's somehow intentional. All these methods are intended to be used with UTF-8/UTF-16 text. The implementation under
{NonPacked}IndexOfAnyValueTypeetc has specialized optimizations for 8bit or 16bit chars.The fallback implementation under
SpanHelpershas loop unrolling. It's important to do a comparison for the performance.MichalPetryka commentedon Jul 25, 2026
Yeah I assume a PR would be fine here if benchmarks show the path to be faster.
jeffhandley commentedon Aug 16, 2026
We would consider a PR with benchmarks showing notable fast path differences. Thanks.
TheNetherWatcher commentedon Sep 5, 2026
I benchmarked this to check whether the vectorized
SpanHelpers.*ValueTypepath is actually faster than the current scalar fallback forint/long, per the concern that the fallback's loop-unrolling might already be competitive.Methodology: extracted the exact current scalar fallback (
SpanHelpers.IndexOfAny<T>, 8x-unrolled, directT.Equals) and the exact vectorized path (NonPackedIndexOfAnyValueType, Vector128/256/512) into an isolated BenchmarkDotNet harness — not a modified corelib build, just the two algorithms copied verbatim so I could compare them directly. Benchmarked across span lengths 2–65536 and three match positions (notfound= full scan,end= match at the last element,start= match at index 0), on two architectures: Apple Silicon (Arm64/NEON, 128-bit only) and a GitHub Actionsubuntu-latestrunner (x64, confirmed AVX2 + AVX-512 via/proc/cpuinfo).Arm64 (NEON) — int
Arm64 (NEON) — long
x64 (AVX2 + AVX-512) — int
x64 (AVX2 + AVX-512) — long
Summary: for the realistic cases (
notfound/end— scanning a meaningful portion of the span), the vectorized path wins consistently once length passes roughly one-vector's-worth of elements: up to ~4x forintand ~2x forlongon x64, ~2.7x/~1.4x on Arm64. The one place it loses isstart(match at index 0) and very short spans just above the vector-width threshold — there the vectorized path pays a small fixed setup cost (~0.5ns on Arm64, ~1.6–1.8ns on x64) that the scalar path skips by returning on its very first comparison. Both are cheap in absolute terms there, so I don't think it's a real regression risk in practice.Given this, I'd like to submit a PR adding the
sizeof(int)/sizeof(long)dispatch branches (following the pattern already sketched in the issue body) to all 16 affected methods. Happy to measure anything else first if useful.aw0lid commentedon Sep 13, 2026
Hey @TheNetherWatcher, thanks for the benchmarks! Are you still planning to submit a PR for this? I’m interested in working on it, so if you’re not actively working on it anymore, I’d be happy to pick it up.
TheNetherWatcher commentedon Sep 14, 2026
Yes, still on it — sorry for the delay. Opening the PR now with the benchmarked fast paths above.