| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using GDX.Mathematics; |
| | 6 | |
|
| | 7 | | namespace GDX.Collections.Pooling |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// An object pool for arrays with power-of-two lengths. |
| | 11 | | /// </summary> |
| | 12 | | /// <typeparam name="T">The data type contained by pooled arrays.</typeparam> |
| | 13 | | public struct ArrayPool<T> |
| | 14 | | { |
| | 15 | | public JaggedArrayWithCount<T>[] ArrayPools; |
| | 16 | | public int[] MaxPoolCapacities; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initialize the array pool with initial and maximum sizes for each power-of-two, 0 through 30 inclusive ( |
| | 20 | | /// power-of-two length supported in C#). |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="initialPoolCounts"></param> |
| | 23 | | /// <param name="maxPoolCapacities"></param> |
| | 24 | | public ArrayPool(int[] initialPoolCounts, int[] maxPoolCapacities) |
| 18 | 25 | | { |
| 18 | 26 | | ArrayPools = new JaggedArrayWithCount<T>[31]; |
| 18 | 27 | | MaxPoolCapacities = maxPoolCapacities; |
| | 28 | |
|
| 1152 | 29 | | for (int i = 0; i < 31; i++) |
| 558 | 30 | | { |
| 558 | 31 | | int initialArrayCount = initialPoolCounts[i]; |
| 558 | 32 | | int maxArraysForSize = maxPoolCapacities[i]; |
| 558 | 33 | | T[][] arrayPoolForSize = new T[maxArraysForSize][]; |
| | 34 | |
|
| 1150 | 35 | | for (int j = 0; j < initialArrayCount; j++) |
| 17 | 36 | | { |
| 17 | 37 | | arrayPoolForSize[j] = new T[1 << i]; |
| 17 | 38 | | } |
| | 39 | |
|
| 558 | 40 | | JaggedArrayWithCount<T> pool = |
| | 41 | | new JaggedArrayWithCount<T> { Pool = arrayPoolForSize, Count = initialArrayCount }; |
| | 42 | |
|
| 558 | 43 | | ArrayPools[i] = pool; |
| 558 | 44 | | } |
| 18 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Allocates an array from the pool. Finds an array of the smallest power-of-two length larger than or equa |
| | 49 | | /// requested size. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="requestedSize"> |
| | 52 | | /// The desired array length. The returned array will be the smallest power-of-two larger than |
| | 53 | | /// or equal to this size. |
| | 54 | | /// </param> |
| | 55 | | /// <returns></returns> |
| | 56 | | public T[] Get(int requestedSize) |
| 15 | 57 | | { |
| 15 | 58 | | requestedSize = requestedSize < 1 ? 1 : requestedSize; |
| 15 | 59 | | requestedSize--; |
| 15 | 60 | | requestedSize |= requestedSize >> 1; |
| 15 | 61 | | requestedSize |= requestedSize >> 2; |
| 15 | 62 | | requestedSize |= requestedSize >> 4; |
| 15 | 63 | | requestedSize |= requestedSize >> 8; |
| 15 | 64 | | requestedSize |= requestedSize >> 16; |
| 15 | 65 | | int nextPowerOfTwo = requestedSize + 1; |
| | 66 | |
|
| | 67 | | LongDoubleConversionUnion u; |
| 15 | 68 | | u.DoubleValue = 0.0; |
| 15 | 69 | | u.LongValue = 0x4330000000000000L + nextPowerOfTwo; |
| 15 | 70 | | u.DoubleValue -= 4503599627370496.0; |
| 15 | 71 | | int index = (int)(u.LongValue >> 52) - 0x3FF; |
| | 72 | |
|
| 15 | 73 | | JaggedArrayWithCount<T> arrayPool = ArrayPools[index]; |
| | 74 | |
|
| | 75 | | T[] array; |
| 15 | 76 | | if (arrayPool.Count < 1) |
| 1 | 77 | | { |
| 1 | 78 | | array = new T[nextPowerOfTwo]; |
| 1 | 79 | | } |
| | 80 | | else |
| 14 | 81 | | { |
| 14 | 82 | | array = arrayPool.Pool[arrayPool.Count - 1]; |
| 14 | 83 | | arrayPool.Pool[arrayPool.Count - 1] = null; |
| 14 | 84 | | arrayPool.Count--; |
| 14 | 85 | | } |
| | 86 | |
|
| 15 | 87 | | ArrayPools[index] = arrayPool; |
| | 88 | |
|
| 15 | 89 | | return array; |
| 15 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Return a power-of-two sized array to the pool. Only pass power-of-two sized arrays to this function. Doe |
| | 94 | | /// the array. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="array">The power-of-two sized array to return to the pool. Power-of-two sizes only.</param> |
| | 97 | | public void Return(T[] array) |
| 15 | 98 | | { |
| 15 | 99 | | uint length = |
| | 100 | | unchecked((uint)array.Length); // Counting on people to be cool and not pass in a non-power-of-two here. |
| | 101 | |
|
| | 102 | | LongDoubleConversionUnion u; |
| 15 | 103 | | u.DoubleValue = 0.0; |
| 15 | 104 | | u.LongValue = 0x4330000000000000L + length; |
| 15 | 105 | | u.DoubleValue -= 4503599627370496.0; |
| 15 | 106 | | int index = (int)(u.LongValue >> 52) - 0x3FF; |
| | 107 | |
|
| 15 | 108 | | JaggedArrayWithCount<T> arrayPool = ArrayPools[index]; |
| 15 | 109 | | int maxPoolCapacity = MaxPoolCapacities[index]; |
| | 110 | |
|
| 15 | 111 | | if (arrayPool.Count < maxPoolCapacity) |
| 14 | 112 | | { |
| 14 | 113 | | arrayPool.Pool[arrayPool.Count] = array; |
| 14 | 114 | | arrayPool.Count++; |
| 14 | 115 | | } |
| | 116 | |
|
| 15 | 117 | | ArrayPools[index] = arrayPool; |
| 15 | 118 | | } |
| | 119 | | } |
| | 120 | | } |