Mukul Gupta
Mukul Gupta

Reputation: 2425

Memory allocator for a light tree based data structure for frequent allocation and deallocation

I have a binary tree based data structure that does all operations in O(log N). It allocates / deallocates memory via new and delete. In my typical usage, the data structure is in use for a 40 minutes run and allocates/de-allocates small chunks tree nodes half a billion times. The overall memory footprint of this data structure is low as de-allocation happens soon enough. This has been identified as a performance bottleneck.

I am thinking of overloading operator new and delete or modifying my existing code to be able to use allocators and create a custom allocator for such purpose. I am thinking of allocating contiguous large amount of memory upfront and re-using chunks of memory from it. Does C++11 standard have such a pool based memory allocator?

What memory allocation/de-allocation strategy would be best suited for such usage?

Upvotes: 1

Views: 392

Answers (2)

Surt
Surt

Reputation: 16109

Use C++17 std::pmr::unsynchronized_pool_resource or the synchronized version.

Upvotes: 0

TonySalimi
TonySalimi

Reputation: 8427

There is no out-of-the-box feature in C++11 for memory pooling. But you can use Boost.Pool to this aim. From the Boost.Pool Documentation:

When should I use Pool?

Pools are generally used when there is a lot of allocation and deallocation of small objects. Another common usage is the situation above, where many objects may be dropped out of memory. In general, use Pools when you need a more efficient way to do unusual memory control.

Upvotes: 1

Related Questions