Object Recycler
Last updated
Last updated
A system for recycling spawned objects to improve performance. This type of system is very popular among open world games, games with lots of objects, or games with very tight performance requirements.
What does it do?
Spawning objects is expensive. So why are we destroying objects when we can recycle them? Take for example a projectile: If the player shoots a projectile, it lands on a target, deals damage, spawns VFX, etc. But instead of destroying the projectile, we make it invisible, add it to a pool, then when the player shoots another projectile, we check if any projectiles in the pool are available for re-use. If so, then set a new world location, make it visible, reset any parameters and treat it as if you just spawned in a whole new actor, without paying the cost of spawning and constructing an actor.
How fast is it at recycling an object?
The system has been designed to scale extremely well. So 1 object in the pool or a thousand. In a test of over 3000+ calls where the pool slowly scaled up from 0-200 objects being recycled, the average recycling time was 3 microseconds. For context, in this test the actor that was being spawned only had 1 widget component for displaying damage numbers. On average, this actor was taking 136 microseconds to spawn from scratch. Recycling it was about 45 times more efficient.
The above was just exclusive time. If we take inclusive time into account, recycling took about 10 microseconds and spawning from scratch took 266 microseconds. Not as efficient as exclusive time, but still 26.6 times faster, but since we are now talking about inclusive time, this scaling will drastically improve when the actor has more components and is more expensive to spawn. Recycling an actor is usually just resetting its location and some parameters.
A hidden benefit this system adds is that it reduces hitches caused by the garbage collector. The garbage collector is one of the main causes of large hitches and low performance spikes in most games, especially UE games as the game starts removing hundreds of objects from memory (This should be improved in 5.4 with the asynchronous garbage collector). Since this system is re-using objects and not allowing them to be garbage collected, this hitch can be greatly reduced.
This is a subsystem, so no reparenting of any kind is needed. Just simply call these functions:
For classes to participate, they must implement the I_ObjectRecycling
interface, which includes these functions: