Skip to content

Commit

Permalink
fix: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Jun 8, 2021
1 parent 4f9bbf5 commit e352d15
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public static void Clear(this CombineInstance[] self)

internal static class MeshPool
{
private static readonly Stack<Mesh> s_Pool = new Stack<Mesh>();
private static readonly Stack<Mesh> s_Pool = new Stack<Mesh>(32);
private static readonly HashSet<int> s_HashPool = new HashSet<int>();

public static void Init()
{
Expand All @@ -117,6 +118,7 @@ static MeshPool()
var m = new Mesh();
m.MarkDynamic();
s_Pool.Push(m);
s_HashPool.Add(m.GetInstanceID());
}
}

Expand All @@ -126,7 +128,11 @@ public static Mesh Rent()
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();
Expand All @@ -136,9 +142,14 @@ public static Mesh Rent()

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);
}
}

Expand Down

0 comments on commit e352d15

Please sign in to comment.