Last active
May 19, 2019 11:12
-
-
Save AnsisMalins/f16bff2552881cc3af1dc05d3610e226 to your computer and use it in GitHub Desktop.
Array Pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ArrayPool<T> | |
{ | |
private static readonly List<T[]> pool = new List<T[]>(); | |
public static Item Get(int length) | |
{ | |
for (int i = pool.Count - 1; i >= 0; i--) | |
{ | |
var array = pool[i]; | |
if (array.Length >= length) | |
{ | |
pool.RemoveAtSwap(i); | |
return new Item(array, length); | |
} | |
} | |
return new Item(new T[length], length); | |
} | |
public struct Item : IDisposable | |
{ | |
public T[] Array { get; private set; } | |
public int Length { get; private set; } | |
internal Item(T[] array, int length) | |
{ | |
Array = array; | |
Length = length; | |
} | |
public ref T this[int index] | |
{ | |
get { return ref Array[index]; } | |
} | |
public static implicit operator T[] (Item item) | |
{ | |
return item.Array; | |
} | |
void IDisposable.Dispose() | |
{ | |
if (Array != null) | |
{ | |
// Only clear the portion of the array we used. | |
System.Array.Clear(Array, 0, Length); | |
pool.Add(Array); | |
Array = null; | |
} | |
} | |
} | |
} | |
public static class Extensions | |
{ | |
/// <summary>Removes an item quickly by using the swap trick. The order of items in the List is not | |
/// preserved.</summary> | |
public static void RemoveAtSwap<T>(this List<T> list, int index) | |
{ | |
var indexOfLastItem = list.Count - 1; | |
if (index < indexOfLastItem) | |
list[index] = list[indexOfLastItem]; | |
list.RemoveAt(indexOfLastItem); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: