Class ListPool<T>
A basic implementation of IListPool<T> which uses a simple List of lists to store the pool.
Implements
Namespace: SadRogue.Primitives.Pooling
Assembly: TheSadRogue.Primitives.dll
Syntax
public sealed class ListPool<T> : Object, IListPool<T>
Type Parameters
Name | Description |
---|---|
T | Type of items being stored in the list. |
Constructors
ListPool(Int32, Int32)
Constructs a new pool with the given parameters.
Declaration
public ListPool(int maxLists, int maxCapacity)
Parameters
Type | Name | Description |
---|---|---|
Int32 | maxLists | Maximum number of lists that are allowed to be in the pool at any given time. Any lists beyond this number which are returned, are allowed to be GCed. |
Int32 | maxCapacity | The maximum Capacity allowed for lists that are returned via Return(List<T>, Boolean). Any lists with capacities over this value are shrunk to this value when they are returned. |
Properties
MaxCapacity
The maximum Capacity allowed for lists that are returned via Return(List<T>, Boolean). Any lists with capacities over this value are shrunk to this value when they are returned.
Declaration
public int MaxCapacity { get; }
Property Value
Type | Description |
---|---|
Int32 |
Remarks
This value, in combination with MaxLists, ensures that a hard limit can be imposed on the memory usage for a pool. However, if this value is small relative to the actual capacity of lists being returned, it will result in lists that are constantly shrunk then sized back up when they are used; and as such it will reduce the effectiveness of the pool.
MaxLists
Maximum number of lists that are allowed to be in the pool at any given time. Any lists beyond this number which are returned, are allowed to be GCed.
Declaration
public int MaxLists { get; }
Property Value
Type | Description |
---|---|
Int32 |
Methods
Clear()
Clears the pool of all lists.
Declaration
public void Clear()
Rent()
Retrieve a list from the pool, or allocate a new one if there are no lists available.
Declaration
public List<T> Rent()
Returns
Type | Description |
---|---|
List<T> | A list from the pool, or a new list if no lists are available in the pool. |
Return(List<T>, Boolean)
Returns the given list to the pool. The list will be discarded (and allowed to be queued for GC) if there are already at least MaxLists unused lists in the pool.
Declaration
public void Return(List<T> list, bool clear = true)
Parameters
Type | Name | Description |
---|---|---|
List<T> | list | The list to return. |
Boolean | clear | Whether or not to clear the list given before adding it to the pool. Should be set to true unless you are absolutely sure the list is cleared via other means before passing it. |