LinkDotNet.StringBuilder 0.9.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package LinkDotNet.StringBuilder --version 0.9.4
NuGet\Install-Package LinkDotNet.StringBuilder -Version 0.9.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LinkDotNet.StringBuilder" Version="0.9.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LinkDotNet.StringBuilder --version 0.9.4
#r "nuget: LinkDotNet.StringBuilder, 0.9.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LinkDotNet.StringBuilder as a Cake Addin
#addin nuget:?package=LinkDotNet.StringBuilder&version=0.9.4

// Install LinkDotNet.StringBuilder as a Cake Tool
#tool nuget:?package=LinkDotNet.StringBuilder&version=0.9.4

StringBuilder

.NET Nuget GitHub tag

A fast and low allocation StringBuilder for .NET.

Getting Started

Install the package:

PM> Install-Package LinkDotNet.StringBuilder

Afterwards use the package as follow:

ValueStringBuilder stringBuilder = new ValueStringBuilder();
stringBuilder.AppendLine("Hello World");

string result = stringBuilder.ToString();

What does it solve?

The dotnet version of the StringBuilder is a all purpose version which normally fits a wide variety of needs. But sometimes low allocation is key. Therefore I created the ValueStringBuilder. It is not a class but a ref struct which tries to do as less allocations as possible. If you want to know how the ValueStringBuilder works and why it uses allocations and is even faster, checkout this blog post. The blog goes a bit more in detail how it works with a simplistic version of the ValueStringBuilder.

What it doesn't solve!

The library is not meant as a general replacement for the StringBuilder shipped with the .net framework itself. You can head over to the documentation and read about the "Known limitations". The library works best for a small to medium amount of strings (not multiple 100'000 characters, even though it can be still faster and uses less allocations). At anytime you can convert the ValueStringBuilder to a "normal" StringBuilder and vice versa.

The normal use case is to add concatenate strings in a hot-path where the goal is to put as minimal pressure on the GC as possible.

Documentation

A more detailed documentation can be found here.

Benchmark

The following table gives you a small comparison between the StringBuilder which is part of .NET, ZString and the ValueStringBuilder:

|              Method |       Mean |    Error |    StdDev |     Median | Ratio | RatioSD |   Gen 0 |  Gen 1 | Allocated |
|-------------------- |-----------:|---------:|----------:|-----------:|------:|--------:|--------:|-------:|----------:|
| DotNetStringBuilder |   401.7 ns | 29.15 ns |  84.56 ns |   373.4 ns |  1.00 |    0.00 |  0.3576 |      - |   1,496 B |
|  ValueStringBuilder |   252.8 ns |  9.05 ns |  26.27 ns |   249.0 ns |  0.65 |    0.13 |  0.1583 |      - |     664 B |
|  ZStringBuilderUtf8 | 1,239.0 ns | 44.93 ns | 131.06 ns | 1,192.0 ns |  3.18 |    0.56 | 15.6250 |      - |  66,136 B |
| ZStringBuilderUtf16 | 1,187.6 ns | 21.35 ns |  25.42 ns | 1,185.0 ns |  2.88 |    0.52 | 15.6250 | 0.0019 |  66,136 B |

For more comparison check the documentation.

Another benchmark shows that this ValueStringBuilder uses less memory when it comes to appending ValueTypes such as int, double, ...

|              Method |     Mean |    Error |   StdDev |  Gen 0 | Allocated |
|-------------------- |---------:|---------:|---------:|-------:|----------:|
| DotNetStringBuilder | 17.21 us | 0.622 us | 1.805 us | 1.5259 |      6 KB |
|  ValueStringBuilder | 16.24 us | 0.496 us | 1.462 us | 0.3357 |      1 KB |

Checkout the Benchmark for more detailed comparison and setup.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LinkDotNet.StringBuilder:

Package Downloads
DccUtils.TypeFormatting

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.20.0 2 5/2/2024
1.19.1 132 4/19/2024
1.19.0 1,471 3/2/2024
1.18.6 3,342 11/3/2023
1.18.5 2,327 10/19/2023
1.18.4 333 10/14/2023
1.18.3 3,392 9/22/2023
1.18.2 220 9/8/2023
1.18.1 18,302 8/10/2023
1.18.0 16,980 6/8/2023
1.17.0 3,217 4/13/2023
1.16.0 327 3/28/2023
1.15.0 197 3/26/2023
1.14.0 205 3/25/2023
1.13.1 266 3/17/2023
1.13.0 32,641 3/4/2023
1.12.2 268 2/21/2023
1.12.0 712 1/9/2023
1.11.5 296 1/9/2023
1.11.4 308 1/7/2023
1.11.3 302 1/3/2023
1.11.2 300 1/3/2023
1.11.1 311 1/1/2023
1.11.0 296 1/1/2023
1.10.6 301 12/30/2022
1.10.5 290 12/29/2022
1.10.4 319 12/27/2022
1.10.3 306 12/26/2022
1.10.2 334 12/16/2022
1.10.1 363 11/28/2022
1.10.0 356 11/20/2022
1.9.0 350 11/18/2022
1.8.0 345 11/15/2022
1.7.0 347 11/12/2022
1.6.2 354 11/11/2022
1.6.1 367 11/11/2022
1.6.0 339 11/10/2022
1.5.1 360 11/5/2022
1.5.0 360 11/5/2022
1.4.1 370 11/4/2022
1.4.0 451 10/11/2022
1.3.0 438 7/25/2022
1.2.0 501 4/20/2022
1.1.0 476 4/16/2022
1.0.1 442 4/13/2022
1.0.0 446 4/12/2022
0.9.5 455 4/10/2022
0.9.4 466 4/9/2022
0.9.3 449 4/9/2022
0.9.2 437 4/7/2022
0.9.1 433 4/6/2022
0.9.0 465 4/4/2022