Skip to content

Commit b9ba9e6

Browse files
committed
Revert "Vectorized common String.Split() paths (dotnet#38001)"
This reverts commit 295bcdc.
1 parent 56e36a0 commit b9ba9e6

File tree

1 file changed

+54
-115
lines changed

1 file changed

+54
-115
lines changed

src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs

Lines changed: 54 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using System.Globalization;
77
using System.Numerics;
88
using System.Runtime.InteropServices;
9-
using System.Runtime.Intrinsics;
10-
using System.Runtime.Intrinsics.X86;
119
using System.Text;
1210
using Internal.Runtime.CompilerServices;
1311

@@ -1493,137 +1491,78 @@ private string[] SplitWithPostProcessing(ReadOnlySpan<int> sepList, ReadOnlySpan
14931491
/// <param name="sepListBuilder"><see cref="ValueListBuilder{T}"/> to store indexes</param>
14941492
private void MakeSeparatorList(ReadOnlySpan<char> separators, ref ValueListBuilder<int> sepListBuilder)
14951493
{
1496-
// Special-case no separators to mean any whitespace is a separator.
1497-
if (separators.Length == 0)
1494+
char sep0, sep1, sep2;
1495+
1496+
switch (separators.Length)
14981497
{
1499-
for (int i = 0; i < Length; i++)
1500-
{
1501-
if (char.IsWhiteSpace(this[i]))
1498+
// Special-case no separators to mean any whitespace is a separator.
1499+
case 0:
1500+
for (int i = 0; i < Length; i++)
15021501
{
1503-
sepListBuilder.Append(i);
1502+
if (char.IsWhiteSpace(this[i]))
1503+
{
1504+
sepListBuilder.Append(i);
1505+
}
15041506
}
1505-
}
1506-
}
1507-
1508-
// Special-case the common cases of 1, 2, and 3 separators, with manual comparisons against each separator.
1509-
else if (separators.Length <= 3)
1510-
{
1511-
char sep0, sep1, sep2;
1512-
sep0 = separators[0];
1513-
sep1 = separators.Length > 1 ? separators[1] : sep0;
1514-
sep2 = separators.Length > 2 ? separators[2] : sep1;
1515-
1516-
if (Length >= 16 && Sse41.IsSupported)
1517-
{
1518-
MakeSeparatorListVectorized(ref sepListBuilder, sep0, sep1, sep2);
1519-
return;
1520-
}
1507+
break;
15211508

1522-
for (int i = 0; i < Length; i++)
1523-
{
1524-
char c = this[i];
1525-
if (c == sep0 || c == sep1 || c == sep2)
1509+
// Special-case the common cases of 1, 2, and 3 separators, with manual comparisons against each separator.
1510+
case 1:
1511+
sep0 = separators[0];
1512+
for (int i = 0; i < Length; i++)
15261513
{
1527-
sepListBuilder.Append(i);
1514+
if (this[i] == sep0)
1515+
{
1516+
sepListBuilder.Append(i);
1517+
}
15281518
}
1529-
}
1530-
}
1531-
1532-
// Handle > 3 separators with a probabilistic map, ala IndexOfAny.
1533-
// This optimizes for chars being unlikely to match a separator.
1534-
else
1535-
{
1536-
unsafe
1537-
{
1538-
ProbabilisticMap map = default;
1539-
uint* charMap = (uint*)&map;
1540-
InitializeProbabilisticMap(charMap, separators);
1541-
1519+
break;
1520+
case 2:
1521+
sep0 = separators[0];
1522+
sep1 = separators[1];
15421523
for (int i = 0; i < Length; i++)
15431524
{
15441525
char c = this[i];
1545-
if (IsCharBitSet(charMap, (byte)c) && IsCharBitSet(charMap, (byte)(c >> 8)) &&
1546-
separators.Contains(c))
1526+
if (c == sep0 || c == sep1)
15471527
{
15481528
sepListBuilder.Append(i);
15491529
}
15501530
}
1551-
}
1552-
}
1553-
}
1554-
1555-
private void MakeSeparatorListVectorized(ref ValueListBuilder<int> sepListBuilder, char c, char c2, char c3)
1556-
{
1557-
// Redundant test so we won't prejit remainder of this method
1558-
// on platforms without SSE.
1559-
if (!Sse41.IsSupported)
1560-
{
1561-
throw new PlatformNotSupportedException();
1562-
}
1563-
1564-
// Constant that allows for the truncation of 16-bit (FFFF/0000) values within a register to 4-bit (F/0)
1565-
Vector128<byte> shuffleConstant = Vector128.Create(0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
1566-
1567-
Vector128<ushort> v1 = Vector128.Create(c);
1568-
Vector128<ushort> v2 = Vector128.Create(c2);
1569-
Vector128<ushort> v3 = Vector128.Create(c3);
1570-
1571-
ref char c0 = ref MemoryMarshal.GetReference(this.AsSpan());
1572-
int cond = Length & -Vector128<ushort>.Count;
1573-
int i = 0;
1574-
1575-
for (; i < cond; i += Vector128<ushort>.Count)
1576-
{
1577-
Vector128<ushort> charVector = ReadVector(ref c0, i);
1578-
Vector128<ushort> cmp = Sse2.CompareEqual(charVector, v1);
1579-
1580-
cmp = Sse2.Or(Sse2.CompareEqual(charVector, v2), cmp);
1581-
cmp = Sse2.Or(Sse2.CompareEqual(charVector, v3), cmp);
1582-
1583-
if (Sse41.TestZ(cmp, cmp)) { continue; }
1584-
1585-
Vector128<byte> mask = Sse2.ShiftRightLogical(cmp.AsUInt64(), 4).AsByte();
1586-
mask = Ssse3.Shuffle(mask, shuffleConstant);
1587-
1588-
uint lowBits = Sse2.ConvertToUInt32(mask.AsUInt32());
1589-
mask = Sse2.ShiftRightLogical(mask.AsUInt64(), 32).AsByte();
1590-
uint highBits = Sse2.ConvertToUInt32(mask.AsUInt32());
1591-
1592-
for (int idx = i; lowBits != 0; idx++)
1593-
{
1594-
if ((lowBits & 0xF) != 0)
1531+
break;
1532+
case 3:
1533+
sep0 = separators[0];
1534+
sep1 = separators[1];
1535+
sep2 = separators[2];
1536+
for (int i = 0; i < Length; i++)
15951537
{
1596-
sepListBuilder.Append(idx);
1538+
char c = this[i];
1539+
if (c == sep0 || c == sep1 || c == sep2)
1540+
{
1541+
sepListBuilder.Append(i);
1542+
}
15971543
}
1544+
break;
15981545

1599-
lowBits >>= 8;
1600-
}
1601-
1602-
for (int idx = i + 4; highBits != 0; idx++)
1603-
{
1604-
if ((highBits & 0xF) != 0)
1546+
// Handle > 3 separators with a probabilistic map, ala IndexOfAny.
1547+
// This optimizes for chars being unlikely to match a separator.
1548+
default:
1549+
unsafe
16051550
{
1606-
sepListBuilder.Append(idx);
1607-
}
1608-
1609-
highBits >>= 8;
1610-
}
1611-
}
1612-
1613-
for (; i < Length; i++)
1614-
{
1615-
char curr = Unsafe.Add(ref c0, (IntPtr)(uint)i);
1616-
if (curr == c || curr == c2 || curr == c3)
1617-
{
1618-
sepListBuilder.Append(i);
1619-
}
1620-
}
1551+
ProbabilisticMap map = default;
1552+
uint* charMap = (uint*)&map;
1553+
InitializeProbabilisticMap(charMap, separators);
16211554

1622-
static Vector128<ushort> ReadVector(ref char c0, int offset)
1623-
{
1624-
ref char ci = ref Unsafe.Add(ref c0, (IntPtr)(uint)offset);
1625-
ref byte b = ref Unsafe.As<char, byte>(ref ci);
1626-
return Unsafe.ReadUnaligned<Vector128<ushort>>(ref b);
1555+
for (int i = 0; i < Length; i++)
1556+
{
1557+
char c = this[i];
1558+
if (IsCharBitSet(charMap, (byte)c) && IsCharBitSet(charMap, (byte)(c >> 8)) &&
1559+
separators.Contains(c))
1560+
{
1561+
sepListBuilder.Append(i);
1562+
}
1563+
}
1564+
}
1565+
break;
16271566
}
16281567
}
16291568

0 commit comments

Comments
 (0)