-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve vectorization of String.Split #64899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
826e568
e64d510
60d94e1
4c8c2b5
0cf5ff2
915ff82
dcadf05
8e7996c
51b102b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1615,8 +1615,7 @@ private void MakeSeparatorList(ReadOnlySpan<char> separators, ref ValueListBuild | |
| sep0 = separators[0]; | ||
| sep1 = separators.Length > 1 ? separators[1] : sep0; | ||
| sep2 = separators.Length > 2 ? separators[2] : sep1; | ||
|
|
||
| if (Length >= 16 && Sse41.IsSupported) | ||
| if (Vector128.IsHardwareAccelerated && Length >= Vector128<ushort>.Count * 2) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to double-check: the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes exactly, smaller strings doesn't perform as well |
||
| { | ||
| MakeSeparatorListVectorized(ref sepListBuilder, sep0, sep1, sep2); | ||
| return; | ||
|
|
@@ -1659,75 +1658,54 @@ private void MakeSeparatorList(ReadOnlySpan<char> separators, ref ValueListBuild | |
| private void MakeSeparatorListVectorized(ref ValueListBuilder<int> sepListBuilder, char c, char c2, char c3) | ||
| { | ||
| // Redundant test so we won't prejit remainder of this method | ||
| // on platforms without SSE. | ||
| if (!Sse41.IsSupported) | ||
| // on platforms where it is not supported | ||
| if (!Vector128.IsHardwareAccelerated) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the comment from the previous version (left side of comparison) to make it clear that this check is needed to avoid prejit.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll reintroduce the comment with a small text change since it's not limited to only SSE anymore |
||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| } | ||
|
|
||
| // Constant that allows for the truncation of 16-bit (FFFF/0000) values within a register to 4-bit (F/0) | ||
| Vector128<byte> shuffleConstant = Vector128.Create(0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); | ||
| Debug.Assert(Length >= Vector128<ushort>.Count); | ||
|
|
||
| nuint offset = 0; | ||
| nuint lengthToExamine = (nuint)(uint)Length; | ||
|
|
||
| ref ushort source = ref Unsafe.As<char, ushort>(ref _firstChar); | ||
|
|
||
| Vector128<ushort> v1 = Vector128.Create((ushort)c); | ||
| Vector128<ushort> v2 = Vector128.Create((ushort)c2); | ||
| Vector128<ushort> v3 = Vector128.Create((ushort)c3); | ||
|
|
||
| ref char c0 = ref MemoryMarshal.GetReference(this.AsSpan()); | ||
| int cond = Length & -Vector128<ushort>.Count; | ||
| int i = 0; | ||
|
|
||
| for (; i < cond; i += Vector128<ushort>.Count) | ||
| do | ||
| { | ||
| Vector128<ushort> charVector = ReadVector(ref c0, i); | ||
| Vector128<ushort> cmp = Sse2.CompareEqual(charVector, v1); | ||
|
|
||
| cmp = Sse2.Or(Sse2.CompareEqual(charVector, v2), cmp); | ||
| cmp = Sse2.Or(Sse2.CompareEqual(charVector, v3), cmp); | ||
| Vector128<ushort> vector = Vector128.LoadUnsafe(ref source, offset); | ||
| Vector128<ushort> v1Eq = Vector128.Equals(vector, v1); | ||
| Vector128<ushort> v2Eq = Vector128.Equals(vector, v2); | ||
| Vector128<ushort> v3Eq = Vector128.Equals(vector, v3); | ||
| Vector128<byte> cmp = (v1Eq | v2Eq | v3Eq).AsByte(); | ||
|
|
||
| if (Sse41.TestZ(cmp, cmp)) { continue; } | ||
|
|
||
| Vector128<byte> mask = Sse2.ShiftRightLogical(cmp.AsUInt64(), 4).AsByte(); | ||
| mask = Ssse3.Shuffle(mask, shuffleConstant); | ||
|
|
||
| uint lowBits = Sse2.ConvertToUInt32(mask.AsUInt32()); | ||
| mask = Sse2.ShiftRightLogical(mask.AsUInt64(), 32).AsByte(); | ||
| uint highBits = Sse2.ConvertToUInt32(mask.AsUInt32()); | ||
|
|
||
| for (int idx = i; lowBits != 0; idx++) | ||
| if (cmp != Vector128<byte>.Zero) | ||
| { | ||
| if ((lowBits & 0xF) != 0) | ||
| // Skip every other bit | ||
| uint mask = cmp.ExtractMostSignificantBits() & 0x5555; | ||
| do | ||
| { | ||
| sepListBuilder.Append(idx); | ||
| } | ||
|
|
||
| lowBits >>= 8; | ||
| uint bitPos = (uint)BitOperations.TrailingZeroCount(mask) / sizeof(char); | ||
| sepListBuilder.Append((int)(offset + bitPos)); | ||
| mask = BitOperations.ResetLowestSetBit(mask); | ||
| } while (mask != 0); | ||
| } | ||
|
|
||
| for (int idx = i + 4; highBits != 0; idx++) | ||
| { | ||
| if ((highBits & 0xF) != 0) | ||
| { | ||
| sepListBuilder.Append(idx); | ||
| } | ||
|
|
||
| highBits >>= 8; | ||
| } | ||
| } | ||
| offset += (nuint)Vector128<ushort>.Count; | ||
| } while (offset <= lengthToExamine - (nuint)Vector128<ushort>.Count); | ||
|
|
||
| for (; i < Length; i++) | ||
| while (offset < lengthToExamine) | ||
| { | ||
| char curr = Unsafe.Add(ref c0, (IntPtr)(uint)i); | ||
| char curr = (char)Unsafe.Add(ref source, offset); | ||
| if (curr == c || curr == c2 || curr == c3) | ||
| { | ||
| sepListBuilder.Append(i); | ||
| sepListBuilder.Append((int)offset); | ||
| } | ||
| } | ||
|
|
||
| static Vector128<ushort> ReadVector(ref char c0, int offset) | ||
| { | ||
| ref char ci = ref Unsafe.Add(ref c0, (IntPtr)(uint)offset); | ||
| ref byte b = ref Unsafe.As<char, byte>(ref ci); | ||
| return Unsafe.ReadUnaligned<Vector128<ushort>>(ref b); | ||
| offset++; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.