I have a class as follows:
typedef struct grid_cell_type {
int x;
int y;
grid_cell_type(int x0, int y0){
x=x0;
y=y0;
}
} grid_cell;
I’ll be pumping approximately 100 million of these through a queue.
Right now, this happens as follows:
my_queue.push(new grid_cell(x0,y0));
The individual piece-wise allocation of all these objects seems as though it is probably not as quick as some mass-allocation.
Any thoughts as to the best strategy to pursue here?
These are small and self-contained objects – put them directly in the queue instead of putting the pointers.
intis 32-bit (which it is, for example, under Visual C++), the pointer will be as large as the object itself! So even if you have a bulk allocator, you still pay this price.While you could devise a fairly efficient “bulk” allocation scheme, I think it’s simpler to sidestep the issue and altogether avoid the individual object allocations.
— EDIT —
You can push elements to the
std::queuelike this:For the
std::priority_queue, you’d need to decide how you want to order the elements.— EDIT 2 —
@Richard Your code is quite different.
push, your code would allocate a new block of dynamic memory, construct the object in it (i.e. assignxandy) and then push the pointer to that block of memory to the queue.queueitself. And as you already noted, few big allocationsare better than many small ones.
Your code is:
A specialized bulk allocator could remove the last two problems but why not remove them all?
— EDIT 3 —
As for speed, the general dynamic memory allocation is expensive (about 40-50 machine instructions for best allocators).
The specialized block allocator would be much faster, but you still have an issue of memory latency: keeping everything nicely together is guaranteed to achieve better cache locality and be much more suitable for CPU’s prefetching logic than repeatedly “jumping” between the queue and the actual objects by de-referencing pointers.