BetterBuffers 0.1.0
dotnet add package BetterBuffers --version 0.1.0
NuGet\Install-Package BetterBuffers -Version 0.1.0
<PackageReference Include="BetterBuffers" Version="0.1.0" />
<PackageVersion Include="BetterBuffers" Version="0.1.0" />
<PackageReference Include="BetterBuffers" />
paket add BetterBuffers --version 0.1.0
#r "nuget: BetterBuffers, 0.1.0"
#:package BetterBuffers@0.1.0
#addin nuget:?package=BetterBuffers&version=0.1.0
#tool nuget:?package=BetterBuffers&version=0.1.0
BetterBuffers
A high-performance replacement for .NET's ArrayPool that delivers similar or better performance with improved memory management.
Overview
BetterBuffers provides an optimized alternative to the standard .NET ArrayPool, addressing common memory management issues while maintaining or improving performance. It uses advanced algorithms to reduce memory fragmentation, minimize GC pressure, and optimize allocation/deallocation patterns in high-throughput scenarios.
Features
- Equivalent or better performance compared to standard ArrayPool in most scenarios
- Reduced memory fragmentation through intelligent buffer sizing and allocation
- Lower GC pressure with optimized memory management
- Thread-safe implementation suitable for concurrent applications
- API compatible with existing ArrayPool implementations for easy migration
- Customizable sizing and retention policies
Benchmarks
| Method | Mean | Error | StdDev | Median | Gen0 | Gen1 | Gen2 | Allocated |
|------------------------- |------------:|-----------:|-----------:|------------:|-----------:|-----------:|----------:|-------------:|
| UsingHeap_Short | 1,193.13 ms | 38.089 ms | 111.709 ms | 1,186.35 ms | 5000.0000 | 5000.0000 | 4000.0000 | 124702.31 MB |
| UsingArrayPool_Short | 1,906.81 ms | 63.321 ms | 186.703 ms | 1,906.36 ms | - | - | - | 19434.89 MB |
| UsingBetterBuffers_Short | 26.18 ms | 0.475 ms | 0.397 ms | 26.12 ms | 406.2500 | 406.2500 | - | 24.72 MB |
| Method | Mean | Error | StdDev | Median | Gen0 | Gen1 | Gen2 | Allocated |
|------------------------- |------------:|-----------:|-----------:|------------:|-----------:|-----------:|----------:|-------------:|
| UsingArrayPool_Long | 3,980.47 ms | 128.304 ms | 372.233 ms | 3,853.01 ms | 6000.0000 | 6000.0000 | 6000.0000 | 115406.41 MB |
| UsingBetterBuffers_Long | 1,625.22 ms | 29.842 ms | 27.914 ms | 1,626.48 ms | 25000.0000 | 12000.0000 | - | 1220.59 MB |
Installation
dotnet add package BetterBuffers
Quick Start
using BetterBuffers;
// Create a buffer pool
var pool = new BufferPool<byte>();
// Rent a buffer
byte[] arrayBuffer = pool.RentExactly(1024, initializeWithDefaultValues: true);
// For even better performance, use this:
// Memory<byte> memoryBuffer = pool.RentMemory(1024, initializeWithDefaultValues: false);
try
{
// Use the buffer...
ProcessData(buffer);
}
finally
{
// Return the buffer when done
pool.Return(buffer);
}
Documentation
See docs for tutorials, how-to guides, explanations, and reference information.
Migration from ArrayPool
BetterBuffers is designed to be a drop-in replacement for ArrayPool:
// Before:
using System.Buffers;
var pool = ArrayPool<byte>.Create();
// After:
using BetterBuffers;
var pool = new BufferPool<byte>();
Advanced Usage
Getting memory segments (improves performance)
Memory<T> is more flexible compared to T[], and can be obtained more efficiently compared to RentExactly()
Memory<byte> memoryBuffer = pool.RentMemory(1024, initializeWithDefaultValues: true);
Skipping value initialization (improves performance)
Use if you plan to overwrite the values in the buffer anyway
Memory<byte> memoryBuffer = pool.RentMemory(1024, initializeWithDefaultValues: false);
Exceptionless error handling
if (!pool.TryRentMemory(1024, initializeWithDefaultValues: true, out Memory<byte> buffer))
{
Console.WriteLine("Failed to allocate memory");
return;
}
ProcessData(buffer);
How It Works
BetterBuffers improves on .NET's ArrayPool implementation in several key ways:
- Gradually clears buffers that haven't been used in a while, reducing memory usage
- No arbitrary caps on how many buffers can be rented, or number of buckets
- More performant rental logic when a buffer is already available
- Better error handling
- Can provide exact array sizes when needed
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by .NET's ArrayPool implementation
- Performance testing methodology based on BenchmarkDotNet
Product | Versions 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
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 |
---|---|---|
0.1.0 | 10,698 | 5/19/2025 |