Table of Contents

Class FastObjectPool<T>

Namespace
Enderlook.Pools
Assembly
Enderlook.Pools.dll

A lightweight, fast, dynamically-sized and thread-safe object pool to store objects.
The pool is designed for fast rent and return of elements, so during multithreading scenarios it may accidentally free unnecessary objects during return (however, this is not a fatal error for the pool).

public sealed class FastObjectPool<T> : ObjectPool<T> where T : class

Type Parameters

T

Type of object to pool

Inheritance
FastObjectPool<T>
Inherited Members

Constructors

FastObjectPool(Func<T>)

Creates a pool of objects.

public FastObjectPool(Func<T> factory)

Parameters

factory Func<T>

Delegate used to construct instances of the pooled objects.
For a default delegate use CreateDefault<T>() or get_Factory<T>().

Exceptions

ArgumentNullException

Thrown when factory is null.

See Also

Properties

Capacity

Capacity of the pool.
This region of the pool support concurrent access.
The capacity should preferably be not greater than ProcessorCount * 2, since it's fully iterated before accessing the reserve.

public int Capacity { get; init; }

Property Value

int

Exceptions

ArgumentOutOfRangeException

Throw when value is lower than 1.

Factory

Delegate which instantiates new objects.

public Func<T> Factory { get; }

Property Value

Func<T>

IsReserveFixed

Determines if the reserve pool is not allowed to grow nor shrink given its usage.

public bool IsReserveFixed { get; init; }

Property Value

bool

Reserve

Current capacity of the reserve.
This reserve pool is only acceded when the non-reserve capacity gets full or empty.
This is because this region can only be acceded by a single thread
This pool has a dynamic size so this value represent the initial size of the pool which may enlarge or shrink over time.

public int Reserve { get; init; }

Property Value

int

Exceptions

ArgumentOutOfRangeException

Throw when value is negative.

Methods

ApproximateCount()

Gets an approximate count of the objects stored in the pool.
This value is not accurate and may be lower or higher than the actual count.
This is primarily used for debugging purposes.

public override int ApproximateCount()

Returns

int

Approximate count of elements in the pool. If this operation is not supported, return -1 instead of throwing.

Rent()

Rent an element from the pool.
If the pool is empty, instantiate a new element.
Implementors of this class can choose how elements are instantiated and initialized, or throw if instantiation of new elements is not supported.

public override T Rent()

Returns

T

Rented element.

Return(T)

Return rented object to pool.

public override void Return(T element)

Parameters

element T

Object to return.

Trim(bool)

Trim the content of the pool.

public override void Trim(bool force = false)

Parameters

force bool

If true, the pool is forced to clear all elements inside. Otherwise, the pool may only clear partially or not clear at all if the heuristic says so.

See Also