< Summary

Class:MainScripts.DCL.Helpers.Utils.PoolUtils
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/PoolUtils.cs
Covered lines:7
Uncovered lines:9
Coverable lines:16
Total lines:143
Line coverage:43.7% (7 of 16)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:7
Method coverage:57.1% (4 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Prewarm[T](...)0%330100%
Prewarm[T](...)0%6200%
CreatePool[T](...)0%6200%
RentList[T]()0%110100%
RentList[T](...)0%2100%
RentListOfDisposables[T]()0%110100%
RentDictionary[TKey, TValue]()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/PoolUtils.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Buffers;
 4using System.Collections.Generic;
 5using System.Threading;
 6using UnityEngine.Pool;
 7
 8namespace MainScripts.DCL.Helpers.Utils
 9{
 10    public static class PoolUtils
 11    {
 12        public struct ListPoolRent<T> : IDisposable
 13        {
 14            private List<T> list;
 15
 16            public List<T> GetList() =>
 17                list;
 18
 19            internal ListPoolRent(List<T> list)
 20            {
 21                this.list = list;
 22            }
 23
 24            public void Dispose()
 25            {
 26                ListPool<T>.Release(list);
 27                list = null;
 28            }
 29        }
 30
 31        /// <summary>
 32        /// Calls `IDispose` on elements before returning the list to the pool
 33        /// </summary>
 34        public struct ListPoolOfDisposablesRent<T> : IDisposable where T : IDisposable
 35        {
 36            private List<T> list;
 37
 38            public List<T> GetList() =>
 39                list;
 40
 41            internal ListPoolOfDisposablesRent(List<T> list)
 42            {
 43                this.list = list;
 44            }
 45
 46            public void Dispose()
 47            {
 48                foreach (T element in list)
 49                    element.Dispose();
 50
 51                ListPool<T>.Release(list);
 52                list = null;
 53            }
 54        }
 55
 56        public struct DictionaryPoolRent<TKey, TValue> : IDisposable
 57        {
 58            private Dictionary<TKey, TValue> dictionary;
 59
 60            public Dictionary<TKey, TValue> GetDictionary() =>
 61                dictionary;
 62
 63            internal DictionaryPoolRent(Dictionary<TKey, TValue> dictionary)
 64            {
 65                this.dictionary = dictionary;
 66            }
 67
 68            public void Dispose()
 69            {
 70                DictionaryPool<TKey, TValue>.Release(dictionary);
 71                dictionary = null;
 72            }
 73        }
 74
 75        /// <summary>
 76        /// This method uses the Create function provided to the pool on construction
 77        /// to generate instances. Therefore some allocation cannot be avoided.
 78        /// </summary>
 79        /// <param name="pool"></param>
 80        /// <param name="count"></param>
 81        /// <typeparam name="T"></typeparam>
 82        public static void Prewarm<T>(this IObjectPool<T> pool, int count) where T: class
 83        {
 28984            T[] instances = new T[count];
 85
 1814986            for (int i = 0; i < count; i++) { instances[i] = pool.Get(); }
 87
 1814988            for (int i = 0; i < count; i++) { pool.Release(instances[i]); }
 28989        }
 90
 91        /// <summary>
 92        /// By providing the Create function, this method can Prewarm a pool without extra allocation.
 93        /// Watch out for inconsistencies between the pool's create method and the one provided
 94        /// </summary>
 95        public static void Prewarm<T>(this IObjectPool<T> pool, Func<T> builder, int count) where T: class
 96        {
 097            for (int i = 0; i < count; i++)
 098                pool.Release(builder());
 099        }
 100
 101        /// <summary>
 102        /// <inheritdoc cref="Prewarm{T}(UnityEngine.Pool.ObjectPool{T},int)"/>
 103        /// </summary>
 104        public static async UniTask PrewarmAsync<T>(this IObjectPool<T> pool, Func<T> builder, int count, int perFrame, 
 105        {
 106            while (count > 0)
 107            {
 108                for (var i = 0; i < perFrame && count > 0; i++)
 109                {
 110                    pool.Release(builder());
 111                    count--;
 112                }
 113
 114                await UniTask.NextFrame(ct);
 115            }
 116        }
 117
 118        public static ObjectPool<T> CreatePool<T>(int prewarmCount, Func<T> createFunc) where T: class
 119        {
 0120            var pool = new ObjectPool<T>(createFunc, defaultCapacity: prewarmCount);
 121
 0122            for (var i = 0; i < prewarmCount; i++) { pool.Release(createFunc()); }
 123
 0124            return pool;
 125        }
 126
 127        public static ListPoolRent<T> RentList<T>() =>
 301128            new ListPoolRent<T>(ListPool<T>.Get());
 129
 130        public static ListPoolRent<T> RentList<T>(this IEnumerable<T> copyFrom)
 131        {
 0132            var list = ListPool<T>.Get();
 0133            list.AddRange(copyFrom);
 0134            return new ListPoolRent<T>(list);
 135        }
 136
 137        public static ListPoolOfDisposablesRent<T> RentListOfDisposables<T>() where T : IDisposable =>
 14138            new ListPoolOfDisposablesRent<T>(ListPool<T>.Get());
 139
 140        public static DictionaryPoolRent<TKey, TValue> RentDictionary<TKey, TValue>() =>
 101141            new DictionaryPoolRent<TKey, TValue>(DictionaryPool<TKey, TValue>.Get());
 142    }
 143}