< Summary

Class:MainScripts.DCL.Helpers.Utils.PoolUtilsListPoolRent[T]
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/PoolUtils.cs
Covered lines:6
Uncovered lines:0
Coverable lines:6
Total lines:143
Line coverage:100% (6 of 6)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:3
Method coverage:100% (3 of 3)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetList()0%110100%
ListPoolRent(...)0%110100%
Dispose()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() =>
 48117                list;
 18
 19            internal ListPoolRent(List<T> list)
 20            {
 30121                this.list = list;
 30122            }
 23
 24            public void Dispose()
 25            {
 29726                ListPool<T>.Release(list);
 29727                list = null;
 29728            }
 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        {
 84            T[] instances = new T[count];
 85
 86            for (int i = 0; i < count; i++) { instances[i] = pool.Get(); }
 87
 88            for (int i = 0; i < count; i++) { pool.Release(instances[i]); }
 89        }
 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        {
 97            for (int i = 0; i < count; i++)
 98                pool.Release(builder());
 99        }
 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        {
 120            var pool = new ObjectPool<T>(createFunc, defaultCapacity: prewarmCount);
 121
 122            for (var i = 0; i < prewarmCount; i++) { pool.Release(createFunc()); }
 123
 124            return pool;
 125        }
 126
 127        public static ListPoolRent<T> RentList<T>() =>
 128            new ListPoolRent<T>(ListPool<T>.Get());
 129
 130        public static ListPoolRent<T> RentList<T>(this IEnumerable<T> copyFrom)
 131        {
 132            var list = ListPool<T>.Get();
 133            list.AddRange(copyFrom);
 134            return new ListPoolRent<T>(list);
 135        }
 136
 137        public static ListPoolOfDisposablesRent<T> RentListOfDisposables<T>() where T : IDisposable =>
 138            new ListPoolOfDisposablesRent<T>(ListPool<T>.Get());
 139
 140        public static DictionaryPoolRent<TKey, TValue> RentDictionary<TKey, TValue>() =>
 141            new DictionaryPoolRent<TKey, TValue>(DictionaryPool<TKey, TValue>.Get());
 142    }
 143}