Wednesday, August 24, 2016

c++ - Object pools for efficient resource management


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




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...