Is there a way to use boost :: obect_pool with faster free operations

I used boost object_pool for some time and was generally happy with the results. Previously, I mainly allocated individual objects, but rarely freed them individually, immediately freed the entire pool. Recently, when I was faced with the need to free many objects from the pool, I found it to be VERY slow.

Obviously, the pool is looking for a list of already released pieces in order to link the recently released object. The documentation talks about ordered and unordered pools and mentions pool_allocator as well as fast_pool_allocator. Presumably, an unordered pool (using fast_memory_allocator) will release chunks of memory much faster. However, I cannot understand how I can use this.

Do I understand correctly that I have a choice between pool_allocator and fast_pool_allocator only in combination with boost :: singleton_pool, but not with boost :: object_pool?

The following is a small test program illustrating the problem. It was built with VS2013 and increased 1_57_0. The test allocates n objects in the object pool, and then randomly releases 10%. This one has some rough time frame, which shows that for n == 100,000 the distribution takes 0.004 seconds and the output takes 0.4 seconds. Meanwhile, for n == 1,000,000, it takes 0.02 seconds to allocate and 42 seconds to release on my machine.

#include <boost/pool/object_pool.hpp> #include "time.h" #include <vector> #include <random> struct foo { int data[10]; }; struct test { test(unsigned n) : size{ n } {} void run(); float elapsedSec(clock_t& watch); unsigned size; boost::object_pool<foo> _pool; float mallocSec; float freeSec; }; void test::run() { std::vector<foo *> foos(size, nullptr); std::default_random_engine generator; std::uniform_int_distribution<int> distribution(0, size - 1); auto dice = std::bind(distribution, generator); clock_t watch = clock(); for (int i = 0; i < size; ++i) foos[i] = _pool.malloc(); mallocSec = elapsedSec(watch); for (int i = 0; i < size / 10;) { auto idx = dice(); if (foos[idx] == nullptr) continue; _pool.free(foos[idx]); foos[idx] = nullptr; i += 1; } freeSec = elapsedSec(watch); } float test::elapsedSec(clock_t& watch) { clock_t start = watch; watch = clock(); return (watch - start) / static_cast<float>(CLOCKS_PER_SEC); } 
-4
source share
1 answer

A free call reaches ordered_free on the base dispenser. Indeed, ordered_malloc_need_resize() takes up most of the execution time .

Do I understand correctly that I have a choice between pool_allocator and fast_pool_allocator only in combination with boost :: singleton_pool, but not with boost :: object_pool?

Yes. object_pool explicitly uses the ordered_free function in the underlying simple_segregated_storage . This is clearly by design (although at the moment the rationale is eluding me. Obviously, in the intended use for object_pool it makes sense to always optimize the allocation / allocation of arrays).

The way I understand things, now pool_allocator and fast_pool_allocations are classes that stand on their own and are not related to singleton_pool arguments / parameters.

Yes. They have tough possibilities for using instances of single pools. The Boost Pool clearly precedes standard library support for stateful generators. You can copy the implementation of fast_pool_allocator out to use a runtime instance instead of a singleton pool.

The following non_boost::fast_pool_allocator uses a state distribution agent on top of a specific instance of an object pool. This makes the generator generator workable. This is mainly a pointer to a pool.

_alloc.destroy used to destroy an instance of foo and free memory. Any unsolicited items will still be freed after the destruction of _pool ( Note , since we are not using object_pool , no destructors for foo will be executed in that case., foo is POS and therefore trivially destroyed. If not, you can, of course, use std::unique_ptr or similar, or really write a version of object_pool that doesn't insist on an ordered distribution).

Demo

Live on coliru

 #include <boost/pool/pool.hpp> #include <boost/pool/object_pool.hpp> #include <boost/pool/pool_alloc.hpp> #include "time.h" #include <vector> #include <random> struct foo { int data[10]; }; namespace non_boost { template <typename T, typename UserAllocator = boost::default_user_allocator_new_delete> class fast_pool_allocator { public: typedef T value_type; typedef UserAllocator user_allocator; typedef value_type * pointer; typedef const value_type * const_pointer; typedef value_type & reference; typedef const value_type & const_reference; typedef boost::pool<UserAllocator> pool_type; typedef typename pool_type::size_type size_type; typedef typename pool_type::difference_type difference_type; template <typename U> struct rebind { typedef fast_pool_allocator<U, UserAllocator> other; }; pool_type* _ref; public: fast_pool_allocator(pool_type& ref) : _ref(&ref) { } fast_pool_allocator(fast_pool_allocator const&) = default; fast_pool_allocator& operator=(fast_pool_allocator const&) = default; // Not explicit, mimicking std::allocator [20.4.1] template <typename U> fast_pool_allocator(const fast_pool_allocator<U, UserAllocator> & other) : _ref(other._ref) { } // Default destructor used. static pointer address(reference r) { return &r; } static const_pointer address(const_reference s) { return &s; } static size_type max_size() { return (std::numeric_limits<size_type>::max)(); } void construct(const pointer ptr, const value_type & t) { new (ptr) T(t); } void destroy(const pointer ptr) { ptr->~T(); } bool operator==(fast_pool_allocator const& rhs) const { return _ref == rhs._ref; } bool operator!=(fast_pool_allocator const& rhs) const { return _ref != rhs._ref; } pointer allocate(const size_type n) { const pointer ret = (n == 1) ? static_cast<pointer>( (_ref->malloc)() ) : static_cast<pointer>( _ref->ordered_malloc(n) ); if (ret == 0) boost::throw_exception(std::bad_alloc()); return ret; } pointer allocate(const size_type n, const void * const) { return allocate(n); } pointer allocate() { const pointer ret = static_cast<pointer>( (_ref->malloc)() ); if (ret == 0) boost::throw_exception(std::bad_alloc()); return ret; } void deallocate(const pointer ptr, const size_type n) { #ifdef BOOST_NO_PROPER_STL_DEALLOCATE if (ptr == 0 || n == 0) return; #endif if (n == 1) (_ref->free)(ptr); else (_ref->free)(ptr, n); } void deallocate(const pointer ptr) { (_ref->free)(ptr); } }; //Specialization of fast_pool_allocator<void> required to make the allocator standard-conforming. template<typename UserAllocator> class fast_pool_allocator<void, UserAllocator> { public: typedef void* pointer; typedef const void* const_pointer; typedef void value_type; template <class U> struct rebind { typedef fast_pool_allocator<U, UserAllocator> other; }; }; } struct test { test(unsigned n) : size{ n } {} void run(); float elapsedSec(clock_t& watch); unsigned size; boost::pool<boost::default_user_allocator_malloc_free> _pool { sizeof(foo) }; non_boost::fast_pool_allocator<foo, boost::default_user_allocator_malloc_free> _alloc { _pool }; float mallocSec; float freeSec; }; void test::run() { std::vector<foo *> foos(size, nullptr); std::default_random_engine generator; std::uniform_int_distribution<int> distribution(0, size - 1); auto dice = std::bind(distribution, generator); clock_t watch = clock(); for (unsigned i = 0; i < size; ++i) foos[i] = _alloc.allocate(); mallocSec = elapsedSec(watch); for (unsigned i = 0; i < size / 10;) { auto idx = dice(); if (foos[idx] != nullptr) { _alloc.destroy(foos[idx]); foos[idx] = nullptr; } i += 1; } freeSec = elapsedSec(watch); } float test::elapsedSec(clock_t& watch) { clock_t start = watch; watch = clock(); return (watch - start) / static_cast<float>(CLOCKS_PER_SEC); } int main() { test t(10u << 20); t.run(); std::cout << t.mallocSec << "\n"; std::cout << t.freeSec << "\n"; } 

Print (on my system):

 0.135127 0.050991 
+2
source

Source: https://habr.com/ru/post/983495/


All Articles