From 4d88c835760fc9f743a28b005750a0d2b73d52ca Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 11 Nov 2018 02:37:13 +0100 Subject: [PATCH] add rvalue reference overloads for add_torrent_params, settings_pack and session_params. Those structures are pretty large so being able to take them by rvalue reference may be beneficial --- bindings/python/src/session.cpp | 4 +- include/libtorrent/aux_/session_impl.hpp | 6 ++- include/libtorrent/kademlia/dht_tracker.hpp | 11 ++++- include/libtorrent/session.hpp | 50 ++++++++++++++------- include/libtorrent/session_handle.hpp | 3 +- simulation/test_dht_rate_limit.cpp | 4 +- src/kademlia/dht_tracker.cpp | 2 +- src/session.cpp | 38 ++++++++++++---- src/session_handle.cpp | 7 ++- src/session_impl.cpp | 2 +- 10 files changed, 92 insertions(+), 35 deletions(-) diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index 5e0d6fd59..63b2ee0c6 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -979,8 +979,8 @@ void bind_session() #endif // TORRENT_DISABLE_DHT .def("add_torrent", &add_torrent) .def("async_add_torrent", &async_add_torrent) - .def("async_add_torrent", <::session::async_add_torrent) - .def("add_torrent", allow_threads((lt::torrent_handle (session_handle::*)(add_torrent_params const&))<::session::add_torrent)) + .def("async_add_torrent", static_cast(<::session::async_add_torrent)) + .def("add_torrent", allow_threads(static_cast(<::session::add_torrent))) #ifndef BOOST_NO_EXCEPTIONS #if TORRENT_ABI_VERSION == 1 .def( diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 1d0024374..7648c7c02 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -395,7 +395,11 @@ namespace aux { void add_dht_router(std::pair const& node); void set_dht_settings(dht::dht_settings const& s); dht::dht_settings const& get_dht_settings() const { return m_dht_settings; } - void set_dht_state(dht::dht_state state); + + // you must give up ownership of the dht state + void set_dht_state(dht::dht_state&& state); + void set_dht_state(dht::dht_state const& state) = delete; + void set_dht_storage(dht::dht_storage_constructor_type sc); void start_dht(); void stop_dht(); diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 76c6a70b9..604791b5f 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -72,7 +72,16 @@ namespace libtorrent { namespace dht { , dht_settings const& settings , counters& cnt , dht_storage_interface& storage - , dht_state state); + , dht_state&& state); + + // the dht_state must be moved in! + dht_tracker(dht_observer* observer + , io_service& ios + , send_fun_t const& send_fun + , dht_settings const& settings + , counters& cnt + , dht_storage_interface& storage + , dht_state const& state) = delete; #if defined(_MSC_VER) && _MSC_VER < 1910 // workaround for a bug in msvc 14.0 diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 67f036b25..1ecebe154 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -131,10 +131,15 @@ namespace aux { // (ut_metadata, ut_pex and smart_ban). The default values in the // settings is to start the default features like upnp, NAT-PMP, // and dht for example. - explicit session_params(settings_pack sp = settings_pack()); + explicit session_params(settings_pack&& sp); + explicit session_params(settings_pack const& sp); + session_params(); + // This constructor helps to configure the set of initial plugins // to be added to the session before it's started. - session_params(settings_pack sp + session_params(settings_pack&& sp + , std::vector> exts); + session_params(settings_pack const& sp , std::vector> exts); session_params(session_params const&) = default; @@ -178,8 +183,13 @@ namespace aux { // In order to avoid a race condition between starting the session and // configuring it, you can pass in a session_params object. Its settings // will take effect before the session starts up. - explicit session(session_params params = session_params()) + explicit session(session_params const& params) + { start(session_params(params), nullptr); } + explicit session(session_params&& params) + { start(std::move(params), nullptr); } + session() { + session_params params; start(std::move(params), nullptr); } @@ -196,10 +206,10 @@ namespace aux { // call session::abort() and save the session_proxy first, then // destruct the session object, then sync with the io_service, then // destruct the session_proxy object. - session(session_params params, io_service& ios) - { - start(std::move(params), &ios); - } + session(session_params&& params, io_service& ios) + { start(std::move(params), &ios); } + session(session_params const& params, io_service& ios) + { start(session_params(params), &ios); } // Constructs the session objects which acts as the container of torrents. // It provides configuration options across torrents (such as rate limits, @@ -212,11 +222,12 @@ namespace aux { // NAT-PMP) and default plugins (ut_metadata, ut_pex and smart_ban). The // default is to start those features. If you do not want them to start, // pass 0 as the flags parameter. - session(settings_pack pack + session(settings_pack&& pack , session_flags_t const flags = start_default_features | add_default_plugins) - { - start(flags, std::move(pack), nullptr); - } + { start(flags, std::move(pack), nullptr); } + session(settings_pack const& pack + , session_flags_t const flags = start_default_features | add_default_plugins) + { start(flags, settings_pack(pack), nullptr); } // movable session(session&&) = default; @@ -239,12 +250,14 @@ namespace aux { // call session::abort() and save the session_proxy first, then // destruct the session object, then sync with the io_service, then // destruct the session_proxy object. - session(settings_pack pack + session(settings_pack&& pack , io_service& ios , session_flags_t const flags = start_default_features | add_default_plugins) - { - start(flags, std::move(pack), &ios); - } + { start(flags, std::move(pack), &ios); } + session(settings_pack const& pack + , io_service& ios + , session_flags_t const flags = start_default_features | add_default_plugins) + { start(flags, settings_pack(pack), &ios); } #if TORRENT_ABI_VERSION == 1 #ifdef __GNUC__ @@ -348,8 +361,11 @@ namespace aux { private: - void start(session_params params, io_service* ios); - void start(session_flags_t flags, settings_pack sp, io_service* ios); + void start(session_params&& params, io_service* ios); + void start(session_flags_t flags, settings_pack&& sp, io_service* ios); + + void start(session_params const& params, io_service* ios) = delete; + void start(session_flags_t flags, settings_pack const& sp, io_service* ios) = delete; // data shared between the main thread // and the working thread diff --git a/include/libtorrent/session_handle.hpp b/include/libtorrent/session_handle.hpp index ea9e2a626..679ec13df 100644 --- a/include/libtorrent/session_handle.hpp +++ b/include/libtorrent/session_handle.hpp @@ -215,7 +215,8 @@ namespace libtorrent { torrent_handle add_torrent(add_torrent_params const& params); #endif torrent_handle add_torrent(add_torrent_params const& params, error_code& ec); - void async_add_torrent(add_torrent_params params); + void async_add_torrent(add_torrent_params&& params); + void async_add_torrent(add_torrent_params const& params); #ifndef BOOST_NO_EXCEPTIONS #if TORRENT_ABI_VERSION == 1 diff --git a/simulation/test_dht_rate_limit.cpp b/simulation/test_dht_rate_limit.cpp index 61b126aed..800c76b3b 100644 --- a/simulation/test_dht_rate_limit.cpp +++ b/simulation/test_dht_rate_limit.cpp @@ -124,7 +124,7 @@ TORRENT_TEST(dht_rate_limit) std::unique_ptr dht_storage(dht::dht_default_storage_constructor(dhtsett)); auto dht = std::make_shared( &o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5) - , dhtsett, cnt, *dht_storage, state); + , dhtsett, cnt, *dht_storage, std::move(state)); dht->new_socket(ls); bool stop = false; @@ -245,7 +245,7 @@ TORRENT_TEST(dht_delete_socket) std::unique_ptr dht_storage(dht::dht_default_storage_constructor(dhtsett)); auto dht = std::make_shared( &o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5) - , dhtsett, cnt, *dht_storage, state); + , dhtsett, cnt, *dht_storage, std::move(state)); dht->start([](std::vector> const&){}); dht->new_socket(ls); diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 7eec91007..a840bcf99 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -88,7 +88,7 @@ namespace libtorrent { namespace dht { , dht_settings const& settings , counters& cnt , dht_storage_interface& storage - , dht_state state) + , dht_state&& state) : m_counters(cnt) , m_storage(storage) , m_state(std::move(state)) diff --git a/src/session.cpp b/src/session.cpp index 230f042d4..55e3b843a 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -319,7 +319,7 @@ namespace { return params; } - void session::start(session_params params, io_service* ios) + void session::start(session_params&& params, io_service* ios) { bool const internal_executor = ios == nullptr; @@ -334,16 +334,18 @@ namespace { *static_cast(this) = session_handle(m_impl); #ifndef TORRENT_DISABLE_EXTENSIONS - for (auto const& ext : params.extensions) + for (auto& ext : params.extensions) { - m_impl->add_ses_extension(ext); + m_impl->add_ses_extension(std::move(ext)); } #endif #ifndef TORRENT_DISABLE_DHT - m_impl->set_dht_settings(params.dht_settings); + m_impl->set_dht_settings(std::move(params.dht_settings)); m_impl->set_dht_state(std::move(params.dht_state)); - m_impl->set_dht_storage(params.dht_storage_constructor); + + TORRENT_ASSERT(params.dht_storage_constructor); + m_impl->set_dht_storage(std::move(params.dht_storage_constructor)); #endif m_impl->start_session(); @@ -376,7 +378,7 @@ namespace { } } - void session::start(session_flags_t const flags, settings_pack sp, io_service* ios) + void session::start(session_flags_t const flags, settings_pack&& sp, io_service* ios) { start({std::move(sp), default_plugins(!(flags & add_default_plugins))}, ios); @@ -431,16 +433,36 @@ namespace { } } - session_params::session_params(settings_pack sp) + session_params::session_params(settings_pack&& sp) : session_params(std::move(sp), default_plugins()) {} - session_params::session_params(settings_pack sp + session_params::session_params(settings_pack const& sp) + : session_params(sp, default_plugins()) + {} + + session_params::session_params() + : extensions(default_plugins()) +#ifndef TORRENT_DISABLE_DHT + , dht_storage_constructor(dht::dht_default_storage_constructor) +#endif + {} + + session_params::session_params(settings_pack&& sp , std::vector> exts) : settings(std::move(sp)) , extensions(std::move(exts)) #ifndef TORRENT_DISABLE_DHT , dht_storage_constructor(dht::dht_default_storage_constructor) +#endif + {} + + session_params::session_params(settings_pack const& sp + , std::vector> exts) + : settings(sp) + , extensions(std::move(exts)) +#ifndef TORRENT_DISABLE_DHT + , dht_storage_constructor(dht::dht_default_storage_constructor) #endif {} } diff --git a/src/session_handle.cpp b/src/session_handle.cpp index 0a364d90a..47af91c63 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -393,7 +393,12 @@ namespace { return sync_call_ret(&session_impl::add_torrent, p, ecr); } - void session_handle::async_add_torrent(add_torrent_params params) + void session_handle::async_add_torrent(add_torrent_params const& params) + { + async_add_torrent(add_torrent_params(params)); + } + + void session_handle::async_add_torrent(add_torrent_params&& params) { TORRENT_ASSERT_PRECOND(!params.save_path.empty()); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 74877b3b8..4558f8590 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -5860,7 +5860,7 @@ namespace aux { m_dht_settings = settings; } - void session_impl::set_dht_state(dht::dht_state state) + void session_impl::set_dht_state(dht::dht_state&& state) { m_dht_state = std::move(state); }