Skip to content

Commit 920fb05

Browse files
authored
Remove destroyed instances from stack. Added initial capacity for collections
1 parent 32cdb14 commit 920fb05

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

Runtime/Pool.cs

+50-26
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ public sealed class Pool
1010
private readonly Quaternion _rotation = default;
1111
private readonly Vector3 _scale = default;
1212

13-
private static readonly Dictionary<int, Pool> _prefabLookup = new Dictionary<int, Pool>();
14-
private static readonly Dictionary<int, Pool> _instanceLookup = new Dictionary<int, Pool>();
13+
private static readonly Dictionary<int, Pool> _prefabLookup = new Dictionary<int, Pool>(100);
14+
private static readonly Dictionary<int, Pool> _instanceLookup = new Dictionary<int, Pool>(10000);
15+
16+
private const int CAPACITY = 100;
1517

1618
public Pool(GameObject prefab)
1719
{
@@ -24,7 +26,7 @@ public Pool(GameObject prefab)
2426
_prefab.gameObject.SetActive(false);
2527
}
2628

27-
_instances = new Stack<Poolable>();
29+
_instances = new Stack<Poolable>(CAPACITY);
2830
_prefabLookup.Add(prefab.GetHashCode(), this);
2931

3032
var transform = prefab.transform;
@@ -48,42 +50,37 @@ public static bool TryGetInstancePool(GameObject instance, out Pool pool) =>
4850
public void Populate(int count)
4951
{
5052
for (int i = 0; i < count; i++)
51-
{
52-
var instance = CreateInstance();
53-
_instances.Push(instance);
54-
}
53+
_instances.Push(CreateInstance());
5554
}
5655

57-
public GameObject Get() =>
58-
GetInstance().gameObject;
59-
60-
public GameObject Get(Transform parent)
56+
public GameObject Get()
6157
{
6258
var instance = GetInstance();
6359

64-
instance.transform.SetParent(parent);
65-
6660
return instance.gameObject;
6761
}
6862

69-
public GameObject Get(Transform parent, bool worldPositionStays)
63+
public GameObject Get(Transform parent)
7064
{
7165
var instance = GetInstance();
7266

73-
instance.transform.SetParent(parent, worldPositionStays);
67+
instance.transform.SetParent(parent);
7468

7569
return instance.gameObject;
7670
}
7771

78-
public GameObject Get(Vector3 position, Quaternion rotation)
72+
public GameObject Get(Transform parent, bool worldPositionStays)
7973
{
8074
var instance = GetInstance();
8175

82-
instance.transform.SetPositionAndRotation(position, rotation);
76+
instance.transform.SetParent(parent, worldPositionStays);
8377

8478
return instance.gameObject;
8579
}
8680

81+
public GameObject Get(Vector3 position, Quaternion rotation) =>
82+
Get(position, rotation, null);
83+
8784
public GameObject Get(Vector3 position, Quaternion rotation, Transform parent)
8885
{
8986
var instance = GetInstance();
@@ -111,24 +108,51 @@ public void Release(GameObject instance)
111108

112109
private Poolable GetInstance()
113110
{
114-
Poolable instance;
111+
int count = _instances.Count;
115112

116-
if (_instances.Count == 0)
113+
if (count != 0)
117114
{
118-
instance = CreateInstance();
119-
}
120-
else
121-
{
122-
instance = _instances.Pop();
115+
var instance = _instances.Pop();
123116

124117
if (instance == null)
118+
{
119+
count--;
120+
121+
while (count != 0)
122+
{
123+
instance = _instances.Pop();
124+
125+
if (instance != null)
126+
{
127+
instance.OnGet();
128+
instance.gameObject.SetActive(true);
129+
130+
return instance;
131+
}
132+
133+
count--;
134+
}
135+
125136
instance = CreateInstance();
137+
instance.gameObject.SetActive(true);
138+
139+
return instance;
140+
}
126141
else
142+
{
127143
instance.OnGet();
144+
instance.gameObject.SetActive(true);
145+
146+
return instance;
147+
}
128148
}
149+
else
150+
{
151+
var instance = CreateInstance();
152+
instance.gameObject.SetActive(true);
129153

130-
instance.gameObject.SetActive(true);
131-
return instance;
154+
return instance;
155+
}
132156
}
133157

134158
private Poolable CreateInstance()

0 commit comments

Comments
 (0)