Tedd.ObjectPool 2.0.0

dotnet add package Tedd.ObjectPool --version 2.0.0
                    
NuGet\Install-Package Tedd.ObjectPool -Version 2.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="Tedd.ObjectPool" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tedd.ObjectPool" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Tedd.ObjectPool" />
                    
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 Tedd.ObjectPool --version 2.0.0
                    
#r "nuget: Tedd.ObjectPool, 2.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.
#:package Tedd.ObjectPool@2.0.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=Tedd.ObjectPool&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Tedd.ObjectPool&version=2.0.0
                    
Install as a Cake Tool

Tedd.ObjectPool

High-performance, thread-safe object pool for reference types in .NET. Optimized for ultra-low overhead under contention with per-thread fast paths, avoiding locks on hot paths.

Thread-safety

  • Designed for multi-threaded use. Most hot-path operations avoid locks.
  • A per-thread single-item cache greatly reduces interlocked traffic for common allocate/free pairs.
  • Allocation attempts spread contention across slots using rotating indices.

Implementation and performance details

  • Fast paths kept tiny: Allocate/Free are aggressively inlined; slow paths are marked NoInlining to keep the I-cache hot.
  • Per-thread 1-slot cache: Most allocate/free pairs complete without touching shared memory.
  • Fast slot + array: One hot fast slot (_firstItem) plus an array of elements (size - 1).
  • Correct memory publication: Uses Volatile.Read/Write for unsynchronized accesses and a single CAS (Interlocked.CompareExchange) to claim/release.
  • Contention spreading: Rotating probe indices on allocate/free reduce CAS collisions and cache-line ping-pong.
  • Optional prefill: Prefill reduces first-use latency when the factory is expensive.
  • Overflow policy: Configurable disposal of IDisposable items when the pool is full; default is drop-on-floor to let GC reclaim.
  • DEBUG diagnostics: Optional leak tracking warns via Debug.WriteLine if an allocated item is never returned; ForgetTrackedObject lets you intentionally replace an item (e.g., upsizing a buffer) without spurious warnings.

Usage patterns

1) Simple allocate/free

var pool = new ObjectPool<byte[]>(
    factory: () => new byte[4096],
    cleanup: _ => { /* optional reset */ },
    size: 128
);

var buffer = pool.Allocate();
try
{
    // use buffer
}
finally
{
    pool.Free(buffer);
}

2) Scoped usage (auto-free)

pool.Scoped(Unit.Value, (sb, _) =>
{
    sb.AppendLine("Scoped work");
});

// Or with state:
pool.Scoped("hello", (sb, text) =>
{
    sb.Append(text);
});

readonly struct Unit { public static readonly Unit Value = default; }

3) Execute and deallocate helper

pool.AllocateExecuteDeallocate(
    action: sb => { sb.Append("work"); },
    cleanupAction: sb => sb.Clear() // optional extra cleanup for this call
);

4) Prefill to warm the pool

pool.Prefill(count: 32);

5) Dispose overflow when full (for IDisposable)

var socketPool = new ObjectPool<System.Net.Sockets.Socket>(
    factory: () => new System.Net.Sockets.Socket(System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp),
    cleanup: s => { /* reset if applicable */ },
    size: 32,
    disposeWhenFull: true // overflowed sockets are disposed instead of dropped
);

Thread-safety

  • Designed for multi-threaded use. Most hot-path operations avoid locks.
  • A per-thread single-item cache greatly reduces interlocked traffic for common allocate/free pairs.
  • Allocation attempts spread contention across slots using rotating indices.

When to use

  • Reuse of expensive-to-create reference types (e.g., StringBuilder, buffers, serializers, MemoryStream).
  • High-throughput services needing minimal allocation and contention.

Notes and limitations

  • T must be a reference type (class).
  • The pool does not own lifetime of items except when disposeWhenFull: true is enabled for overflow.
  • Dispose() only tears down internal thread-local storage; it does not dispose pooled items.
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Tedd.ObjectPool:

Package Downloads
Tedd.NetworkMessageProtocol

Simple and fast message based TCP network communication library.

Tedd.Profiler

Simple tool for measuring things within application. Implement measuring in application and pull numbers from ProfilerGroup. Thread safe, ~100% unit test code coverage.

Tedd.AsyncLock

Async locking mechanism

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 102 8/8/2025
1.0.1 1,128 11/3/2019
1.0.0 1,933 11/3/2019

Substantially faster than previous version.