Efficiently Handling a Large amount of Objects with Short Lifespans

Started by
1 comment, last by Ludus 10 years ago

I have searched this topic a number of times, but could not find satisfactory answers. My question is, what are some good ways to efficiently handle a large amount of objects with short lifespans in C++? (think bullets, spells, massive amounts of enemies that appear and disappear off screen) More specifically, what kind of container should store these objects? Should these objects be dynamically allocated and deallocated with "new" and "delete", thus making this a container of pointers to the objects? Instead of deleting objects directly from the list at the end of the objects' lifespan, should they be flagged as "dead" and be removed or replaced at a more opportune time?

I know that when using a vector there is a trick of swapping the element to be deleted with the very last element of the vector, then using pop_back to delete it, thus avoiding relocation of elements in the vector. What are some other tricks when dealing with a large number of short-lived objects?

The container these objects are stored in will double as a way of iterating through these objects for the purpose of updating logic, so iteration needs to be quick as well.

Advertisement

Probably a pool container, implemented using a fixed-size-array.
You've got a vector of objects, and a vector of links. To begin with, you make each link point to the next one (a linked list of items stored in an array).
You've also got two pointers, the first 'free' object (which to begin with, points to the first item in the array) and the first 'used' object (which points to NULL initially).
To allocate, you take the first object out of the free list, and add it to the used list. To release, you unlink the object from the used list and add it back into the free list.
To iterate, you go to the first used object, and follow the links.

My version is here: https://code.google.com/p/eight/source/browse/include/eight/core/alloc/pool.h cool.png

Yes, it seems that pool allocation definitely fits the criteria. I'll look into it.Thanks for the information.

Any other suggestions are welcome.

This topic is closed to new replies.

Advertisement