Table of Contents

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

elementsCapacity long

ElementsCapacity

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

long

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

bool

UsageData

Returns data about the pool's usage.

public ArrayPoolUsageData UsageData { get; }

Property Value

ArrayPoolUsageData

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

minimalSize int

The array's guaranteed minimal size

clearArray bool

Whether 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

exactSize int

The array's exact size

clearArray bool

Whether 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

source SubArray<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

source T[]

Returns

SubArray<T>

Free(SubArray<T>)

Returns an array to the pool, ready to be reused

public void Free(SubArray<T> subArray)

Parameters

subArray SubArray<T>

Free(T[])

Returns an array to the pool, ready to be reused

public void Free(T[] array)

Parameters

array T[]

Resize(ref SubArray<T>, int, bool)

Resizes the given array

public void Resize(ref SubArray<T> subArray, int newMinimalSize, bool clearNewSpace = true)

Parameters

subArray SubArray<T>

The array to resize

newMinimalSize int

The new size

clearNewSpace bool

When 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

subArray SubArray<T>
newMinimalSize int

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)

Parameters

subArray SubArray<T>
newMinimalSize int

See Also