Recycled Objects

Suppose I need to allocate and delete an object on a heap (of arbitrary size) often, is there any performance advantage if, instead of deleting these objects, I return it to some “pool” that will be reused later?

would it be useful to reduce heap allocation / deallocation? or it will be slower compared to memory allocation performance, since the "pool" must control the dynamic set of pointers.

my use case: suppose I create a queue container based on a linked list and each node of this list is allocated on the heap, so every call to push () and pop () will allocate and release that node:

`

template <typename T> struct QueueNode {
    QueueNode<T>* next;
    T object;
}

template <typename T> class Queue {
    void push(T object) {
        QueueNode<T>* newNode = QueueNodePool<T>::get(); //get recycled node
        if(!newNode) {
            newNode = new QueueNode<T>(object);
        }
        // push newNode routine here..
    }
    T pop() {
        //pop routine here...
        QueueNodePool<T>::store(unusedNode); //recycle node
        return unusedNode->object;
    }
}

`

+3
5

- , . . , .

,

+4

, . , , .

+1

Boost - , : >

+1

, . , , .

0

" " . , , , , malloc libc.

Since most of Doug Lee’s work is present in the GNU lib, you can read about his experiences in Memory Alarms .

0
source

All Articles