Performance utilities for .NET projects, including the MemoryStreamSlim, StringBuilderCache, InterlockedOps, and Compression classes.
$ dotnet add package KZDev.PerfUtilsThe KZDev.PerfUtils package provides the following high-performance utility classes:
MemoryStreamSlim: A high-performance, memory-efficient, and easy-to-use replacement for the MemoryStream class, offering significant performance benefits for large or frequently used streams.StringBuilderCache: A thread-safe cache of StringBuilder instances to improve speed and reduce the overhead of memory allocations associated with the StringBuilder class.InterlockedOps: A utility that extends the functionality of the Interlocked class in the .NET Class Library by providing additional atomic thread-safe operations.This sampling of performance benchmarks clearly demonstrates the advantages of using the KZDev.PerfUtils package:


For more details, refer to the benchmark related pages in the documentation.
MemoryStreamSlim is a drop-in replacement for the MemoryStream class, offering the following benefits:
StringBuilderCache is a static class that provides a thread-safe cache of StringBuilder instances, reducing allocations and deallocations in high-throughput scenarios. Key features include:
InterlockedOps is a static class that provides additional thread-safe atomic operations for integer types, including:
Below is an example of how to use the MemoryStreamSlim class. Other than instantiation using the Create method, the API is identical to the standard MemoryStream class. Note that it is always a best practice to dispose of the MemoryStreamSlim instance when it is no longer needed.
using KZDev.PerfUtils;
// Create a new MemoryStreamSlim instance
// For the best management of the memory buffers, it is very important to
// dispose of the MemoryStreamSlim instance when it is no longer needed.
using (MemoryStreamSlim stream = MemoryStreamSlim.Create())
{
// Write some data to the stream
stream.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
// Read the data back from the stream
stream.Position = 0;
byte[] buffer = new byte[5];
stream.Read(buffer, 0, 5);
}
Below is an example of how to use the StringBuilderCache class to acquire a StringBuilder instance, append strings to it, and then release it back to the cache. The GetStringAndRelease method is used to retrieve the string and release the StringBuilder instance back to the cache.
using KZDev.PerfUtils;
class Program
{
static void Main()
{
StringBuilder stringBuilder = StringBuilderCache.Acquire();
stringBuilder.Append("Hello, ");
stringBuilder.Append("World!");
Console.WriteLine(StringBuilderCache.GetStringAndRelease(stringBuilder));
}
}
Below is an example of how to use the InterlockedOps class to perform an atomic XOR operation on an integer variable. The Xor method toggles a bit flag between 1 and 0 in a thread-safe manner and returns a boolean value indicating whether the bit flag was set to 1.
using KZDev.PerfUtils;
public class XorExample
{
private int _flag;
public bool ToggleFlag ()
{
int originalValue = InterlockedOps.Xor(ref _flag, 1);
return originalValue == 0;
}
}
The MemoryStreamSlim class is conceptually similar to the RecyclableMemoryStream class from Microsoft. However, the internal implementation of buffer management is quite different. Compared to RecyclableMemoryStream, the MemoryStreamSlim class is designed to:
Performance comparisons are also available in the benchmarks documentation section.
Comprehensive documentation for the package is available on the PerfUtils Documentation page.
The roadmap for this package includes plans to add additional performance-focused utilities as time permits.