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();
if(!newNode) {
newNode = new QueueNode<T>(object);
}
}
T pop() {
QueueNodePool<T>::store(unusedNode);
return unusedNode->object;
}
}
`