KZDev.PerfUtils 1.1.0

dotnet add package KZDev.PerfUtils --version 1.1.0                
NuGet\Install-Package KZDev.PerfUtils -Version 1.1.0                
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="KZDev.PerfUtils" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KZDev.PerfUtils --version 1.1.0                
#r "nuget: KZDev.PerfUtils, 1.1.0"                
#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 KZDev.PerfUtils as a Cake Addin
#addin nuget:?package=KZDev.PerfUtils&version=1.1.0

// Install KZDev.PerfUtils as a Cake Tool
#tool nuget:?package=KZDev.PerfUtils&version=1.1.0                

KZDev.PerfUtils

This package contains the MemoryStreamSlim class; a high-performance, memory-efficient, and easy-to-use replacement for the MemoryStream class that provides particular performance benefits for large or frequently used streams. The package also contains the InterlockedOps class, which provides additional atomic thread-safe operations to extend the functionality of the Interlocked class in the .NET Class Library.

Features

MemoryStreamSlim is a drop-in replacement for the MemoryStream class that provides the following benefits:

  • Throughput performance is better than the standard MemoryStream.
  • Much lower memory traffic and far fewer garbage collections than the standard MemoryStream.
  • Eliminates Large Object Heap (LOH) fragmentation caused by frequent use and release of single-byte arrays used by the standard MemoryStream.
  • Simple replacement for MemoryStream with the same API, other than the constructor.
  • Optionally allows using native memory for storage, which allows even more flexibility to minimize GC pressure.

InterlockedOps is a static class providing the following thread-safe atomic operations:

  • Xor : Exclusive OR operation on any integer types.
  • ClearBits : Clear bits on any integer types.
  • SetBits : Set bits on any integer types.
  • ConditionAnd : Conditionally update bits using an AND operation on any integer types.
  • ConditionOr : Conditionally update bits using an OR operation on any integer types.
  • ConditionXor : Conditionally update bits using an XOR operation on any integer types.
  • ConditionClearBits : Conditionally clear bits on any integer types.
  • ConditionSetBits : Conditionally set bits on any integer types.

InterlockedOps Example

Below is an example of how to use the InterlockedOps class to perform an atomic XOR operation on an integer variable. The Xor method is used to toggle a bit flag between 1 and 0 in a thread-safe manner and returns a boolean value indicating if 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;
    }
}

MemoryStreamSlim Example

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. 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);
}

Compare to RecyclableMemoryStream

The MemoryStreamSlim class is similar in concept and purpose to the RecyclableMemoryStream class from Microsoft however the internal implementation of buffer management is quite different. Also, compared to RecyclableMemoryStream, the MemoryStreamSlim class is designed to:

  • 'Just work' and be easier to use without tuning parameters.
  • Be more flexible in most use cases.
  • Perform fewer memory allocations.
  • Incur fewer garbage collections.
  • Perform on par or better in terms of throughput performance.
  • Provide more consistent performance across different workloads.
  • Treat security as a priority and opt-out rather than opt-in zero'ing unused memory.
  • Optionally allow using native memory for storage to avoid GC pressure impact altogether.

Performance comparisons are also available in the Benchmarks documentation section.

Documentation

Full documentation for the package is available on the PerfUtils Documentation page.

Future Features

The roadmap plan for this package is to add several additional helpful performance focused utilities. These will be forthcoming as time permits.

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 is compatible.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 87 10/25/2024
1.0.0 90 10/13/2024

Further MemoryStreamSlim performance enhancements and added InterlockedOps class (version 1.1.0).