| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Buffers; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine.Pool; |
| | 7 | |
|
| | 8 | | namespace 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() => |
| 14 | 39 | | list; |
| | 40 | |
|
| | 41 | | internal ListPoolOfDisposablesRent(List<T> list) |
| | 42 | | { |
| 14 | 43 | | this.list = list; |
| 14 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 84 | 48 | | foreach (T element in list) |
| 28 | 49 | | element.Dispose(); |
| | 50 | |
|
| 14 | 51 | | ListPool<T>.Release(list); |
| 14 | 52 | | list = null; |
| 14 | 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 | | } |