he keding
2012-04-20 07:16:27 UTC
When I test io_service performance(windows),
I find the Handler copy 4 times by call stack.
Can I pass 1 and 4 handler by reference in stead of by value?
If no, any reason? thanks!
1 and 2 is post call
1.
template <typename Handler>
void win_iocp_io_service::post(Handler handler) // here handler copy
2.
completion_handler(Handler& h)
: operation(&completion_handler::do_complete),
handler_(BOOST_ASIO_MOVE_CAST(Handler)(h)) // here handler_ copy
{
}
3 and 4 is call when complete
3.
static void do_complete(io_service_impl* owner, operation* base,
const boost::system::error_code& /*ec*/,
std::size_t /*bytes_transferred*/)
{
// Take ownership of the handler object.
completion_handler* h(static_cast<completion_handler*>(base));
ptr p = { boost::addressof(h->handler_), h, h };
BOOST_ASIO_HANDLER_COMPLETION((h));
// Make a copy of the handler so that the memory can be deallocated
before
// the upcall is made. Even if we're not about to make an upcall, a
// sub-object of the handler may be the true owner of the memory
associated
// with the handler. Consequently, a local copy of the handler is
required
// to ensure that any owning sub-object remains valid until after we
have
// deallocated the memory here.
Handler handler(BOOST_ASIO_MOVE_CAST(Handler)(h->handler_)); // here
handler copy
4.
template <typename Function>
inline void asio_handler_invoke(Function function, ...) // here function
copy
{
function();
}
I find the Handler copy 4 times by call stack.
Can I pass 1 and 4 handler by reference in stead of by value?
If no, any reason? thanks!
1 and 2 is post call
1.
template <typename Handler>
void win_iocp_io_service::post(Handler handler) // here handler copy
2.
completion_handler(Handler& h)
: operation(&completion_handler::do_complete),
handler_(BOOST_ASIO_MOVE_CAST(Handler)(h)) // here handler_ copy
{
}
3 and 4 is call when complete
3.
static void do_complete(io_service_impl* owner, operation* base,
const boost::system::error_code& /*ec*/,
std::size_t /*bytes_transferred*/)
{
// Take ownership of the handler object.
completion_handler* h(static_cast<completion_handler*>(base));
ptr p = { boost::addressof(h->handler_), h, h };
BOOST_ASIO_HANDLER_COMPLETION((h));
// Make a copy of the handler so that the memory can be deallocated
before
// the upcall is made. Even if we're not about to make an upcall, a
// sub-object of the handler may be the true owner of the memory
associated
// with the handler. Consequently, a local copy of the handler is
required
// to ensure that any owning sub-object remains valid until after we
have
// deallocated the memory here.
Handler handler(BOOST_ASIO_MOVE_CAST(Handler)(h->handler_)); // here
handler copy
4.
template <typename Function>
inline void asio_handler_invoke(Function function, ...) // here function
copy
{
function();
}