I have a couple different objects that allocate certain objects to communicate with each other and I decided to give them each a locally managed memory pool where a node can be tagged as free merely by setting a flag within it.
The idea is like this:
struct cat {/**couple data fields*/};
struct rat
{
rat() : poolIndex_(0), poolSize_(INITIAL_SIZE), pool_(new cat *[poolSize_])
{
for (size_t i = 0; i < poolSize_; ++i)
pool_[i] = new cat;
}
size_t poolIndex_;
size_t poolSize_;
cat** pool_;
};
I provide a non-member function for rat and his friends to resize their pool whenever they run out of free cat nodes (giving out the nodes is done via poolIndex_++ % poolSize_;). The non-member function is as follows:
void quadruplePool(cat*** pool, size_t& poolIndex, size_t& poolSize)
{
poolIndex = poolSize;
cat** tmp = new cat *[poolSize*4];
for (size_t i = 0; i < poolSize; ++i)
tmp[i] = (*pool)[i];
delete[] (*pool);
(*pool) = tmp;
poolSize = poolSize*4;
for (size_t i = poolIndex; i < poolSize; ++i)
(*pool)[i] = new cat;
}
is there something in the code which could give me a speedup from what I have right now? (Speed is by far critical for me)
I think it is far more efficient to do an array allocation of
cats, and when they are freed, place them in a free list.But, you should consider using an existing pool allocator implementation. For example, Boost’s
object_pool.