[SOLVED]Performance optimization with recycle/create -- and other performance considerations


(Helios) #1

I’m implementing my own entity recycling system inside a big old factory singleton, so I’m trying to sort out where the most savings would come from.

does the overhead come mostly from calling the “new Entity()” or from actual world.add and world.remove operations?

as a side note, is there any kind of reference sheet for expensive operations to minimize in as3/flashpunk? My general policy on performance optimization is “don’t worry about it unless it becomes a problem, but try to minimize calls to expensive operations if you can predict them.”


(Zachary Lewis) #2

You’ll notice the benefit of recycling entities most when you’re creating and removing entities very frequently, such as in a game where player and enemy bullets are being shot or destroyed every tick.

The reuse of existing entities to use a constant amount of memory is what causes the performance saving.

Without recycling, the entity list will begin to fill up with entities. When an entity is destroyed, it is removed from the entity list, but it persists in memory. As memory fills up, the game begins to “run worse.” When the garbage collector runs, the old entities are expunged from memory and the game “runs better.”

With recycling, the entity list will begin to fill up with entities. When an entity is destroyed, it is moved from the entity list to the recycle list. Then, instead of using more memory, adding a new entity reuses existing memory, causing the game to “run steadily.”

As far as optimizations go, here are some resources.


(Helios) #3

this is a great comprehensive answer. I’m going to make sure this question is appropriately searchable.

It seems like I won’t be getting any particular benefit from implementing this factory with recycle. It already takes dead entities and throws them in a vector to be re-used when needed.

the reason I’m using this instead of recycle by itself – my factory is managing other things, like ensuring there is a minimum stockpile of entities ready to be used, which it refills during times when nothing intense is going on