Reservoir 1.4.0

dotnet add package Reservoir --version 1.4.0
                    
NuGet\Install-Package Reservoir -Version 1.4.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="Reservoir" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Reservoir" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Reservoir" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Reservoir --version 1.4.0
                    
#r "nuget: Reservoir, 1.4.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.
#:package Reservoir@1.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Reservoir&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Reservoir&version=1.4.0
                    
Install as a Cake Tool

Reservoir

Stop allocating the same thing twice.

Reservoir is thread-safe object pooling for .NET with 0 B warm paths and bounded shared retention. It ships as a small runtime library with public, library-friendly types and specialized generic policies.

NuGet CI/CD License: MIT

dotnet add package Reservoir

Requires a .NET Standard 2.0-compatible runtime 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 shared retention. You choose the shared tier's maximum idle-object count. Scoped rentals additionally retain one object per participating thread.
  • Capacity-aware storage. Cache-line-separated slots keep small pools fast; dense striped storage keeps large async working sets scalable.
  • Library-friendly delivery. One public assembly identity flows normally through PackageReference dependency graphs.
  • Scoped ownership. Stack-only leases return rentals automatically when synchronous work leaves scope.

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. It uses a per-pool thread-local fast path and retains one object per participating thread in addition to the bounded shared tier.

For performance-critical synchronous code, prefer RentScoped(out T) on .NET 10; its thread-local path is faster and avoids the ownership validation needed by repeated lease.Value access. On .NET 8, manual Rent() and Return() remain faster. Manual rental is also required when work crosses an await or total idle retention must stay within MaximumRetained. Measure on target hardware when nanoseconds matter.

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.

For synchronous scopes, RentScoped uses a per-pool thread-local fast path and returns the collection automatically:

using ListPool<int>.Lease lease = ListPool<int>.Shared.RentScoped(out List<int> values);
values.Add(42);
Consume(values);

The lease is stack-only and cannot cross an await. Manual Rent and Return keep using the bounded shared pool for asynchronous ownership.

CancellationTokenSourcePool.RentScoped uses the same per-pool thread-local strategy while preserving source reset and disposal semantics. Use Rent() or RentLinked() when ownership crosses an async boundary.

For synchronous, thread-affine hot paths, each specialized collection pool also exposes an opt-in ThreadLocalShared facade:

List<int> values = ListPool<int>.ThreadLocalShared.Rent();
try
{
    Consume(values);
}
finally
{
    ListPool<int>.ThreadLocalShared.Return(values);
}

It retains one item per participating thread, then falls back to the bounded Shared pool. This improves same-thread reuse but can retain items on idle threads and is not globally bounded by Shared.MaximumRetained.

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. 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 shared custom-object reuse, struct-policy specialization, scoped leases, or capacity-aware storage.

Use ArrayPool<T> for raw arrays. Use Microsoft.Extensions.ObjectPool when integration with Microsoft Extensions abstractions matters more than Reservoir's specialized policies and leases.

Go deeper

Documentation · Installation · API guide · Design notes

Reservoir is available under the MIT license.

Product 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 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 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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • 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

Respire

Fast, modern RESP client for Redis, Valkey, KeyDB, and other RESP-compatible servers.

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.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.0 3,144 8/13/2026
1.3.0 141 8/13/2026
1.2.3 184 8/13/2026
1.2.0 104 8/13/2026
1.1.2 109 8/12/2026
1.1.0 97 8/12/2026
1.0.1 120 8/12/2026
0.3.5 108 8/12/2026
0.3.3 84 8/12/2026
0.3.0 94 8/12/2026
0.2.0 104 8/11/2026
0.1.43 96 8/11/2026
0.1.35 87 8/11/2026