LinkDotNet.StringBuilder 1.5.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package LinkDotNet.StringBuilder --version 1.5.1
                    
NuGet\Install-Package LinkDotNet.StringBuilder -Version 1.5.1
                    
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="1.5.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LinkDotNet.StringBuilder" Version="1.5.1" />
                    
Directory.Packages.props
<PackageReference Include="LinkDotNet.StringBuilder" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LinkDotNet.StringBuilder --version 1.5.1
                    
#r "nuget: LinkDotNet.StringBuilder, 1.5.1"
                    
#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.
#addin nuget:?package=LinkDotNet.StringBuilder&version=1.5.1
                    
Install as a Cake Addin
#tool nuget:?package=LinkDotNet.StringBuilder&version=1.5.1
                    
Install as a Cake Tool

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:

using LinkDotNet.StringBuilder; // Namespace of the package

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. It is really important to understand how the ValueStringBuilder works so that you not run into weird situations where performance / allocations can even rise.

Benchmark

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

BenchmarkDotNet=v0.13.2, OS=macOS Monterey 12.6.1 (21G217) [Darwin 21.6.0]
Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
.NET SDK=7.0.100-rc.2.22477.23
  [Host]     : .NET 6.0.10 (6.0.1022.47605), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 6.0.10 (6.0.1022.47605), Arm64 RyuJIT AdvSIMD


|                         Method |       Mean |    Error |   StdDev | Ratio | RatioSD |    Gen0 | Allocated | Alloc Ratio |
|------------------------------- |-----------:|---------:|---------:|------:|--------:|--------:|----------:|------------:|
|            DotNetStringBuilder |   227.3 ns |  1.31 ns |  1.22 ns |  1.00 |    0.00 |  0.7114 |    1488 B |        1.00 |
|             ValueStringBuilder |   128.7 ns |  0.57 ns |  0.53 ns |  0.57 |    0.00 |  0.2677 |     560 B |        0.38 |
| ValueStringBuilderPreAllocated |   113.9 ns |  0.67 ns |  0.60 ns |  0.50 |    0.00 |  0.2677 |     560 B |        0.38 |
|             ZStringBuilderUtf8 | 1,976.1 ns | 17.33 ns | 15.37 ns |  8.69 |    0.08 | 31.2500 |   66120 B |       44.44 |
|            ZStringBuilderUtf16 | 1,888.2 ns | 18.50 ns | 16.40 ns |  8.30 |    0.09 | 31.2500 |   66120 B |       44.44 |

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 (7)

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

Package Downloads
DccUtils.TypeFormatting

Package Description

JsonhCs

JSON for Humans.

SystemCall

A command-parsing library inspired by Jinx.

BigReal

An arbitrary size and precision rational number stored using two BigIntegers.

HjsonSharp

A customisable streaming parser for HJSON, JSON, JSONC and JSON5.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.4.1 1,953 3/25/2025
2.4.0 2,179 2/21/2025
2.3.1 128 2/20/2025
2.3.0 148 2/16/2025
2.2.0 2,391 1/25/2025
2.1.0 325 1/14/2025
2.0.0 125 1/12/2025
1.22.0 288 12/18/2024
1.21.1 1,616 11/8/2024
1.21.0 1,832 9/20/2024
1.20.0 10,020 5/2/2024
1.19.1 258 4/19/2024
1.19.0 7,386 3/2/2024
1.18.6 5,248 11/3/2023
1.18.5 2,422 10/19/2023
1.18.4 414 10/14/2023
1.18.3 4,073 9/22/2023
1.18.2 283 9/8/2023
1.18.1 42,260 8/10/2023
1.18.0 27,934 6/8/2023
1.17.0 3,315 4/13/2023
1.16.0 417 3/28/2023
1.15.0 261 3/26/2023
1.14.0 282 3/25/2023
1.13.1 356 3/17/2023
1.13.0 39,809 3/4/2023
1.12.2 327 2/21/2023
1.12.0 1,180 1/9/2023
1.11.5 380 1/9/2023
1.11.4 377 1/7/2023
1.11.3 380 1/3/2023
1.11.2 379 1/3/2023
1.11.1 392 1/1/2023
1.11.0 377 1/1/2023
1.10.6 375 12/30/2022
1.10.5 357 12/29/2022
1.10.4 404 12/27/2022
1.10.3 368 12/26/2022
1.10.2 407 12/16/2022
1.10.1 443 11/28/2022
1.10.0 432 11/20/2022
1.9.0 427 11/18/2022
1.8.0 422 11/15/2022
1.7.0 424 11/12/2022
1.6.2 432 11/11/2022
1.6.1 443 11/11/2022
1.6.0 410 11/10/2022
1.5.1 431 11/5/2022
1.5.0 423 11/5/2022
1.4.1 443 11/4/2022
1.4.0 526 10/11/2022
1.3.0 529 7/25/2022
1.2.0 595 4/20/2022
1.1.0 567 4/16/2022
1.0.1 528 4/13/2022
1.0.0 542 4/12/2022
0.9.5 522 4/10/2022
0.9.4 567 4/9/2022
0.9.3 539 4/9/2022
0.9.2 518 4/7/2022
0.9.1 529 4/6/2022
0.9.0 577 4/4/2022