What is the difference between InplaceStringBuilder and StringBuilder?

Today in VS2017 intellisense popped up InplaceStringBuilder

when I tried to type StringBuilder

. InplaceStringBuilder

newbie to me, so I started digging to see what I can learn.

The first thing I noticed is that the information about the structure, not the class and type, looks like this:

#region Assembly Microsoft.Extensions.Primitives, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// C:\Users\Ron Clabo\Documents\Visual Studio 2017\Projects\wwwGiftOasisResponsive\packages\Microsoft.Extensions.Primitives.1.1.0\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll
#endregion

using System.Diagnostics;

namespace Microsoft.Extensions.Primitives {
    [DebuggerDisplay("Value = {_value}")]
    public struct InplaceStringBuilder {
        public InplaceStringBuilder(int capacity);

        public int Capacity { get; set; }

        public void Append(string s);
        public void Append(char c);
        public override string ToString();
    }
}

      

Thus, it has far fewer methods than StringBuilder

. Then I googled around to find out more about InplaceStringBuilder

, but there isn't much on the internet yet, so it looks pretty new.

Also, the differences that I have already mentioned, what are the differences between InplaceStringBuilder

and StringBuilder

; and when should a developer use the new one InplaceStringBuilder

instead of the old one StringBuilder

?

+3


source to share


2 answers


Normal StringBuilder

increases capacity when the resulting string is larger than the starting one. InplaceStringBuilder

limited by capacity and throws an exception if the resulting string is larger.

This is a rather limited limitation InplaceStringBuilder

, so it may only be useful in rare cases. In addition, if you know the bandwidth in advance, you could already determine the initial normal capacity StringBuilder

.



Source: Look at the implementation InplaceStringBuilder

on GitHub and compare with StringBuilder

on MSDN

+3


source


Since it only has one selection, it is InplaceStringBuilder

more efficient for well-known reasonably sized rows. We could use it in methods that should be very effective.

Introduced with Pull Request # 157 , which includes the following comment.

Intended to be used instead of pooling StringBuilder

or string.Concat

when all parts of the string are known ... Only one selection of the resulting string ... should only be used for known reasonably sized strings. For everything else use StringBuilder

... don't use waiting points ...



The PR story tells the story:

  • July 2016, Issue # 676 notifies unnecessary allocations.
  • September 2016, Pull request # 699 resolves issue # 676 and suggests decomposing inplace string formatting into a structure ...
  • September 2016, Issue # 717 formalizes this proposal.
  • September 2016, Request Pull # 157 implements this proposal.
+3


source







All Articles