< Summary

Class:GDX.Collections.Pooling.ArrayPool[T]
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ArrayPool.cs
Covered lines:57
Uncovered lines:0
Coverable lines:57
Total lines:120
Line coverage:100% (57 of 57)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:3
Method coverage:100% (3 of 3)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ArrayPool(...)0%330100%
Get(...)0%4.024088.46%
Return()0%220100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ArrayPool.cs

#LineLine coverage
 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
 5using GDX.Mathematics;
 6
 7namespace 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)
 1825        {
 1826            ArrayPools = new JaggedArrayWithCount<T>[31];
 1827            MaxPoolCapacities = maxPoolCapacities;
 28
 115229            for (int i = 0; i < 31; i++)
 55830            {
 55831                int initialArrayCount = initialPoolCounts[i];
 55832                int maxArraysForSize = maxPoolCapacities[i];
 55833                T[][] arrayPoolForSize = new T[maxArraysForSize][];
 34
 115035                for (int j = 0; j < initialArrayCount; j++)
 1736                {
 1737                    arrayPoolForSize[j] = new T[1 << i];
 1738                }
 39
 55840                JaggedArrayWithCount<T> pool =
 41                    new JaggedArrayWithCount<T> { Pool = arrayPoolForSize, Count = initialArrayCount };
 42
 55843                ArrayPools[i] = pool;
 55844            }
 1845        }
 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)
 1557        {
 1558            requestedSize = requestedSize < 1 ? 1 : requestedSize;
 1559            requestedSize--;
 1560            requestedSize |= requestedSize >> 1;
 1561            requestedSize |= requestedSize >> 2;
 1562            requestedSize |= requestedSize >> 4;
 1563            requestedSize |= requestedSize >> 8;
 1564            requestedSize |= requestedSize >> 16;
 1565            int nextPowerOfTwo = requestedSize + 1;
 66
 67            LongDoubleConversionUnion u;
 1568            u.DoubleValue = 0.0;
 1569            u.LongValue = 0x4330000000000000L + nextPowerOfTwo;
 1570            u.DoubleValue -= 4503599627370496.0;
 1571            int index = (int)(u.LongValue >> 52) - 0x3FF;
 72
 1573            JaggedArrayWithCount<T> arrayPool = ArrayPools[index];
 74
 75            T[] array;
 1576            if (arrayPool.Count < 1)
 177            {
 178                array = new T[nextPowerOfTwo];
 179            }
 80            else
 1481            {
 1482                array = arrayPool.Pool[arrayPool.Count - 1];
 1483                arrayPool.Pool[arrayPool.Count - 1] = null;
 1484                arrayPool.Count--;
 1485            }
 86
 1587            ArrayPools[index] = arrayPool;
 88
 1589            return array;
 1590        }
 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)
 1598        {
 1599            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;
 15103            u.DoubleValue = 0.0;
 15104            u.LongValue = 0x4330000000000000L + length;
 15105            u.DoubleValue -= 4503599627370496.0;
 15106            int index = (int)(u.LongValue >> 52) - 0x3FF;
 107
 15108            JaggedArrayWithCount<T> arrayPool = ArrayPools[index];
 15109            int maxPoolCapacity = MaxPoolCapacities[index];
 110
 15111            if (arrayPool.Count < maxPoolCapacity)
 14112            {
 14113                arrayPool.Pool[arrayPool.Count] = array;
 14114                arrayPool.Count++;
 14115            }
 116
 15117            ArrayPools[index] = arrayPool;
 15118        }
 119    }
 120}

Coverage by test methods




















Methods/Properties

ArrayPool(System.Int32[], System.Int32[])
Get(System.Int32)
Return()