Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
Introducing FastStringBuilder
When I first discovered the powerful capabilities of the StringBuilder type in .NET in combination with object pooling, I realized how significantly it could reduce memory allocations and boost performance. This revelation inspired me to develop the FastStringBuilder as part of Spargine in the DotNetTips.Spargine.Core library. The primary goal of FastStringBuilder is to optimize string construction by leveraging a pooled StringBuilder. This approach not only minimizes memory allocations but also accelerates the execution of string-building operations, leading to more efficient and faster performance overall.
I’ve since integrated FastStringBuilder into Spargine and several other projects, where it has shown remarkable improvements in both speed and memory usage.
Key Methods of FastStringBuilder
Here is a breakdown of the core methods available in FastStringBuilder, designed to streamline string manipulation tasks while optimizing memory usage:
- BytesToString(readonly byte[] bytes)
Converts an array of bytes into a hexadecimal string representation, making it easy to inspect raw byte data in string form. This method is overloaded to use a ReadOnlySpan<byte>. - Combine(bool addLineFeed = false, params string[] args)
Combines an array of strings into a single string, optionally appending a line feed after each element if addLineFeed is set to true. - CombineWithSpace(params string[] args)
Combines an array of strings into a single string, inserting a space between each element. - Concat(char delimiter, bool addLineFeed = false, params string[] args)
Concatenates an array of strings using a specified delimiter, with an option to add a line feed after each element. This method is overloaded to accept a string delimiter, giving flexibility in how strings are joined. - Format(string format, params string[] args)
Formats a string using the specified format and arguments. - Join(IEnumerable<string> values, string delimiter)
Joins an enumerable collection of strings with a specified delimiter. Overloaded to accept a char delimiter as well, providing flexibility for various use cases. - PerformAction(Action<StringBuilder> action)
Executes a specified action on a StringBuilder instance. This method allows developers to manipulate the StringBuilder directly while leveraging the pooling mechanism for performance. - Remove(string input, string toRemove)
Removes all occurrences of the specified substring from the input string and returns the modified result. - ToDelimitedString<TKey, TValue>(in Dictionary<TKey, TValue> collection, char delimiter)
Converts a dictionary to a delimited string, useful for serialization or creating CSV-like outputs. This method efficiently builds a string representation of key-value pairs, with a specified delimiter.
Code Examples: How to Use FastStringBuilder
PerformAction() Example
Action<StringBuilder> action = (StringBuilder sb) =>
{
foreach (var word in stringArray)
{
_ = sb.Append(word);
}
};
var result = FastStringBuilder.PerformAction(action);
This code demonstrates how to perform actions on a pooled StringBuilder instance, improving memory efficiency while building a string from an array of words.
Concat() Example
var result = FastStringBuilder.Concat(delimiter: ControlChars.Comma, addLineFeed: true, args: words);
This example shows how to concatenate strings using a comma as a delimiter, optionally adding a line feed after each string.
ToDelimitedString() Example
var result = FastStringBuilder.ToDelimitedString(dictionary, ControlChars.Comma);
Here, a dictionary is converted to a comma-delimited string, ideal for data serialization or formatting.
Performance Improvements
To showcase the performance advantages of FastStringBuilder I’ve conducted a series of benchmarks on its key methods. The results highlight significant improvements in both speed and memory efficiency compared to traditional approaches. Here’s a breakdown of the findings:
- BytesToString(): 1.02x faster, providing a modest yet noticeable speed boost.
- Combine(): 11.04x faster and consumes 6.6x less memory, making it an excellent choice for string concatenation.
- CombineWithSpace(): 9x faster and uses 6x less memory, optimizing performance when adding spaces between strings.
- Concat(): When used with a char delimiter, it’s 9x faster and reduces memory usage by 5.3x. With a string delimiter, it’s still 9x faster and uses 6x less memory.
- Format(): 1.25x faster and allocates 2.7x less memory, improving efficiency in formatting strings.
- Join(): With a char delimiter, it’s 1.3x faster and uses 2x less memory. When using a string delimiter, it reduces memory usage by 1.12x.
- PerformAction(): 1.76x faster, providing a substantial performance gain in operations.
- Remove(): 1.8x faster and allocates 2x fewer bytes, making it more efficient for string manipulation.
- ToDelimitedString(): 1.4x faster and allocates about half the memory compared to standard methods, making it an excellent option for converting collections to delimited strings.
For the full benchmark results and methodology, check out the complete benchmark report.
FastStringBuilder offers a powerful alternative to the standard StringBuilder, optimizing both performance and memory usage.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

