ZString icon indicating copy to clipboard operation
ZString copied to clipboard

Slower than .NET `ValueStringBuilder` & two ideas how to fix it

Open alex-jitbit opened this issue 3 months ago • 0 comments

I benchmarked ZString against .NET built-in ValueStringBuilder (not exposed publicly but we can steal extract the sources from https://source.dot.net/#System.Text.RegularExpressions/src/libraries/Common/src/System/Text/ValueStringBuilder.cs,42c1e0215cccb8b1 )

For some cases (when knowing length upfront and when appending 1-char strings) ValueStringBuilder is much faster.

Steps needed:

  1. We should allow passing capacity to CreateStringBuilder to preallocate buffer if length is known
  2. We should use an optimized path when Append is called with a 1-character string. Look at how ValueStringBuilder does it. If length is 1 it just sets buffer[pos++] = inputString[0] which is extremely fast, much faster than calling .CopyTo. https://source.dot.net/#System.Text.RegularExpressions/src/libraries/Common/src/System/Text/ValueStringBuilder.cs,42c1e0215cccb8b1

alex-jitbit avatar Nov 20 '25 08:11 alex-jitbit