-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Optimized string.Replace(char, char) #67049
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 10 commits
549d7c4
7dfe855
5232726
a442549
4e99ac4
30889ac
8627f6f
ed83650
5d92816
0ee90f4
0a7ca74
c65192a
35679cb
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 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -994,7 +994,7 @@ public string Replace(char oldChar, char newChar) | |||||||||||||||||||||||||||
| if (firstIndex < 0) | ||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| int remainingLength = Length - firstIndex; | ||||||||||||||||||||||||||||
| nint remainingLength = (nint)(uint)(Length - firstIndex); | ||||||||||||||||||||||||||||
| string result = FastAllocateString(Length); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| int copyLength = firstIndex; | ||||||||||||||||||||||||||||
|
|
@@ -1006,35 +1006,56 @@ public string Replace(char oldChar, char newChar) | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // 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); | ||||||||||||||||||||||||||||
| ref ushort pSrc = ref Unsafe.Add(ref GetRawStringDataAsUshort(), (nint)(uint)copyLength); | ||||||||||||||||||||||||||||
| ref ushort pDst = ref Unsafe.Add(ref result.GetRawStringDataAsUshort(), (nint)(uint)copyLength); | ||||||||||||||||||||||||||||
| nint i = 0; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (Vector.IsHardwareAccelerated && remainingLength >= Vector<ushort>.Count) | ||||||||||||||||||||||||||||
| if (Vector.IsHardwareAccelerated && Length >= Vector<ushort>.Count) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| Vector<ushort> oldChars = new Vector<ushort>(oldChar); | ||||||||||||||||||||||||||||
| Vector<ushort> newChars = new Vector<ushort>(newChar); | ||||||||||||||||||||||||||||
| Vector<ushort> oldChars = new(oldChar); | ||||||||||||||||||||||||||||
| Vector<ushort> newChars = new(newChar); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| do | ||||||||||||||||||||||||||||
| Vector<ushort> original; | ||||||||||||||||||||||||||||
| Vector<ushort> equals; | ||||||||||||||||||||||||||||
| Vector<ushort> results; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| nint lengthToExamine = remainingLength - Vector<ushort>.Count; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (lengthToExamine > 0) | ||||||||||||||||||||||||||||
danmoseley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||
| do | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| original = Vector.LoadUnsafe(ref pSrc, i); | ||||||||||||||||||||||||||||
| equals = Vector.Equals(original, oldChars); | ||||||||||||||||||||||||||||
| results = Vector.ConditionalSelect(equals, newChars, original); | ||||||||||||||||||||||||||||
| results.StoreUnsafe(ref pDst, i); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| i += Vector<ushort>.Count; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| while (i < lengthToExamine); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| while (remainingLength >= Vector<ushort>.Count); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for (; remainingLength > 0; remainingLength--) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| ushort currentChar = pSrc; | ||||||||||||||||||||||||||||
| pDst = currentChar == oldChar ? newChar : currentChar; | ||||||||||||||||||||||||||||
| // There are [0, Vector<ushort>.Count) elements remaining now. | ||||||||||||||||||||||||||||
| // As the operation is idempotent, and we know that in total there are at least Vector<ushort>.Count | ||||||||||||||||||||||||||||
| // elements available, we read a vector from the very end of the string, perform the replace | ||||||||||||||||||||||||||||
| // and write to the destination at the very end. | ||||||||||||||||||||||||||||
| // Thus we can eliminate the scalar processing of the remaining elements. | ||||||||||||||||||||||||||||
| // We perform this operation even if there are 0 elements remaining, as it is cheaper than the | ||||||||||||||||||||||||||||
| // additional check which would introduce a branch here. | ||||||||||||||||||||||||||||
|
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.
Can you quantify this? Even with good branch prediction it's still more expensive?
Member
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. It's hard to pour this statement into numbers, as with a BDN-benchmark the branch predictor will very likely do a great job (they got really smart over the last generation of cpus). In contrast to real-world usage I assume that it is more likely to have When I start working on Vector128/256 support for |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
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. Perhaps worth adding an assert that current
Member
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. Hm, I think in this case a test should fail?
Member
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. Tests cover these cases, so I don't see a need for the Debug.Assert -- but I'll add it of course if you want. runtime/src/libraries/Common/tests/Tests/System/StringTests.cs Lines 4566 to 4578 in 8ed8517
|
||||||||||||||||||||||||||||
| pSrc = ref Unsafe.Add(ref pSrc, 1); | ||||||||||||||||||||||||||||
| pDst = ref Unsafe.Add(ref pDst, 1); | ||||||||||||||||||||||||||||
| i = (nint)(uint)Length - Vector<ushort>.Count; | ||||||||||||||||||||||||||||
| original = Vector.LoadUnsafe(ref GetRawStringDataAsUshort(), i); | ||||||||||||||||||||||||||||
| equals = Vector.Equals(original, oldChars); | ||||||||||||||||||||||||||||
| results = Vector.ConditionalSelect(equals, newChars, original); | ||||||||||||||||||||||||||||
| results.StoreUnsafe(ref result.GetRawStringDataAsUshort(), i); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| for (; i < remainingLength; ++i) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| ushort currentChar = Unsafe.Add(ref pSrc, i); | ||||||||||||||||||||||||||||
| Unsafe.Add(ref pDst, i) = currentChar == oldChar ? newChar : currentChar; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.