Class SafeExactLengthArrayObjectPool<T>
A fast, dynamically-sized and thread-safe array pool to store arrays of an specific length.
public sealed class SafeExactLengthArrayObjectPool<T> : ArrayObjectPool<T>
Type Parameters
TType of element array to pool
- Inheritance
-
ObjectPool<T[]>SafeExactLengthArrayObjectPool<T>
- Inherited Members
Constructors
SafeExactLengthArrayObjectPool(int, bool)
Creates a pool of exact length array.
public SafeExactLengthArrayObjectPool(int length, bool shouldClearArrayOnReturnByDefault = false)
Parameters
lengthintLength of the pooled arrays.
shouldClearArrayOnReturnByDefaultboolIf this is true, buffers that will be stored to enable subsequent reuse in Return(T[]), will have their content cleared so that a subsequent consumer will not see the previous consumer's content.
If false or if the pool will release the buffer, the array's contents are left unchanged.
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
Exceptions
- ArgumentOutOfRangeException
Throw when value is lower than 1.
IsReserveFixed
Determines if the reserve pool is not allowed to grow nor shrink given its usage.
public bool IsReserveFixed { get; init; }
Property Value
Length
Determines the length of the pooled arrays.
public int Length { get; }
Property Value
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
Exceptions
- ArgumentOutOfRangeException
Throw when value is negative.
ShouldClearArrayOnReturnByDefault
Determines the default array clearing strategy.
If this is true, buffers that will be stored to enable subsequent reuse in Return(T[]), will have their content cleared so that a subsequent consumer will not see the previous consumer's content.
If false or if the pool will release the buffer, the array's contents are left unchanged.
public override bool ShouldClearArrayOnReturnByDefault { get; }
Property Value
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
-1instead 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
elementT[]Object to return.
Remarks
It uses a clearing policy specified by ShouldClearArrayOnReturnByDefault.
Return(T[], bool)
Return rented object to pool.
public override void Return(T[] element, bool clearArrayOnReturn)
Parameters
elementT[]Object to return.
clearArrayOnReturnboolIf this is true, buffers that will be stored to enable subsequent reuse in Return(T), will have their content cleared so that a subsequent consumer will not see the previous consumer's content.
If false or if the pool will release the buffer, the array's contents are left unchanged.
Trim(bool)
Trim the content of the pool.
public override void Trim(bool force = false)
Parameters
forceboolIf 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.
WithClearArrayOnReturn(bool)
Returns a instance of the pool in which the ShouldClearArrayOnReturnByDefault has been modified.
public override SafeExactLengthArrayObjectPool<T> WithClearArrayOnReturn(bool clearArrayOnReturnByDefault)
Parameters
clearArrayOnReturnByDefaultboolNew value for ShouldClearArrayOnReturnByDefault.
Returns
- SafeExactLengthArrayObjectPool<T>
An instance of the pool in which the ShouldClearArrayOnReturnByDefault has been modified.
It may be a new instance, a pooled one or the same instance if the values matches.