Uh oh!
There was an error while loading. Please reload this page.
Optimize StringBuilder by embracing spans and ISpanFormattable more. - #58907
Optimize StringBuilder by embracing spans and ISpanFormattable more.#58907teo-tsirpanis wants to merge 13 commits into
StringBuilder by embracing spans and ISpanFormattable more.#58907Conversation
ghost
commented
Sep 9, 2021
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
GrabYourPitchforks
commented
Sep 9, 2021
A note on See also #47186. |
ghost
commented
Sep 9, 2021
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsThis PR changes the implementation of For example, the Speaking of
|
And streamline the character array Insert overload.
acbbb00 to
85bcf34Compareteo-tsirpanis
commented
Sep 9, 2021
OK @GrabYourPitchforks, I reverted it. |
danmoseley
commented
Sep 9, 2021
@teo-tsirpanis could you please check StringBuilder has decent coverage in dotnet/performance and use the instructions in that repo to generate a before/after table? |
teo-tsirpanis
commented
Sep 9, 2021
Unfortunately I can't do that; when I had tried to build the runtime, my laptop ran out of space. I will however take a look at the |
teo-tsirpanis
commented
Sep 9, 2021
Some tests are failing. I will investigate it soon. |
danmoseley
commented
Sep 10, 2021
The only relevant failure seems to be Append_Bool_NoSpareCapacity_ThrowsArgumentOutOfRangeException where the parameter name changed. We're usually OK changing the parameter name if there's a reason. |
And move the capacity check in Append(ReadOnlySpan<char>).
10efdae to
16488d5CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
teo-tsirpanis
commented
Sep 13, 2021
Feedback was addressed. |
danmoseley
commented
Oct 2, 2021
@stephentoub do you have further feedback? |
stephentoub
commented
Oct 6, 2021
The changes LGTM, but can you share before/after numbers from running https://github.com/dotnet/performance/blob/main/src/benchmarks/micro/libraries/System.Text/Perf.StringBuilder.cs ? |
teo-tsirpanis
commented
Oct 6, 2021
Unfortunately I can't provide benchmarks, as explained earlier. I even tried copy-pasting |
stephentoub
commented
Oct 6, 2021
Ok
@danmoseley, we need benchmarks validated before this can be merged. |
You should be able to run the existing benchmarks if you do: And then in the performance repo (not 100% positive the filter is correct below): You can also do it if you build the |
teo-tsirpanis
commented
Oct 6, 2021
Sorry, still can't do it. My machine's SSD runs out of space when I try to build the runtime. I tried to do it in a Codespace but still ran into some issues. |
teo-tsirpanis
commented
Oct 15, 2021
stephentoub
commented
Feb 7, 2022
@teo-tsirpanis, I think much of this PR was obsoleted by #64405. Shall we close it and you can open a new one focusing on just the portions you think are still relevant? Thanks! |
teo-tsirpanis
commented
Feb 7, 2022
OK, your solution seems better, great work. I will open a new PR soon. |
This PR changes the implementation of
StringBuilderto completely eliminate pinning by moving all pointer-based implementations to spans.For example, the
Appendoverload that takes aReadOnlySpanof characters used to be implemented by pinning the span and forwarding to theAppendoverload that takes a pointer and a length. Now, the roles are reversed: the pointer overload calls theReadOnlySpanoverload, which does the real work, and to which other overloads ofAppendare now forwarded as well. Something similar also happened with theInsertfamily of methods.Speaking of
Insert, the overloads that take a primitive were optimized to take advantage of theISpanFormattableinterface, eliminating a temporary string allocation most of the time.