A fast and low allocation StringBuilder for .NET.
Install the package:
PM> Install-Package LinkDotNet.StringBuilder
Afterward, use the package as follow:
using LinkDotNet.StringBuilder; // Namespace of the package
using ValueStringBuilder stringBuilder = new();
stringBuilder.AppendLine("Hello World");
string result = stringBuilder.ToString();There are also smaller helper functions, which enable you to use ValueStringBuilder without any instance:
string result1 = ValueStringBuilder.Concat("Hello ", "World"); // "Hello World"
string result2 = ValueStringBuilder.Concat("Hello", 1, 2, 3, "!"); // "Hello123!"By default, ValueStringBuilder uses a rented buffer from ArrayPool<char>.Shared.
You can avoid renting overhead with an initially stack-allocated buffer:
using ValueStringBuilder stringBuilder = new(stackalloc char[128]);Note that this will prevent you from returning stringBuilder or assigning it to an out parameter.
The dotnet version of the StringBuilder is an all-purpose version that 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 that tries to allocate as little as possible.
On top of the ref struct design, it avoids boxing common value types (int, double, DateTime, Guid, and more) passed to AppendJoin, Concat, AppendFormat, and interpolated strings, and vectorizes Trim/TrimStart/TrimEnd via SearchValues<char>.
If you want to know how the ValueStringBuilder works and why it uses allocations and is even faster, check out this blog post.
The blog goes into a bit more in detail about how it works with a simplistic version of the ValueStringBuilder.
The library is not meant as a general replacement for the StringBuilder built into .NET. You can head over to the documentation and read about the "Known limitations".
The library works best for a small to medium length strings (not hundreds of thousands of characters, even though it can be still faster and performs fewer allocations). At any time, you can convert the ValueStringBuilder to a "normal" StringBuilder and vice versa.
The normal use case is to concatenate strings in a hot path where the goal is to put as minimal pressure on the GC as possible.
More detailed documentation can be found here. It is really important to understand how the ValueStringBuilder works so that you did not run into weird situations where performance/allocations can even rise.
The following table compares the built-in StringBuilder and this library's ValueStringBuilder:
BenchmarkDotNet v0.15.8, macOS Sequoia 15.7.9 (24G830) [Darwin 24.6.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 11.0.100-rc.1.26425.128
[Host] : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
DefaultJob : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
| Method | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio |
|-------------------- |----------:|---------:|---------:|------:|-------:|----------:|------------:|
| DotNetStringBuilder | 116.73 ns | 0.994 ns | 0.930 ns | 1.00 | 0.1779 | 1488 B | 1.00 |
| ValueStringBuilder | 65.71 ns | 0.637 ns | 0.596 ns | 0.56 | 0.0583 | 488 B | 0.33 |
For more comparisons, check the documentation.
ValueStringBuilder also avoids boxing value types (int, double, DateTime, Guid, and 16 more) passed to
AppendJoin, Concat, AppendFormat, ReplaceGeneric, and interpolated strings, and vectorizes Trim/TrimStart/TrimEnd
via SearchValues<char>. The following benchmark shows the combined effect against StringBuilder for a few representative
operations:
Operations, top to bottom: concatenating 5 mixed values, joining 10 ints with a separator, an interpolated string with 5 value-type holes, replacing a placeholder with a formatted int, and trimming a padded 1000-char buffer.
BenchmarkDotNet v0.15.8, macOS Sequoia 15.7.9 (24G830) [Darwin 24.6.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 11.0.100-rc.1.26425.128
[Host] : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
DefaultJob : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------- |----------:|----------:|----------:|-------:|-------:|----------:|
| StringBuilderConcat | 246.12 ns | 1.414 ns | 1.254 ns | 0.0792 | - | 664 B |
| StringBuilderAppendJoin | 54.12 ns | 0.132 ns | 0.117 ns | 0.0325 | - | 272 B |
| StringBuilderInterpolated | 251.53 ns | 0.805 ns | 0.753 ns | 0.0610 | - | 512 B |
| StringBuilderReplace | 49.31 ns | 0.127 ns | 0.112 ns | 0.0325 | - | 272 B |
| StringBuilderTrim | 899.23 ns | 11.791 ns | 11.029 ns | 0.7629 | 0.0114 | 6384 B |
| ValueStringBuilderConcat | 194.96 ns | 0.333 ns | 0.295 ns | 0.0210 | - | 176 B |
| ValueStringBuilderAppendJoin | 38.72 ns | 0.757 ns | 0.708 ns | 0.0076 | - | 64 B |
| ValueStringBuilderInterpolated | 148.65 ns | 1.721 ns | 1.526 ns | 0.0191 | - | 160 B |
| ValueStringBuilderReplace | 30.98 ns | 0.649 ns | 0.667 ns | 0.0038 | - | 32 B |
| ValueStringBuilderTrim | 132.67 ns | 0.511 ns | 0.453 ns | 0.0057 | - | 48 B |
Comparing each ValueStringBuilder row against its StringBuilder counterpart above:
| Operation | Time | Allocated |
|---|---|---|
| Concat | 0.79x (1.3x faster) | 0.27x (3.8x less) |
| AppendJoin | 0.72x (1.4x faster) | 0.24x (4.3x less) |
| Interpolated | 0.59x (1.7x faster) | 0.31x (3.2x less) |
| Replace | 0.63x (1.6x faster) | 0.12x (8.5x less) |
| Trim | 0.15x (6.8x faster) | 0.01x (133x less) |
Check out the Benchmark for a more detailed comparison and setup.
Thanks to all contributors and people that are creating bug-reports and valuable input: