Object pools for efficient resource management
- by GameDevEnthusiast
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:
Creating a master look-up table to refer to objects by handles (for safety & ease of serialization), much like EntityList in source engine
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