Show / Hide Table of Contents

Namespace Eco.Shared.Pools

Classes

ArrayPools

FixedSizePool<T>

Implementation of ThreadSafeFixedSizePool<T> with extended functionality. Obsolete, should be replaced with PoolService<T> when all functionality will be supported.

ListPool<T>

ListPool<T>.ReturnPromise

Disposable return promise which returns Value when disposed.

ObjectPoolExtensions

Extension methods for IObjectPool<T> instances.

PooledMemoryStream

PooledMemoryStream (based on https://github.com/itn3000/PooledStream). It uses .NET Standard System.Buffers.ArrayPool<> for buffer allocations. Also it has Truncate() which returns buffer to System.Buffers.ArrayPool<> and resets length, position and capacity to zero. It makes the stream reusable without holding extra memory for buffer.

PoolService<T>

IObjectPool<T> manager which simplifies pool usage, adding methods for automatic objects creation and cleaning up when returned to pool. When PoolService<T> created you should provide Eco.Shared.Pools.PoolService`1.factory method for new object instantiation (if pool is empty) and optionally Eco.Shared.Pools.PoolService`1.onReturn method which will be called on an object when it returned (added) to pool.

PoolService<T>.ReturnPromise

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.

ThreadSafePool<T>

Simple thread-safe System.Collections.Generic.Stack<> based pool implementation. It implements IObjectPool<T> interface and has unlimited pool size. For optimal performance you should prefer thread-safe lock free FixedSizePool<T>, but it has fixed pool size and can't be shrink or extended.

ThreadUnsafeFixedSizePool<T>

Thread-unsafe lock free System.Collections.Generic.Stack<> based pool implementation. It implements IObjectPool<T> interface and has fixed pool size. You should prefer it to use over thread-safe pool implementations where you're sure it won't be used from multiple threads (in example in Unity components).

ThreadUnsafePool<T>

Thread-unsafe lock free System.Collections.Generic.Stack<> based pool implementation. It implements IObjectPool<T> interface and has unlimited pool size. You should prefer it to use over thread-safe pool implementations where you're sure it won't be used from multiple threads (in example in Unity components).

Interfaces

IObjectPool<T>

Minimal Pool interface. If you wanna to have more advanced pool management you can use PoolService<T>.

☀
☾
In This Article
Back to top
Copyright (c) Strange Loop Games 2021
☀
☾