runtime icon indicating copy to clipboard operation
runtime copied to clipboard

MemoryExtensions: multi-value IndexOfAny/IndexOfAnyExcept/LastIndexOfAny/LastIndexOfAnyExcept missing int/long fast paths

Open Rob-Hague opened this issue 4 months ago • 8 comments

Not sure if these were missed or intentionally left out. It refers to e.g.

https://github.com/dotnet/runtime/blob/8f65ed776323eac3ad84930f5c82a3f665280e70/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs#L938-L961


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);
}

Rob-Hague avatar Jul 25 '26 09:07 Rob-Hague

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Jul 25 '26 10:07 dotnet-policy-service

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Jul 25 '26 11:07 dotnet-policy-service

It's somehow intentional. All these methods are intended to be used with UTF-8/UTF-16 text. The implementation under {NonPacked}IndexOfAnyValueType etc has specialized optimizations for 8bit or 16bit chars.

The fallback implementation under SpanHelpers has loop unrolling. It's important to do a comparison for the performance.

huoyaoyuan avatar Jul 25 '26 11:07 huoyaoyuan

Yeah I assume a PR would be fine here if benchmarks show the path to be faster.

MichalPetryka avatar Jul 25 '26 17:07 MichalPetryka

We would consider a PR with benchmarks showing notable fast path differences. Thanks.

jeffhandley avatar Aug 16 '26 06:08 jeffhandley

I benchmarked this to check whether the vectorized SpanHelpers.*ValueType path is actually faster than the current scalar fallback for int/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, direct T.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 Actions ubuntu-latest runner (x64, confirmed AVX2 + AVX-512 via /proc/cpuinfo).

Arm64 (NEON) — int

Length Position Scalar (ns) Vectorized (ns) Speedup
4 notfound 0.509 0.233 2.19x
4 end 0.504 0.504 1.00x
4 start 0.242 0.487 0.50x
8 notfound 1.209 0.480 2.52x
8 end 0.979 0.763 1.28x
8 start 0.000 0.499 0.00x
16 notfound 2.765 1.501 1.84x
16 end 2.524 1.237 2.04x
16 start 0.000 0.503 0.00x
64 notfound 12.204 4.553 2.68x
64 end 11.961 4.576 2.61x
64 start 0.004 0.498 0.01x
256 notfound 50.960 18.696 2.73x
256 end 50.335 19.036 2.64x
256 start 0.000 0.438 0.00x
1024 notfound 206.682 97.946 2.11x
1024 end 206.925 91.292 2.27x
1024 start 0.000 0.495 0.00x
8192 notfound 1627.387 646.791 2.52x
8192 end 1611.261 659.156 2.44x
8192 start 0.000 0.509 0.00x
65536 notfound 12901.492 5095.651 2.53x
65536 end 12929.153 5105.680 2.53x
65536 start 0.019 0.497 0.04x

Arm64 (NEON) — long

Length Position Scalar (ns) Vectorized (ns) Speedup
2 notfound 0.413 0.236 1.75x
2 end 0.251 0.490 0.51x
2 start 0.266 0.494 0.54x
4 notfound 0.494 0.491 1.01x
4 end 0.576 0.747 0.77x
4 start 0.226 0.534 0.42x
8 notfound 1.284 0.974 1.32x
8 end 0.992 1.224 0.81x
8 start 0.000 0.492 0.00x
16 notfound 2.840 2.628 1.08x
16 end 2.544 2.232 1.14x
16 start 0.000 0.506 0.00x
64 notfound 12.264 8.911 1.38x
64 end 11.995 9.118 1.32x
64 start 0.002 0.501 0.00x
256 notfound 50.819 40.148 1.27x
256 end 50.518 48.854 1.03x
256 start 0.005 0.532 0.01x
1024 notfound 207.455 167.323 1.24x
1024 end 207.679 175.209 1.19x
1024 start 0.027 0.480 0.06x
8192 notfound 1645.569 1276.357 1.29x
8192 end 1653.048 1286.406 1.29x
8192 start 0.002 0.485 0.00x
65536 notfound 13004.227 10161.589 1.28x
65536 end 12986.635 10160.412 1.28x
65536 start 0.002 0.501 0.00x

x64 (AVX2 + AVX-512) — int

Length Position Scalar (ns) Vectorized (ns) Speedup
4 notfound 1.163 1.556 0.75x
4 end 1.130 1.542 0.73x
4 start 0.822 1.538 0.53x
8 notfound 2.732 1.554 1.76x
8 end 2.089 1.601 1.31x
8 start 0.549 1.593 0.34x
16 notfound 5.260 1.617 3.25x
16 end 4.657 1.833 2.54x
16 start 0.547 1.828 0.30x
64 notfound 19.815 5.415 3.66x
64 end 19.116 5.494 3.48x
64 start 0.548 1.837 0.30x
256 notfound 78.061 21.160 3.69x
256 end 76.484 21.789 3.51x
256 start 0.547 1.831 0.30x
1024 notfound 311.229 80.184 3.88x
1024 end 313.477 84.033 3.73x
1024 start 0.546 1.831 0.30x
8192 notfound 2506.188 644.707 3.89x
8192 end 2496.796 654.038 3.82x
8192 start 0.548 1.836 0.30x
65536 notfound 20236.753 5023.002 4.03x
65536 end 19924.820 5026.574 3.96x
65536 start 0.546 1.829 0.30x

x64 (AVX2 + AVX-512) — long

Length Position Scalar (ns) Vectorized (ns) Speedup
2 notfound 1.097 1.557 0.70x
2 end 1.075 1.538 0.70x
2 start 0.823 1.546 0.53x
4 notfound 1.219 1.559 0.78x
4 end 1.122 1.551 0.72x
4 start 0.839 1.606 0.52x
8 notfound 2.779 1.551 1.79x
8 end 2.081 1.831 1.14x
8 start 0.287 1.831 0.16x
16 notfound 5.313 2.342 2.27x
16 end 4.834 2.680 1.80x
16 start 0.284 1.829 0.16x
64 notfound 19.542 10.151 1.93x
64 end 18.906 13.784 1.37x
64 start 0.274 1.832 0.15x
256 notfound 77.954 41.931 1.86x
256 end 79.175 42.667 1.86x
256 start 0.275 1.852 0.15x
1024 notfound 310.340 160.970 1.93x
1024 end 309.171 159.075 1.94x
1024 start 0.276 1.828 0.15x
8192 notfound 2498.368 1284.968 1.94x
8192 end 2501.720 1296.288 1.93x
8192 start 0.276 1.828 0.15x
65536 notfound 20045.538 9971.562 2.01x
65536 end 19917.563 9954.582 2.00x
65536 start 0.275 1.830 0.15x

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 for int and ~2x for long on x64, ~2.7x/~1.4x on Arm64. The one place it loses is start (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.

TheNetherWatcher avatar Sep 05 '26 12:09 TheNetherWatcher

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.

aw0lid avatar Sep 13 '26 15:09 aw0lid

Yes, still on it — sorry for the delay. Opening the PR now with the benchmarked fast paths above.

TheNetherWatcher avatar Sep 14 '26 10:09 TheNetherWatcher