Skip to content

Commit 71ed54b

Browse files
committed
common/async: Completion uses asio::recycling_allocator by default
ceph::async::Completion<> already respects the wrapped Handler's associated allocator, but defaults to std::allocator when there isn't one associated copy other asio primitives like asio::any_completion_handler<> (which satisfies very similar use case to our Completion) by instead choosing asio::recycling_allocator<void> as the default handler allocator from https://www.boost.org/doc/libs/1_85_0/doc/html/boost_asio/reference/recycling_allocator.html: > The recycling_allocator uses a simple strategy where a limited number > of small memory blocks are cached in thread-local storage, if the > current thread is running an io_context or is part of a thread_pool. Signed-off-by: Casey Bodley <[email protected]>
1 parent 24378a0 commit 71ed54b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/common/async/completion.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <boost/asio/defer.hpp>
2222
#include <boost/asio/dispatch.hpp>
2323
#include <boost/asio/executor_work_guard.hpp>
24+
#include <boost/asio/recycling_allocator.hpp>
2425
#include <boost/asio/post.hpp>
2526

2627
#include "bind_handler.h"
@@ -173,7 +174,8 @@ class CompletionImpl final : public Completion<void(Args...), T> {
173174
Handler handler;
174175

175176
// use Handler's associated allocator
176-
using Alloc2 = boost::asio::associated_allocator_t<Handler>;
177+
using DefaultAlloc = boost::asio::recycling_allocator<void>;
178+
using Alloc2 = boost::asio::associated_allocator_t<Handler, DefaultAlloc>;
177179
using Traits2 = std::allocator_traits<Alloc2>;
178180
using RebindAlloc2 = typename Traits2::template rebind_alloc<CompletionImpl>;
179181
using RebindTraits2 = std::allocator_traits<RebindAlloc2>;
@@ -196,7 +198,7 @@ class CompletionImpl final : public Completion<void(Args...), T> {
196198
void destroy_defer(std::tuple<Args...>&& args) override {
197199
auto w = std::move(work);
198200
auto ex2 = w.second.get_executor();
199-
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler);
201+
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler, DefaultAlloc{});
200202
auto f = bind_and_forward(ex2, std::move(handler), std::move(args));
201203
RebindTraits2::destroy(alloc2, this);
202204
RebindTraits2::deallocate(alloc2, this, 1);
@@ -205,7 +207,7 @@ class CompletionImpl final : public Completion<void(Args...), T> {
205207
void destroy_dispatch(std::tuple<Args...>&& args) override {
206208
auto w = std::move(work);
207209
auto ex2 = w.second.get_executor();
208-
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler);
210+
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler, DefaultAlloc{});
209211
auto f = bind_and_forward(ex2, std::move(handler), std::move(args));
210212
RebindTraits2::destroy(alloc2, this);
211213
RebindTraits2::deallocate(alloc2, this, 1);
@@ -214,14 +216,14 @@ class CompletionImpl final : public Completion<void(Args...), T> {
214216
void destroy_post(std::tuple<Args...>&& args) override {
215217
auto w = std::move(work);
216218
auto ex2 = w.second.get_executor();
217-
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler);
219+
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler, DefaultAlloc{});
218220
auto f = bind_and_forward(ex2, std::move(handler), std::move(args));
219221
RebindTraits2::destroy(alloc2, this);
220222
RebindTraits2::deallocate(alloc2, this, 1);
221223
boost::asio::post(std::move(f));
222224
}
223225
void destroy() override {
224-
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler);
226+
RebindAlloc2 alloc2 = boost::asio::get_associated_allocator(handler, DefaultAlloc{});
225227
RebindTraits2::destroy(alloc2, this);
226228
RebindTraits2::deallocate(alloc2, this, 1);
227229
}
@@ -238,7 +240,7 @@ class CompletionImpl final : public Completion<void(Args...), T> {
238240
public:
239241
template <typename ...TArgs>
240242
static auto create(const Executor1& ex, Handler&& handler, TArgs&& ...args) {
241-
auto alloc2 = boost::asio::get_associated_allocator(handler);
243+
auto alloc2 = boost::asio::get_associated_allocator(handler, DefaultAlloc{});
242244
using Ptr = std::unique_ptr<CompletionImpl>;
243245
return Ptr{new (alloc2) CompletionImpl(ex, std::move(handler),
244246
std::forward<TArgs>(args)...)};

0 commit comments

Comments
 (0)