Reservoir 0.3.5
See the version list below for details.
dotnet add package Reservoir --version 0.3.5
NuGet\Install-Package Reservoir -Version 0.3.5
<PackageReference Include="Reservoir" Version="0.3.5"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Reservoir" Version="0.3.5" />
<PackageReference Include="Reservoir"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Reservoir --version 0.3.5
#r "nuget: Reservoir, 0.3.5"
#:package Reservoir@0.3.5
#addin nuget:?package=Reservoir&version=0.3.5
#tool nuget:?package=Reservoir&version=0.3.5
Reservoir
Stop allocating the same thing twice.
Reservoir is bounded, thread-safe object pooling for .NET with a 0 B warm general-purpose object-pool rent/return path. It ships as C# source, so the optimized code compiles into your assembly—no runtime dependency, version conflict, or extra DLL.
dotnet add package Reservoir
Requires .NET Standard 2.0 and C# 12 or later.
Why Reservoir?
- Zero general-purpose pool allocations when warm.
ObjectPool<T,TPolicy>rent and return reuse fixed slots without allocating nodes. Legacy collection fallbacks may trim or replace backing storage. - Bounded retention. You choose the maximum number of idle objects; the pool cannot grow without limit.
- Capacity-aware storage. Cache-line-separated slots keep small pools fast; dense striped storage keeps large async working sets scalable.
- Source-only delivery. Reservoir stays private to your project and adds no runtime package or assembly.
- Ownership guardrails. Debug builds detect invalid returns and report leaked rentals.
Rent. Work. Return.
Define lifecycle behavior as a struct policy so the JIT can specialize and inline it:
using Reservoir;
var pool = new ObjectPool<Buffer, BufferPolicy>(maxCapacity: 64);
using var lease = pool.RentScoped(out Buffer buffer);
buffer.Write(payload);
sealed class Buffer
{
public int Length { get; set; }
public void Write(ReadOnlySpan<byte> value) => Length += value.Length;
}
readonly struct BufferPolicy : IPooledObjectPolicy<Buffer>
{
public Buffer Create() => new();
public bool TryReset(Buffer buffer)
{
buffer.Length = 0;
return true;
}
}
Create() handles a miss. TryReset() prepares an object for reuse or returns false to discard it. Discarded IDisposable objects are disposed automatically; implement IPooledObjectDestroyPolicy<T> for custom cleanup. The scoped lease guarantees return when control leaves the synchronous scope.
For work that crosses an await, use Rent() and return the object in finally. See the quick start for both patterns.
Pools included
Reservoir includes ready-to-use pools for:
List<T> · Dictionary<TKey,TValue> · HashSet<T> · Queue<T> · Stack<T> · StringBuilder · CancellationTokenSource
List<int> values = ListPool<int>.Shared.Rent();
try
{
values.Add(42);
Consume(values);
}
finally
{
ListPool<int>.Shared.Return(values);
}
Collections return empty. Oversized backing stores are discarded instead of retained. Each pool exposes a shared instance and constructors for custom limits.
The ownership rule
Returning an object transfers ownership to the pool. Do not touch it, return it twice, or return it to another pool.
Another thread may rent the same object immediately. Debug diagnostics make violations visible and report rentals that become unreachable without being returned. Read the complete ownership rules.
Measured, not promised
BenchmarkDotNet 0.15.8 MediumRun, .NET 10.0.11, Windows 11, AMD EPYC 9V74:
| Method | Mean | Ratio | Allocated |
|---|---|---|---|
new |
11.83 ns | 1.00 | 304 B |
| Reservoir | 10.36 ns | 0.88 | 0 B |
Microsoft.Extensions.ObjectPool |
11.57 ns | 0.98 | 0 B |
ConcurrentBag<T> pool |
25.19 ns | 2.13 | 0 B |
Every measured warm Reservoir path allocated 0 B. Timings vary by machine; compare methods within the same run.
See all benchmark results or reproduce them locally:
dotnet run -c Release -f net10.0 --project benchmarks/Reservoir.Benchmarks -- --filter "*" --job Short --runtimes net8.0 net10.0 --apples
When it fits
Choose Reservoir when you want bounded custom-object reuse, source ownership, struct-policy specialization, scoped leases, or debug ownership diagnostics.
Use ArrayPool<T> for raw arrays. Use Microsoft.Extensions.ObjectPool when ecosystem integration matters more than source-only delivery.
Go deeper
Documentation · Installation · API guide · Design notes
Reservoir is available under the MIT license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Reservoir:
| Package | Downloads |
|---|---|
|
Dekaf
High-performance, pure C# Apache Kafka client library for .NET |
|
|
Kevlar
Kevlar is a fast, allocation-conscious resilience library for .NET: retries, circuit breakers, timeouts, rate limiting, concurrency limiting, hedging and fallbacks, composed through one fluent Shield API. |
|
|
Respire
Fast, modern RESP client for Redis, Valkey, KeyDB, and other RESP-compatible servers. |
GitHub repositories
This package is not used by any popular GitHub repositories.