Class ThreadSafeFixedSizePool<T>
Fixed Size Pool used to avoid any extra-GC allocations, unless requesting more items from pool than are available.
It uses circular buffer where
tail
points to first element which will be retrieved from pool on request (read position)
head
points to next write position which will be used for returning element to pool (write position)
when head
== tail
then buffer is empty
when head
== tail - 1
then buffer is full
we're using poolSize + 1
for capacity to maintain full buffer scenario, because otherwise we can have two situations when write position == read position (empty and full).
Performance:
~2x faster in concurrent environment than basic Stack based pool implementation with lock
~10% faster in single-thread environment than basic Stack based pool implementation with lock.
Inheritance
System.Object
ThreadSafeFixedSizePool<T>
Assembly: Eco.Shared.dll
Syntax
public class ThreadSafeFixedSizePool<T> : Object, IObjectPool<T> where T : class
Type Parameters
Constructors
ThreadSafeFixedSizePool(Int32, Boolean)
Declaration
public ThreadSafeFixedSizePool(int poolSize, bool ensureReturnFromSameThread = false)
Parameters
Type |
Name |
Description |
System.Int32 |
poolSize |
|
System.Boolean |
ensureReturnFromSameThread |
|
Fields
MaxPoolSize
Declaration
public const int MaxPoolSize = 65533
Field Value
Type |
Description |
System.Int32 |
|
Properties
Count
Declaration
public int Count { get; }
Property Value
Type |
Description |
System.Int32 |
|
MaxSize
Max number of elements in the pool.
Declaration
public int MaxSize { get; }
Property Value
Type |
Description |
System.Int32 |
|
Methods
CheckDuplicates(T, Int64)
Declaration
public void CheckDuplicates(T obj, long writePos)
Parameters
Type |
Name |
Description |
T |
obj |
|
System.Int64 |
writePos |
|
Clear()
Declaration
Get()
Returns a object from the pool or null if no objects in the pool.
Declaration
Returns
Type |
Description |
T |
object from the pool or a new object.
|
TryAdd(T)
Tries to add object to the pool (if enough capacity in the pool).
Declaration
public bool TryAdd(T obj)
Parameters
Type |
Name |
Description |
T |
obj |
|
Returns
Type |
Description |
System.Boolean |
|
Implements
Extension Methods