|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Oxide.Pooling |
| 5 | +{ |
| 6 | + public sealed class ArrayPool<T> : PoolFactory, IArrayPool<T> |
| 7 | + { |
| 8 | + #region Settings / Defaults |
| 9 | + |
| 10 | + public const int DEFAULT_ARRAY_MAX_LENGTH = 512; |
| 11 | + |
| 12 | + public const int DEFAULT_ARRAY_MAX_PER_POOL = 50; |
| 13 | + |
| 14 | + public const bool DEFAULT_ARRAY_CLEAN_ON_RETURN = true; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// The amount of arrays this pool can store per array length |
| 18 | + /// </summary> |
| 19 | + public int ArraysPerLength { get; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Max length of the array this pool can store |
| 23 | + /// </summary> |
| 24 | + public int ArrayMaxLength { get; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Empties the array on return |
| 28 | + /// </summary> |
| 29 | + public bool CleanOnReturn { get; } |
| 30 | + |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region Properties |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The shared pool instance |
| 37 | + /// </summary> |
| 38 | + public static IArrayPool<T> Shared { get; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Creates a new pool instance with default settings |
| 42 | + /// </summary> |
| 43 | + public static IArrayPool<T> Default => new ArrayPool<T>(DEFAULT_ARRAY_MAX_PER_POOL, DEFAULT_ARRAY_MAX_LENGTH, DEFAULT_ARRAY_CLEAN_ON_RETURN); |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Returns a pool instance that doesn't pool items |
| 47 | + /// </summary> |
| 48 | + public static IArrayPool<T> NoPool => NullArrayPool<T>.Instance; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Creates a new pool instance with custom settings |
| 52 | + /// </summary> |
| 53 | + /// <param name="arrayMaxLength">Max length of the array this pool can store</param> |
| 54 | + /// <param name="arrayMaxPerPool">The amount of arrays this pool can store per array length</param> |
| 55 | + /// <param name="arrayCleanOnReturn">Empties the array on return</param> |
| 56 | + /// <returns>The custom array pool</returns> |
| 57 | + public static IArrayPool<T> Custom(int arrayMaxLength = DEFAULT_ARRAY_MAX_LENGTH, int arrayMaxPerPool = DEFAULT_ARRAY_MAX_PER_POOL, bool arrayCleanOnReturn = DEFAULT_ARRAY_CLEAN_ON_RETURN) |
| 58 | + { |
| 59 | + return new ArrayPool<T>(arrayMaxPerPool, arrayMaxLength, arrayCleanOnReturn); |
| 60 | + } |
| 61 | + |
| 62 | + public static T[] Empty { get; } |
| 63 | + |
| 64 | + private Stack<T[]>[] Pools { get; } |
| 65 | + |
| 66 | + #endregion |
| 67 | + |
| 68 | + static ArrayPool() |
| 69 | + { |
| 70 | + Empty = new T[0]; |
| 71 | + Shared = Default; |
| 72 | + } |
| 73 | + |
| 74 | + private ArrayPool(int arraysPerLength = 50, int maxArrayLength = 512, bool cleanOnReturn = true) |
| 75 | + { |
| 76 | + ArraysPerLength = arraysPerLength <= 0 ? throw new ArgumentOutOfRangeException(nameof(arraysPerLength)) : arraysPerLength; |
| 77 | + ArrayMaxLength = maxArrayLength < 1 ? 1 : maxArrayLength; |
| 78 | + CleanOnReturn = cleanOnReturn; |
| 79 | + |
| 80 | + Pools = new Stack<T[]>[ArrayMaxLength]; |
| 81 | + for (int i = 0; i < Pools.Length; i++) |
| 82 | + { |
| 83 | + Pools[i] = new Stack<T[]>(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public T[] Take() => Empty; |
| 88 | + |
| 89 | + public T[] Take(int length) |
| 90 | + { |
| 91 | + if (length <= 0) |
| 92 | + { |
| 93 | + return Empty; |
| 94 | + } |
| 95 | + |
| 96 | + if (length > ArrayMaxLength) |
| 97 | + { |
| 98 | + return new T[length]; |
| 99 | + } |
| 100 | + |
| 101 | + Stack<T[]> store = Pools[length - 1]; |
| 102 | + |
| 103 | + lock (store) |
| 104 | + { |
| 105 | + if (store.Count > 0) |
| 106 | + { |
| 107 | + return store.Pop(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return new T[length]; |
| 112 | + } |
| 113 | + |
| 114 | + public void Return(T[] item) |
| 115 | + { |
| 116 | + if (item == null || item.Length == 0 || item.Length > ArrayMaxLength) |
| 117 | + { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + if (CleanOnReturn) |
| 122 | + { |
| 123 | + Array.Clear(item, 0, item.Length); |
| 124 | + } |
| 125 | + |
| 126 | + Stack<T[]> store = Pools[item.Length - 1]; |
| 127 | + |
| 128 | + lock (store) |
| 129 | + { |
| 130 | + if (store.Count < ArraysPerLength) |
| 131 | + { |
| 132 | + store.Push(item); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + protected override void OnPurge() |
| 138 | + { |
| 139 | + for (int i = 0; i < Pools.Length; i++) |
| 140 | + { |
| 141 | + Stack<T[]> store = Pools[i]; |
| 142 | + lock (store) |
| 143 | + { |
| 144 | + while (store.Count > 0) |
| 145 | + { |
| 146 | + T[] array = store.Pop(); |
| 147 | + Array.Clear(array, 0, array.Length); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments