Skip to content
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

Vectorise string.Replace(char, char) #279

Merged
merged 2 commits into from
Nov 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Text;
using Internal.Runtime.CompilerServices;

Expand Down Expand Up @@ -1055,63 +1056,55 @@ public string Replace(char oldChar, char newChar)
if (oldChar == newChar)
return this;

unsafe
{
int remainingLength = Length;

fixed (char* pChars = &_firstChar)
{
char* pSrc = pChars;
int firstIndex = IndexOf(oldChar);

while (remainingLength > 0)
{
if (*pSrc == oldChar)
{
break;
}
if (firstIndex < 0)
return this;

remainingLength--;
pSrc++;
}
}
int remainingLength = Length - firstIndex;
string result = FastAllocateString(Length);

if (remainingLength == 0)
return this;
int copyLength = firstIndex;

string result = FastAllocateString(Length);

fixed (char* pChars = &_firstChar)
{
fixed (char* pResult = &result._firstChar)
{
int copyLength = Length - remainingLength;
// Copy the characters already proven not to match.
if (copyLength > 0)
{
Buffer.Memmove(ref result._firstChar, ref _firstChar, (uint)copyLength);
}

// Copy the characters already proven not to match.
if (copyLength > 0)
{
wstrcpy(pResult, pChars, copyLength);
}
// Copy the remaining characters, doing the replacement as we go.
ref ushort pSrc = ref Unsafe.Add(ref Unsafe.As<char, ushort>(ref _firstChar), copyLength);
ref ushort pDst = ref Unsafe.Add(ref Unsafe.As<char, ushort>(ref result._firstChar), copyLength);

// Copy the remaining characters, doing the replacement as we go.
char* pSrc = pChars + copyLength;
char* pDst = pResult + copyLength;
if (Vector.IsHardwareAccelerated && remainingLength >= Vector<ushort>.Count)
{
Vector<ushort> oldChars = new Vector<ushort>(oldChar);
Vector<ushort> newChars = new Vector<ushort>(newChar);

do
{
char currentChar = *pSrc;
if (currentChar == oldChar)
currentChar = newChar;
*pDst = currentChar;

remainingLength--;
pSrc++;
pDst++;
} while (remainingLength > 0);
}
do
{
Vector<ushort> original = Unsafe.ReadUnaligned<Vector<ushort>>(ref Unsafe.As<ushort, byte>(ref pSrc));
Vector<ushort> equals = Vector.Equals(original, oldChars);
Vector<ushort> results = Vector.ConditionalSelect(equals, newChars, original);
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref pDst), results);

pSrc = ref Unsafe.Add(ref pSrc, Vector<ushort>.Count);
pDst = ref Unsafe.Add(ref pDst, Vector<ushort>.Count);
remainingLength -= Vector<ushort>.Count;
}
while (remainingLength >= Vector<ushort>.Count);
}

return result;
for (; remainingLength > 0; remainingLength--)
{
ushort currentChar = pSrc;
pDst = currentChar == oldChar ? newChar : currentChar;

pSrc = ref Unsafe.Add(ref pSrc, 1);
pDst = ref Unsafe.Add(ref pDst, 1);
}

return result;
}

public string Replace(string oldValue, string? newValue)
Expand Down