Skip to content

Commit e9914e4

Browse files
hamarb123Copilot
andauthored
Optimize string.Split(char, ...) (#125379)
In some cases `string.Split(char, ...)` was slower (significantly) than the single `string` seperator overload - this PR optimizes the `char` overload/s to have better performance - the single-`char` overloads are the most common case, so I have added the additional optimisation just for that for now - benchmarks below. BENCHMARKS ARE OUT OF DATE Main benchmark that was the focus - single char splitter with rare/no seperators (~20% improvement on arm, ~47% improvement on x64) - `char` outperforms `string` now: X64 (AMD): <img width="1530" height="334" alt="image" src="https://github.com/user-attachments/assets/ad7c31b3-4220-4f3b-8349-76faf15d3d0c" /> Arm: <img width="738" height="157" alt="image" src="https://github.com/user-attachments/assets/5a3cb1b2-de37-4116-bd6d-c26eca91fb3c" /> Other cases have not regressed meaningfully (within margin of error). There's still up to about 15% performance available for .NET 12: #125379 (comment). --------- Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent 58dfec7 commit e9914e4

3 files changed

Lines changed: 290 additions & 43 deletions

File tree

‎src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Packed.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ private static int IndexOfAnyInRange<TNegator>(ref short searchSpace, short lowI
11401140

11411141
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11421142
[CompExactlyDependsOn(typeof(Avx512BW))]
1143-
private static Vector512<byte> PackSources(Vector512<short> source0, Vector512<short> source1)
1143+
internal static Vector512<byte> PackSources(Vector512<short> source0, Vector512<short> source1)
11441144
{
11451145
Debug.Assert(Avx512BW.IsSupported);
11461146
// Pack two vectors of characters into bytes. While the type is Vector256<short>, these are really UInt16 characters.
@@ -1152,7 +1152,7 @@ private static Vector512<byte> PackSources(Vector512<short> source0, Vector512<s
11521152

11531153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11541154
[CompExactlyDependsOn(typeof(Avx2))]
1155-
private static Vector256<byte> PackSources(Vector256<short> source0, Vector256<short> source1)
1155+
internal static Vector256<byte> PackSources(Vector256<short> source0, Vector256<short> source1)
11561156
{
11571157
Debug.Assert(Avx2.IsSupported);
11581158
// Pack two vectors of characters into bytes. While the type is Vector256<short>, these are really UInt16 characters.
@@ -1164,7 +1164,7 @@ private static Vector256<byte> PackSources(Vector256<short> source0, Vector256<s
11641164

11651165
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11661166
[CompExactlyDependsOn(typeof(Sse2))]
1167-
private static Vector128<byte> PackSources(Vector128<short> source0, Vector128<short> source1)
1167+
internal static Vector128<byte> PackSources(Vector128<short> source0, Vector128<short> source1)
11681168
{
11691169
Debug.Assert(Sse2.IsSupported);
11701170
// Pack two vectors of characters into bytes. While the type is Vector128<short>, these are really UInt16 characters.

0 commit comments

Comments
 (0)