Object pools for efficient resource management

Posted by GameDevEnthusiast on Game Development See other posts from Game Development or by GameDevEnthusiast
Published on 2012-04-10T11:54:44Z Indexed on 2012/04/10 17:47 UTC
Read the original article Hit count: 294

How can I avoid using default new() to create each object?

My previous demo had very unpleasant framerate hiccups during dynamic memory allocations (usually, when arrays are resized), and creating lots of small objects which often contain one pointer to some DirectX resource seems like an awful lot of waste.

I'm thinking about:

  1. Creating a master look-up table to refer to objects by handles (for safety & ease of serialization), much like EntityList in source engine

  2. Creating a templated object pool, which will store items contiguously (more cache-friendly, fast iteration, etc.) and the stored elements will be accessed (by external systems) via the global lookup table.

The object pool will use the swap-with-last trick for fast removal (it will invoke the object's ~destructor first) and will update the corresponding indices in the global table accordingly (when growing/shrinking/moving elements). The elements will be copied via plain memcpy().

Is it a good idea? Will it be safe to store objects of non-POD types (e.g. pointers, vtable) in such containers?

Related post: Dynamic Memory Allocation and Memory Management

© Game Development or respective owner

Related posts about c++

Related posts about Performance