A* Pathfinding Project  4.1.19
The A* Pathfinding Project for Unity 3D
ArrayPool< T > Class Template Reference

Lightweight Array Pool. More...

Detailed Description

Lightweight Array Pool.

Handy class for pooling arrays of type T.


  • Claim a new array using
    SomeClass[] foo = ArrayPool<SomeClass>.Claim (capacity);
  • Use it and do stuff with it
  • Release it with
    ArrayPool<SomeClass>.Release (foo);
Arrays returned from the Claim method may contain arbitrary data. You cannot rely on it being zeroed out.

After you have released a array, you should never use it again, if you do use it your code may modify it at the same time as some other code is using it which will likely lead to bad results.

Version 3.8.6
See also

Static Public Member Functions

static T [] Claim (int minimumLength)
 Returns an array with at least the specified length. More...
static T [] ClaimWithExactLength (int length)
 Returns an array with the specified length. More...
static void Release (ref T[] array, bool allowNonPowerOfTwo=false)
 Pool an array. More...

Private Attributes

const int MaximumExactArrayLength = 256
 Maximum length of an array pooled using ClaimWithExactLength. More...

Static Private Attributes

static readonly Stack< T[]> [] exactPool = new Stack<T[]>[MaximumExactArrayLength+1]
static readonly HashSet< T[]> inPool = new HashSet<T[]>()
static readonly Stack< T[]> [] pool = new Stack<T[]>[31]
 Internal pool. More...

Member Function Documentation

◆ Claim()

static T [] Claim ( int  minimumLength)

Returns an array with at least the specified length.

◆ ClaimWithExactLength()

static T [] ClaimWithExactLength ( int  length)

Returns an array with the specified length.

Use with caution as pooling too many arrays with different lengths that are rarely being reused will lead to an effective memory leak.

Use Claim if you just need an array that is at least as large as some value.

◆ Release()

static void Release ( ref T []  array,
bool  allowNonPowerOfTwo = false 

Pool an array.

If the array was got using the ClaimWithExactLength method then the allowNonPowerOfTwo parameter must be set to true. The parameter exists to make sure that non power of two arrays are not pooled unintentionally which could lead to memory leaks.

Member Data Documentation

◆ exactPool

readonly Stack<T[]> [] exactPool = new Stack<T[]>[MaximumExactArrayLength+1]

◆ inPool

readonly HashSet<T[]> inPool = new HashSet<T[]>()

◆ MaximumExactArrayLength

const int MaximumExactArrayLength = 256

Maximum length of an array pooled using ClaimWithExactLength.

Arrays with lengths longer than this will silently not be pooled.

◆ pool

readonly Stack<T[]> [] pool = new Stack<T[]>[31]

Internal pool.

The arrays in each bucket have lengths of 2^i

The documentation for this class was generated from the following file: