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:
- We should allow passing
capacity to CreateStringBuilder to preallocate buffer if length is known
- 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
I benchmarked
ZStringagainst .NET built-inValueStringBuilder(not exposed publicly but we canstealextract 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:
capacitytoCreateStringBuilderto preallocate buffer if length is knownAppendis called with a 1-character string. Look at howValueStringBuilderdoes it. If length is1it just setsbuffer[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