Show / Hide Table of Contents

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>
Implements
IObjectPool<T>
Namespace: Eco.Shared.Pools
Assembly: Eco.Shared.dll
Syntax
public class ThreadSafeFixedSizePool<T> : Object, IObjectPool<T> where T : class
Type Parameters
Name Description
T

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()

Clears cache.

Declaration
public void Clear()

Get()

Returns a object from the pool or null if no objects in the pool.

Declaration
public T Get()
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

IObjectPool<T>

Extension Methods

CommandLine.FeedFromCommandLine(Object)
CommandLine.ToCommandLineArgs(Object, Func<Object, Boolean>)
ListUtil.DepthFirstTraversal<T>(T, Func<T, IEnumerable<T>>)
EnumerableExtensions.SingleItemAsEnumerable<T>(T)
EventUtils.RaiseEvent<TEventArgs>(Object, String, TEventArgs)
PredicateUtils.MatchesAll<TEnumerable, T>(T, TEnumerable)
PredicateUtils.MatchesAll<T>(T, Func<T, Boolean>[])
PredicateUtils.MatchesAny<TEnumerable, T>(T, TEnumerable)
ReflectionUtils.PropertyValue<T>(Object, PropertyInfo)
ReflectionUtils.TryGetPropertyValueByName<T>(Object, String, out T)
ReflectionUtils.GetPropertyValueByName<T>(Object, String)
ReflectionUtils.SetPropertyByName(Object, String, Object)
ReflectionUtils.GetStructPropertyByName<T>(Object, String)
ReflectionUtils.GetStringPropertyByName(Object, String)
ReflectionUtils.ZipByProperty<T>(Object, Object, Object, Func<T, T, T>)
☀
☾
In This Article
Back to top
Copyright (c) Strange Loop Games 2021
☀
☾