Object pooling is an optimization pattern. It's used to improve performance, in certain cases, by re-using objects instead of allocating memory for them on demand. In C/C++, one the things object pooling has to offer is a solution to avoid memory fragmentation. In C#, we don't have to worry about memory fragmentation thanks to garbage collection. However, garbage collection can be still be too expensive for certain parts of real-time applications, especially on mobile devices with slower CPUs and simpler garbage collectors. More details on object pooling here.
ObjectPool is available in the default
MonoGame.Extended is distributed via a NuGet package. You can add the NuGet package to your C# project through your IDE of choice (Visual Studio, Xamarin Studio, Rider, etc) or through the Command Line Interface (CLI) using the dotnet command.
Always profile the game for performance problems!
Pool<T> without first profiling for the need of one may result in a decrease in performance in certain cases. If you are unsure, don't use the object pooling pattern.
Creating a Pool-able Object
All objects which can be pooled need to implement the
The following is a code snippet with comments demonstrating how to implement the interface.
Creating a Pool
Pool<T> is similar to any generic collection, i.e
T has to implement
The parameter parameter is the delegate responsible for creating each object instance.
Having too large of a capacity will waste memory, but having too small of a capacity will limit the number of object instances that can be pooled.
Getting a Pooled Object
A free pooled object instance can be requested from the pool instance.
If the pool is empty, the result will be
Returning a Object to the Pool
When the object instance is no longer needed it should be returned to the pool so it can be re-used.