Uh oh!
There was an error while loading. Please reload this page.
Remove pinning from StringBuilder - #64405
Conversation
ghost
commented
Jan 27, 2022
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. |
ghost
commented
Jan 27, 2022
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsSwitch from pointers to refs to avoid pinning the inputs. As part of this, I also consolidated the fast-path that was there specifically for string inputs to also apply to span inputs, char arrays, pointers, etc.
usingSystem;usingSystem.Linq;usingSystem.Runtime.CompilerServices;usingSystem.Text;usingSystem.Text.RegularExpressions;usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;[DisassemblyDiagnoser]publicclassProgram{publicstaticvoidMain(string[]args)=>BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);privateconstintIterations=1000;privateStringBuilder_sb=newStringBuilder(100_000);privatechar[]_chars;privatestring[]_strings;[Params("a","ab","abc","abcd","abcdefgh")]publicstringValue{get;set;}[GlobalSetup]publicvoidSetup(){_chars=Value.ToCharArray();_strings=Enumerable.Repeat(Value,4).ToArray();}[Benchmark(OperationsPerInvoke=Iterations)]publicvoidString(){_sb.Clear();for(inti=0;i<Iterations;i++)_sb.Append(Value);}[Benchmark(OperationsPerInvoke=Iterations)]publicvoidSpan(){_sb.Clear();for(inti=0;i<Iterations;i++)_sb.Append((ReadOnlySpan<char>)Value);}[Benchmark(OperationsPerInvoke=Iterations)]publicvoidCharArray(){_sb.Clear();for(inti=0;i<Iterations;i++)_sb.Append(_chars);}[Benchmark(OperationsPerInvoke=Iterations)]publicvoidAppendJoin(){_sb.Clear();for(inti=0;i<Iterations;i++){_sb.AppendJoin(", ",_strings);}}}
|
Switch from pointers to refs to avoid pinning the inputs. As part of this, I also consolidated the fast-path that was there specifically for string inputs to also apply to span inputs, char arrays, pointers, etc.
EgorBo
commented
Feb 1, 2022
Improvements on Win-x64: dotnet/perf-autofiling-issues#3219 |
stephentoub
commented
Feb 1, 2022
Nice, thanks. |
EgorBo
commented
Feb 1, 2022
Improvement for DateTimeOffset.ToString if it's related: dotnet/perf-autofiling-issues#3239 |
Likely. That test uses ToString(null) which will end up formatting into a StringBuilder. |
Switch from pointers to refs to avoid pinning the inputs.
As part of this, I also consolidated the fast-path that was there specifically for string inputs to also apply to span inputs, char arrays, pointers, etc.