Object pooling our resources to save time later
The last pattern we are discussing in this book is object pooling. This pattern aims to tackle one of the core problems with the CPU: allocating and deallocating memory is a slow process. Every time you spawn a new actor, the space it needs in memory must be reserved in the right subdivisions for each variable and handed back to the game process ready to receive data. Every time you delete an actor, that memory must be freed from all references and returned to the heap. For something such as a minigun spawning 3,000 projectiles per minute, that means a lot of allocation of the chunks for memory, which are all the same size. Object pooling is the practice of predicting this massive cost and offsetting it to a place where the lag it causes is not so noticeable. This for us means spawning all the projectiles we could possibly need at the start of the game and hiding them. When one is required, it is taken from the shelf of deactivated projectiles...