dimanche 11 décembre 2016

c++ Object Pool design and performance concern regarding alike object types

I am stuck with dilemma regarding a design decision and I would like to hear some opinion from you.

I have 3 types of objects that all share pretty much the same qualities.

Base: BODY Children: PLAYER, CREATURE and ITEM

Their qualities are derived from the base class BODY: position, health, movement speed, unique id, etc.

Because there is going to be many allocation and deallocation, my intention is to create an object pool.

First, I though about a big object pool for every single unit.

Reasoning: They are pretty much the same size, and the extra size I could offset. However, for instance I could have 1000 thousand players and 30000 thousand monsters. Iterating to update on this array would overwhelm, also, it stops me in the future to maybe create other threads to handle player and creatures separately.

Then, I thought about creating multiple objects pools for each of the items, so I could easily set a more accurate fixed size, and possible separate in threads in the future if I want to. This way, threading or not threading, I could organize better my iteration.

The problem is, the object pool is exactly the same as from each other, not only I would have different copy of the code, I would have to write some extra code on the future. For instance, I could have a class WORLD that controls entity creation and delegation. WORLD would serve as a interface to spawn or destroy objects, I would send a type and the interface would determine what object pool to extract or recycle. Because I would have many different pools, of the type, I could do a switch statement based on the entity base class and works fine. This method would work; however, I could do a better job design avoiding switch statements.

Based on this last situation, I thought about doing a abstract object pool, and deviating the other pools, but again, I would not want to allocate on the heap my pool.

Lastly, it is what I think would work.

I could create a really large object pool, but instead of mixing up elements. I could create: the first x elements would be child type 1, then the second x elements would be child type 2, and etc. This way, I would have a single object pool, it would ease in my future code, and if I want to separate concerns, updating methods, threats, I could just iterate on the group of objects that I want. For instance, from 0 to 100 to Players, 101 to 20000 to Creatures.

How this last method sounds?

Aucun commentaire:

Enregistrer un commentaire