Palace Chan
Palace Chan

Reputation: 9183

Is there a faster way or optimization I can apply to my improvised memory pool?

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)

Upvotes: 2

Views: 153

Answers (1)

jxh
jxh

Reputation: 70382

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.

struct CatPool {
    size_t poolSize_;
    size_t i_;
    cat *pool_;
    cat *free_;
    typedef std::unique_ptr<cat[]> PoolPtr;
    std::list<PoolPtr> cleanup_;

    CatPool (size_t pool_size) : poolSize_(pool_size), free_(0) { grow(); }

    void grow () {
        i_ = 0;
        pool_ = new cat[poolSize_];
        cleanup_.push_back(PoolPtr(pool_));
    }

    cat * get () {
        cat *c = free_;
        if (c) {
            free_ = free_->next_;
            return c;
        }
        for (;;) {
            if (i_ < poolSize_) return &pool_[i_++];
            grow();
        }
    }

    void put (cat *c) {
        c->next_ = free_;
        free_ = c;
    }
};

But, you should consider using an existing pool allocator implementation. For example, Boost's object_pool.

Upvotes: 1

Related Questions