What is the fast approach of Pooling system in Unity? A List<Transform>
, simple Transform[]
array , Queue or Stack ?

- 15
- 5
3 Answers
I agree in principle with @Johnny's answer, but because there is a Unity3D tag, indicating a game-development context, I believe there generally is a generally preferred and recommended approach, which in turn give a generally preferred and recommended data-structure.
The basics in general: the main difference between these two data-structures is that, while arrays can be made to grow and shrink, they are not designed for it. Lists are, and have a dynamic size natively.
The context of game-dev and pooling specifically:
Generally, pools are intended as dynamic in size. They can grow and shrink. Pools that are static in size are generally not referred as pools, but rather as caches. The base concept of storing pre-initialized objects for use when needed is the same, and the distinction is generally the dynamicity.
In the context of games specifically, pools are usually used to avoid FPS drop spikes due to initialization or garbage-collection of large quantities of objects in a single frame or a short amount of time.
Games need these spikes in amounts of objects due to things like effects, explosions, destructible objects or terrain, etc.
Pools can additionally be used as controls to limit amount of certain types of objects deployed by a player or in the scene in general. The advantage of pools in this case is that the limit is dynamic and configurable, while an array-based cache would be static to the hard-coded size of the array unless worked-around.
Furthermore, and this is probably the main reason to use a List, is that for games, a single generic pooling system is often enough, and so creating one such system to be flexible allows you to reuse it in multiple areas of the same game, as well as in other games later. Saving you development time and effort.

- 732,580
- 175
- 1,330
- 1,459

- 6,275
- 5
- 43
- 58
You should choose the right data structure regarding your actual requirements. There is no too much difference between List<T>
and T[]
in C#, List<T>
is actually an abstraction of the array in essence.
Ask yourself which functionality you expect from your object pool, e.g, if the number of elements is fixed you could use T[]
. If the number of elements changes, you need to add\remove elements, then maybe List<T>
is a better option.
Also, each collection has its own performances expressed as asymptotic complexity. You could consider that regards how many elements you do expect and which operation you are going to perform, read here.
In the end, there is no silver bullet you should choose what fits the best.

- 8,939
- 2
- 28
- 33
If you have fixed size of objects and you want to loop through them frequently you better use array[]. But if you add and remove objects often you better use List. Because resizing array[] is an expensive process.

- 316
- 2
- 6
-
just btw: so is resizing a `List` since internally it uses an array for storing the values ;) Lists duplicate their array size (starting with 4) everytime adding an element would exceed the size. – derHugo Apr 08 '19 at 06:32