From d687467467486250526bdd4b106b9cd5a9711ec9 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Molteni Date: Wed, 22 Jan 2025 23:05:36 -0300 Subject: [PATCH] Refactor to single handed class --- .../NoiseNativeArrayProvider.cs | 76 +++++++++++-------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/Explorer/Assets/DCL/Landscape/NoiseGeneration/NoiseNativeArrayProvider.cs b/Explorer/Assets/DCL/Landscape/NoiseGeneration/NoiseNativeArrayProvider.cs index ca71e378b3..4114cf3c38 100644 --- a/Explorer/Assets/DCL/Landscape/NoiseGeneration/NoiseNativeArrayProvider.cs +++ b/Explorer/Assets/DCL/Landscape/NoiseGeneration/NoiseNativeArrayProvider.cs @@ -4,56 +4,66 @@ namespace DCL.Landscape.NoiseGeneration { - public class NoiseNativeArrayProvider : IDisposable + internal class NoiseNativeArray : IDisposable { - private readonly Dictionary>> noiseNativeArrayDictionary = new(); - private readonly List> allNativeArrays = new(); + private int currentIndexUsed; + private readonly List> nativeArrays = new (); + private readonly int size; - public void Reset() + public NoiseNativeArray(int size) { - foreach (var nativeArray in allNativeArrays) - { - int size = (int)Math.Sqrt(nativeArray.Length); - if (!noiseNativeArrayDictionary.ContainsKey(size)) - noiseNativeArrayDictionary[size] = new Queue>(); - noiseNativeArrayDictionary[size].Enqueue(nativeArray); - } + this.size = size; + } - allNativeArrays.Clear(); + public NativeArray GetNativeArray() + { + if (currentIndexUsed >= nativeArrays.Count) + nativeArrays.Add(new NativeArray(size * size, Allocator.Persistent)); + return nativeArrays[currentIndexUsed++]; } - public NativeArray GetNoiseNativeArray(int key) + public void Reset() { - // Check if there's a reusable array in the queue for the key - if (noiseNativeArrayDictionary.TryGetValue(key, out var queue) && queue.Count > 0) - return queue.Dequeue(); - - // If no reusable array, create a new one - var newArray = new NativeArray(key * key, Allocator.Persistent); - allNativeArrays.Add(newArray); - return newArray; + currentIndexUsed = 0; } public void Dispose() { - foreach (var queue in noiseNativeArrayDictionary.Values) + foreach (var nativeArray in nativeArrays) { - while (queue.Count > 0) - { - var nativeArray = queue.Dequeue(); - if (nativeArray.IsCreated) - nativeArray.Dispose(); - } + nativeArray.Dispose(); } + } + } + + public class NoiseNativeArrayProvider : IDisposable + { + private readonly Dictionary noiseNativeArrayDictionary = new(); - foreach (var nativeArray in allNativeArrays) + public void Reset() + { + foreach (var noiseNativeArray in noiseNativeArrayDictionary.Values) { - if (nativeArray.IsCreated) - nativeArray.Dispose(); + noiseNativeArray.Reset(); } + } + + public NativeArray GetNoiseNativeArray(int key) + { + if (noiseNativeArrayDictionary.TryGetValue(key, out var noiseArray)) + return noiseArray.GetNativeArray(); - noiseNativeArrayDictionary.Clear(); - allNativeArrays.Clear(); + var newArray = new NoiseNativeArray(key); + noiseNativeArrayDictionary[key] = newArray; + return newArray.GetNativeArray(); + } + + public void Dispose() + { + foreach (var noiseNativeArray in noiseNativeArrayDictionary.Values) + { + noiseNativeArray.Dispose(); + } } } } \ No newline at end of file