Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid dynamically allocating small objects?

I found that many times I only need a small std::map (say less than 10 keys), or a small std::vector containing only a few elements, and I think it's really a waste of performance to always dynamically allocate them, especially in structures like std::map<std::string, std::string>, std::vector<std::string>, there're really a lot dynamic allocation involved.

Any good advice? At least reduce the amount of dynamic allocation, better without sacrifying the ease of use. Thanks

like image 358
Chen Rushan Avatar asked May 15 '26 22:05

Chen Rushan


1 Answers

You may use stack-allocated memory for small size data (as stack allocations are very fast, basically just a stack-pointer movement; although stack's space is precious and it's a very limited resource), and heap-allocated memory for larger size. In other words, think along the lines of the std::string's small string optimization.

Moreover, to speed up allocations, you could also preallocate big memory chunks on the heap, and then carve smaller allocations inside those chunks, again basically just increasing a pointer inside the chunk. For a sample implementation of this pool allocator technique, consider reading this blog post.

You will find this CppCon 2016 talk interesting as well:

High Performance Code 201: Hybrid Data Structures

like image 73
Mr.C64 Avatar answered May 18 '26 12:05

Mr.C64



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!