Class ScyllaGenericObjectPool<T>
High-performance object pool for any type T.
Stores available instances in an array-backed free-list (LIFO).
Unsynchronized by default; use AsSynchronized(object) to obtain a synchronized wrapper.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaGenericObjectPool<T> : IScyllaCollection<T>, IScyllaCollection
Type Parameters
| Name | Description |
|---|---|
| T | Pooled type |
Remarks
// Direct construction
var pool = new ScyllaGenericObjectPool<MyClass>(createFunc: () => new MyClass(), maxSize: 100, capacity: 32);
// Using fluent builder for complex configuration
var pool = ScyllaGenericObjectPool<MyClass>.CreateBuilder()
.WithCapacity(64)
.WithMaxSize(200)
.WithCreateFunc(() => new MyClass())
.OnGet(obj => obj.Reset())
.OnRelease(obj => obj.Cleanup())
.CollectionCheck()
.Synchronized()
.Build();
Constructors
ScyllaGenericObjectPool(Func<T>, Action<T>, Action<T>, Action<T>, bool, bool, bool, int, int)
Creates a new object pool backed by a free-list of available instances.
Declaration
public ScyllaGenericObjectPool(Func<T> createFunc = null, Action<T> onGet = null, Action<T> onRelease = null, Action<T> onDestroy = null, bool collectionCheck = false, bool allowCreate = true, bool disposeOnDestroy = false, int maxSize = 2147483647, int capacity = 4)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | createFunc | Optional creation function used when the pool is empty and |
| Action<T> | onGet | Optional callback invoked after an instance is retrieved from the pool |
| Action<T> | onRelease | Optional callback invoked before an instance is stored back into the pool |
| Action<T> | onDestroy | Optional callback invoked before an instance is destroyed/forgotten |
| bool | collectionCheck | If true, Release(T) checks whether the instance is already in the pool (O(n)). Useful for debugging; disable for maximum performance. |
| bool | allowCreate | If true, Get() creates a new instance when the pool is empty. If false, TryGet(out T) returns false when empty. |
| bool | disposeOnDestroy | If true, when an instance is destroyed/forgotten, and it implements IDisposable, Dispose() will be called. |
| int | maxSize | Maximum number of available instances to keep in the pool. When exceeded, released instances are destroyed/forgotten (and not stored). |
| int | capacity | Initial free-list capacity (rounded up to at least 1) |
Properties
AllowCreate
True if the pool creates new instances when empty.
Declaration
public bool AllowCreate { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Capabilities
Feature/capability flags supported by this pool instance.
Declaration
public ScyllaCollectionCapabilities Capabilities { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionCapabilities |
Capacity
Current free-list capacity (number of available slots that can be stored without resizing).
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int |
Count
Number of available instances currently stored in the pool. This maps to Count.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int |
CountAll
Total number of instances created by this pool and not subsequently destroyed via Clear(bool).
Declaration
public int CountAll { get; }
Property Value
| Type | Description |
|---|---|
| int |
DisposeOnDestroy
True if instances implementing IDisposable are disposed when destroyed/forgotten.
Declaration
public bool DisposeOnDestroy { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsEmpty
True if the pool has no available instances.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
MaxSize
Maximum number of available instances that can be stored before releases begin destroying/forgetting instances.
Declaration
public int MaxSize { get; }
Property Value
| Type | Description |
|---|---|
| int |
SyncRoot
Synchronization root for externally coordinating operations. This pool is unsynchronized by default.
Declaration
public object SyncRoot { get; }
Property Value
| Type | Description |
|---|---|
| object |
ThreadSafety
Thread-safety guarantees provided by this pool instance.
Declaration
public ScyllaCollectionThreadSafety ThreadSafety { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionThreadSafety |
Methods
AsSynchronized(object)
Creates and returns a thread-safe synchronized wrapper around this pool.
Declaration
public ScyllaGenericObjectPool<T>.SynchronizedScyllaGenericObjectPool AsSynchronized(object syncRoot = null)
Parameters
| Type | Name | Description |
|---|---|---|
| object | syncRoot | Optional external lock object to use for synchronization. If null, the wrapper creates and owns a private lock. |
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.SynchronizedScyllaGenericObjectPool | A thread-safe wrapper that synchronizes all operations |
Clear()
Removes all available instances from the pool and destroys/disposes them.
Declaration
public void Clear()
Clear(bool)
Removes all available instances from the pool. If destroy is true,
also destroys/disposes them based on configuration.
Declaration
public void Clear(bool destroy)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | destroy | If true, destroys/disposes available instances; if false, just forgets them |
Contains(T, IEqualityComparer<T>)
Determines whether the pool currently contains the specified available instance (O(n)).
Declaration
public bool Contains(T instance, IEqualityComparer<T> comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | Instance to locate |
| IEqualityComparer<T> | comparer | Optional comparer. For reference types, if null, reference identity is used. For value types, if null, Default is used. |
Returns
| Type | Description |
|---|---|
| bool | True if found among available instances, false otherwise |
CopyTo(Span<T>)
Copies available instances from the pool into the provided span (top-to-bottom / LIFO order).
Declaration
public int CopyTo(Span<T> destination)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<T> | destination | The span to copy instances into |
Returns
| Type | Description |
|---|---|
| int | The number of instances copied |
CopyTo(T[], int)
Copies available instances from the pool into the provided array starting at the specified index (top-to-bottom / LIFO order).
Declaration
public int CopyTo(T[] destination, int destinationIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | destination | The array to copy instances into |
| int | destinationIndex | The zero-based index in the destination array at which copying begins |
Returns
| Type | Description |
|---|---|
| int | The number of instances copied |
CreateBuilder()
Creates a new fluent builder for configuring and constructing a ScyllaGenericObjectPool instance. The builder provides a discoverable API for setting capacity, max size, creation function, callbacks, and synchronized wrappers.
Declaration
public static ScyllaGenericObjectPool<T>.Builder CreateBuilder()
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Builder | A new builder instance for configuring ScyllaGenericObjectPool construction |
Remarks
var pool = ScyllaGenericObjectPool<MyClass>.CreateBuilder()
.WithCapacity(64)
.WithMaxSize(200)
.WithCreateFunc(() => new MyClass())
.Build();
Get()
Retrieves an instance from the pool. If the pool is empty and AllowCreate is true, a new instance is created.
Declaration
public T Get()
Returns
| Type | Description |
|---|---|
| T | An instance |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the pool is empty and AllowCreate is false |
GetEnumerator()
Returns an enumerator that iterates through available instances in the pool (top-to-bottom / LIFO order). The enumerator is a value type, enabling allocation-free enumeration when using the concrete type.
Declaration
public ScyllaGenericObjectPool<T>.Enumerator GetEnumerator()
Returns
| Type | Description |
|---|---|
| ScyllaGenericObjectPool<T>.Enumerator | A value-type ScyllaGenericObjectPool<T>.Enumerator positioned before the first available instance. The count is snapshotted at construction time; pool mutations during enumeration are not reflected. |
Prewarm(int)
Pre-creates and stores up to the specified number of available instances.
Declaration
public int Prewarm(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| int | count | Number of instances to create and store |
Returns
| Type | Description |
|---|---|
| int | The number of instances actually created and stored |
Release(T)
Returns an instance back to the pool. If the pool is at MaxSize, the instance is destroyed/forgotten instead of being stored.
Declaration
public bool Release(T instance)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | The instance to return to the pool |
Returns
| Type | Description |
|---|---|
| bool | True if stored in the pool; false if destroyed/forgotten due to capacity constraints |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when |
TryAdd(T)
Attempts to add an instance to the pool (equivalent to Release(T)).
Declaration
public bool TryAdd(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The instance to add |
Returns
| Type | Description |
|---|---|
| bool | True if stored; false if destroyed/forgotten due to capacity constraints |
TryGet(out T)
Attempts to retrieve an instance from the pool. If the pool is empty and AllowCreate is true, a new instance is created.
Declaration
public bool TryGet(out T instance)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | When this method returns, contains the retrieved instance if successful, or default if not |
Returns
| Type | Description |
|---|---|
| bool | True if an instance was retrieved or created; false if the pool is empty and AllowCreate is false |
TryPeek(out T)
Attempts to peek at the next available instance without removing it.
Declaration
public bool TryPeek(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the next available instance if available, or default if empty |
Returns
| Type | Description |
|---|---|
| bool | True if an available instance was present; false otherwise |
TryRemove(out T)
Attempts to remove an instance from the pool (equivalent to TryGet(out T)).
Declaration
public bool TryRemove(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the retrieved instance if successful, or default if not |
Returns
| Type | Description |
|---|---|
| bool | True if an instance was retrieved or created; false if the pool is empty and AllowCreate is false |