Jump to content

  • Log In with Google      Sign In   
  • Create Account


Efficiently Handling a Large amount of Objects with Short Lifespans


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 24 March 2014 - 10:34 PM

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.

 

 

 

 

 



Sponsor:

#2 Hodgman   Moderators   -  Reputation: 29294

Like
6Likes
Like

Posted 24 March 2014 - 10:49 PM

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



#3 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 25 March 2014 - 12:16 AM

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

 

Any other suggestions are welcome.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS