KZDev.PerfUtils 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package KZDev.PerfUtils --version 1.0.0                
NuGet\Install-Package KZDev.PerfUtils -Version 1.0.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.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KZDev.PerfUtils --version 1.0.0                
#r "nuget: KZDev.PerfUtils, 1.0.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.0.0

// Install KZDev.PerfUtils as a Cake Tool
#tool nuget:?package=KZDev.PerfUtils&version=1.0.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 benefits for large or frequently used streams.

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.

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.
  • Optionally allow using native memory for storage to avoid GC pressure impact altogether.

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, so this first release is focused just on the MemoryStreamSlim class.

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

Initial release (version 1.0.0)