0

I need to create many objects which all contain a std::vector<bool> of the same size (but the size is known at runtime).

I already use boost_pool for the objects allocations.

What's the right way to use boost_pool for the std::vector<bool> inside the objects to avoid memory fragmentation?

fontanf
  • 209
  • 1
  • 6
  • You already use boost_pool? What it is? You mean boost::pool_allocator? – Öö Tiib Dec 27 '22 at 08:59
  • @ÖöTiib For the objects, I use a `boost::object_pool`. But I guess that I'll need another allocator for the vectors. – fontanf Dec 27 '22 at 09:03
  • You have to use the vector's allocator template argument with one of the [boost allocator types](https://www.boost.org/doc/libs/1_81_0/doc/html/container/extended_allocators.html) – Homer512 Dec 27 '22 at 09:20
  • @Homer512 Does that work with `std::vector` (spaced optimized) or with `boost::dynamic_bitset`? – fontanf Dec 27 '22 at 09:25
  • It is compatible with [STL containers](https://godbolt.org/z/aGqff4c4P) Seeing how it is intended to work with node containers (set, map, list), I'm not sure how much benefit you will get. ```boost::dynamic_bitset``` is just a wrapper around ```std::vector``` so it should work the same with all. – Homer512 Dec 27 '22 at 09:41
  • I just did some quick benchmarks and saw neither performance nor memory overhead improvements. If anything, GCC's allocator was faster on repeated allocations + deallocations compared to boost's ```adaptive_pool``` – Homer512 Dec 27 '22 at 10:11
  • BTW: If you want to optimize for minimal memory, you could abuse ```std::string``` and its short string optimization. That gives you 120-192 usable bits before dynamic allocation is used. – Homer512 Dec 27 '22 at 10:18
  • @Homer512 Indeed, in my experiments, I didn't see a significant improvement after changing the allocators as well. I hoped to get some speed up here. Unfortunately, it's apparently not the case. Thanks for mentioning the short string optimization. I didn't know about it. – fontanf Dec 27 '22 at 11:18
  • boost`s [```small_vector```](https://www.boost.org/doc/libs/1_81_0/doc/html/boost/container/small_vector.html) may be more convenient than string. – Homer512 Dec 27 '22 at 15:24

0 Answers0