Class ArrayPool<T>
A pool of allowing the re-usage of previously allocated and discarded arrays. Helps reducing garbage collection.
public class ArrayPool<T>
Type Parameters
T- A pool of allowing the re-usage of previously allocated and discarded arrays. Helps reducing garbage collection.
- Inheritance
-
ArrayPool<T>
Remarks
Is thread safe
Constructors
ArrayPool(long)
Creates a new pool
public ArrayPool(long elementsCapacity)
Parameters
elementsCapacitylong
Properties
ElementsCapacity
The maximal number of elements that the pool will keep, after they have been freed, to be available for future usage. Once this limit is reached, every freed array will simply get ignored, allowing the garbage collector to collect it
public long ElementsCapacity { get; set; }
Property Value
Remarks
This is not the maximal number of arrays, but the maximal sum of the arrays' lengths
LogAllocations
Log in the console each time a new array is allocated in memory
public bool LogAllocations { get; set; }
Property Value
UsageData
Returns data about the pool's usage.
public ArrayPoolUsageData UsageData { get; }
Property Value
Methods
Allocate(int, bool)
Allocates a new array if none available, or reuses an existing one otherwise
public SubArray<T> Allocate(int minimalSize, bool clearArray = true)
Parameters
minimalSizeintThe array's guaranteed minimal size
clearArrayboolWhether the returned array's elements will be guaranteed to be set to their default value
Returns
- SubArray<T>
AllocateExactSize(int, bool)
Allocates a new array if none available, or reuses an existing one otherwise
public SubArray<T> AllocateExactSize(int exactSize, bool clearArray = true)
Parameters
exactSizeintThe array's exact size
clearArrayboolWhether the returned array's elements will be guaranteed to be set to their default value
Returns
- SubArray<T>
Clone(SubArray<T>)
Return a new SubArray<T> instance that will use a copy of the given input array
public SubArray<T> Clone(SubArray<T> source)
Parameters
sourceSubArray<T>
Returns
- SubArray<T>
Clone(T[])
Return a new SubArray<T> instance that will use a copy of the given input array
public SubArray<T> Clone(T[] source)
Parameters
sourceT[]
Returns
- SubArray<T>
Free(SubArray<T>)
Returns an array to the pool, ready to be reused
public void Free(SubArray<T> subArray)
Parameters
subArraySubArray<T>
Free(T[])
Returns an array to the pool, ready to be reused
public void Free(T[] array)
Parameters
arrayT[]
Resize(ref SubArray<T>, int, bool)
Resizes the given array
public void Resize(ref SubArray<T> subArray, int newMinimalSize, bool clearNewSpace = true)
Parameters
subArraySubArray<T>The array to resize
newMinimalSizeintThe new size
clearNewSpaceboolWhen resizing an array to make it bigger, should the newly available space be cleared or not.
ResizeAndClear(ref SubArray<T>, int)
Resize an array to a new size and clears it. Similar to calling Free(SubArray<T>) then calling Allocate(int, bool), but done in a more optimized way
public void ResizeAndClear(ref SubArray<T> subArray, int newMinimalSize)
Parameters
ResizeCopyless(ref SubArray<T>, int)
Resizes the array, without preserving the content of the array. You basically end up with an array with no guarantee on its content. This is faster than Resize(ref SubArray<T>, int, bool) because it doesn't need to copy the content of the array.
public void ResizeCopyless(ref SubArray<T> subArray, int newMinimalSize)