From e352d153cef8c1efb2792e35010d7eed1e31a040 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Wed, 9 Jun 2021 01:31:39 +0900 Subject: [PATCH] fix: improve performance --- Scripts/Utils.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Scripts/Utils.cs b/Scripts/Utils.cs index d47a2d1..fda7084 100644 --- a/Scripts/Utils.cs +++ b/Scripts/Utils.cs @@ -104,7 +104,8 @@ namespace Coffee.UIParticleExtensions internal static class MeshPool { - private static readonly Stack s_Pool = new Stack(); + private static readonly Stack s_Pool = new Stack(32); + private static readonly HashSet s_HashPool = new HashSet(); public static void Init() { @@ -117,6 +118,7 @@ namespace Coffee.UIParticleExtensions var m = new Mesh(); m.MarkDynamic(); s_Pool.Push(m); + s_HashPool.Add(m.GetInstanceID()); } } @@ -126,7 +128,11 @@ namespace Coffee.UIParticleExtensions while (0 < s_Pool.Count) { m = s_Pool.Pop(); - if (m) return m; + if (m) + { + s_HashPool.Remove(m.GetInstanceID()); + return m; + } } m = new Mesh(); @@ -136,9 +142,14 @@ namespace Coffee.UIParticleExtensions public static void Return(Mesh mesh) { - if (!mesh || s_Pool.Contains(mesh)) return; + if (!mesh) return; + + var id = mesh.GetInstanceID(); + if (s_HashPool.Contains(id)) return; + mesh.Clear(false); s_Pool.Push(mesh); + s_HashPool.Add(id); } }