From f41d2b5d3bd16b6183aa771de0ba9f06658703d4 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 6 Nov 2017 03:17:56 +0200 Subject: [PATCH] move shared_ptr instead of copying them, when constructing DHT observers --- .../libtorrent/kademlia/direct_request.hpp | 4 +-- include/libtorrent/kademlia/find_data.hpp | 4 +-- include/libtorrent/kademlia/get_item.hpp | 4 +-- include/libtorrent/kademlia/get_peers.hpp | 8 ++--- include/libtorrent/kademlia/node.hpp | 4 +-- include/libtorrent/kademlia/observer.hpp | 30 +++++++------------ include/libtorrent/kademlia/put_data.hpp | 4 +-- include/libtorrent/kademlia/rpc_manager.hpp | 5 ++-- .../libtorrent/kademlia/sample_infohashes.hpp | 2 +- .../kademlia/traversal_algorithm.hpp | 4 +-- src/kademlia/node.cpp | 10 +++---- src/kademlia/sample_infohashes.cpp | 4 +-- test/test_dht.cpp | 2 +- 13 files changed, 38 insertions(+), 47 deletions(-) diff --git a/include/libtorrent/kademlia/direct_request.hpp b/include/libtorrent/kademlia/direct_request.hpp index 539f80d21..13679eaf6 100644 --- a/include/libtorrent/kademlia/direct_request.hpp +++ b/include/libtorrent/kademlia/direct_request.hpp @@ -67,9 +67,9 @@ protected: struct direct_observer : observer { - direct_observer(std::shared_ptr const& algo + direct_observer(std::shared_ptr algo , udp::endpoint const& ep, node_id const& id) - : observer(algo, ep, id) + : observer(std::move(algo), ep, id) {} void reply(msg const& m) override diff --git a/include/libtorrent/kademlia/find_data.hpp b/include/libtorrent/kademlia/find_data.hpp index 62eaa3332..5268c7223 100644 --- a/include/libtorrent/kademlia/find_data.hpp +++ b/include/libtorrent/kademlia/find_data.hpp @@ -76,9 +76,9 @@ protected: struct find_data_observer : traversal_observer { find_data_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : traversal_observer(algorithm, ep, id) + : traversal_observer(std::move(algorithm), ep, id) {} void reply(msg const&) override; diff --git a/include/libtorrent/kademlia/get_item.hpp b/include/libtorrent/kademlia/get_item.hpp index abfd09467..adac96bb0 100644 --- a/include/libtorrent/kademlia/get_item.hpp +++ b/include/libtorrent/kademlia/get_item.hpp @@ -80,9 +80,9 @@ class get_item_observer : public find_data_observer { public: get_item_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : find_data_observer(algorithm, ep, id) + : find_data_observer(std::move(algorithm), ep, id) {} void reply(msg const&) override; diff --git a/include/libtorrent/kademlia/get_peers.hpp b/include/libtorrent/kademlia/get_peers.hpp index 736d1fa23..c9a181f49 100644 --- a/include/libtorrent/kademlia/get_peers.hpp +++ b/include/libtorrent/kademlia/get_peers.hpp @@ -85,9 +85,9 @@ private: struct get_peers_observer : find_data_observer { get_peers_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : find_data_observer(algorithm, ep, id) + : find_data_observer(std::move(algorithm), ep, id) {} void reply(msg const&) override; @@ -100,9 +100,9 @@ private: struct obfuscated_get_peers_observer : traversal_observer { obfuscated_get_peers_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : traversal_observer(algorithm, ep, id) + : traversal_observer(std::move(algorithm), ep, id) {} void reply(msg const&) override; }; diff --git a/include/libtorrent/kademlia/node.hpp b/include/libtorrent/kademlia/node.hpp index 0e040de93..f6cd2c27d 100644 --- a/include/libtorrent/kademlia/node.hpp +++ b/include/libtorrent/kademlia/node.hpp @@ -68,9 +68,9 @@ TORRENT_EXTRA_EXPORT entry write_nodes_entry(std::vector const& node class announce_observer : public observer { public: - announce_observer(std::shared_ptr const& algo + announce_observer(std::shared_ptr algo , udp::endpoint const& ep, node_id const& id) - : observer(algo, ep, id) + : observer(std::move(algo), ep, id) {} void reply(msg const&) override { flags |= flag_done; } diff --git a/include/libtorrent/kademlia/observer.hpp b/include/libtorrent/kademlia/observer.hpp index bde3e9419..c085ea9a9 100644 --- a/include/libtorrent/kademlia/observer.hpp +++ b/include/libtorrent/kademlia/observer.hpp @@ -54,22 +54,12 @@ using observer_flags_t = libtorrent::flags::bitfield_flag { - observer(std::shared_ptr const& a + observer(std::shared_ptr a , udp::endpoint const& ep, node_id const& id) - : m_sent() - , m_algorithm(a) + : m_algorithm(std::move(a)) , m_id(id) - , m_port(0) - , m_transaction_id() - , flags{} { - TORRENT_ASSERT(a); -#if TORRENT_USE_ASSERTS - m_in_constructor = true; - m_was_sent = false; - m_was_abandoned = false; - m_in_use = true; -#endif + TORRENT_ASSERT(m_algorithm); set_target(ep); } @@ -149,18 +139,18 @@ private: address_v4::bytes_type v4; } m_addr; - std::uint16_t m_port; + std::uint16_t m_port = 0; // the transaction ID for this call - std::uint16_t m_transaction_id; + std::uint16_t m_transaction_id = 0; public: - observer_flags_t flags; + observer_flags_t flags{}; #if TORRENT_USE_ASSERTS - bool m_in_constructor:1; - bool m_was_sent:1; - bool m_was_abandoned:1; - bool m_in_use:1; + bool m_in_constructor = true; + bool m_was_sent = false; + bool m_was_abandoned = false; + bool m_in_use = true; #endif }; diff --git a/include/libtorrent/kademlia/put_data.hpp b/include/libtorrent/kademlia/put_data.hpp index f46fe8149..3ff3d07a3 100644 --- a/include/libtorrent/kademlia/put_data.hpp +++ b/include/libtorrent/kademlia/put_data.hpp @@ -71,9 +71,9 @@ protected: struct put_data_observer : traversal_observer { put_data_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id, std::string const& token) - : traversal_observer(algorithm, ep, id) + : traversal_observer(std::move(algorithm), ep, id) , m_token(token) { } diff --git a/include/libtorrent/kademlia/rpc_manager.hpp b/include/libtorrent/kademlia/rpc_manager.hpp index 912194bda..f85d6729a 100644 --- a/include/libtorrent/kademlia/rpc_manager.hpp +++ b/include/libtorrent/kademlia/rpc_manager.hpp @@ -57,8 +57,9 @@ struct socket_manager; struct TORRENT_EXTRA_EXPORT null_observer : observer { - null_observer(std::shared_ptr const& a - , udp::endpoint const& ep, node_id const& id): observer(a, ep, id) {} + null_observer(std::shared_ptr a + , udp::endpoint const& ep, node_id const& id) + : observer(std::move(a), ep, id) {} void reply(msg const&) override { flags |= flag_done; } }; diff --git a/include/libtorrent/kademlia/sample_infohashes.hpp b/include/libtorrent/kademlia/sample_infohashes.hpp index c91adb673..1fbd863cc 100644 --- a/include/libtorrent/kademlia/sample_infohashes.hpp +++ b/include/libtorrent/kademlia/sample_infohashes.hpp @@ -68,7 +68,7 @@ class sample_infohashes_observer final : public traversal_observer { public: - sample_infohashes_observer(std::shared_ptr const& algorithm + sample_infohashes_observer(std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id); void reply(msg const&) override; diff --git a/include/libtorrent/kademlia/traversal_algorithm.hpp b/include/libtorrent/kademlia/traversal_algorithm.hpp index 00a32213f..d45193890 100644 --- a/include/libtorrent/kademlia/traversal_algorithm.hpp +++ b/include/libtorrent/kademlia/traversal_algorithm.hpp @@ -156,9 +156,9 @@ void look_for_nodes(char const* nodes_key, udp const& protocol struct traversal_observer : observer { traversal_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : observer(algorithm, ep, id) + : observer(std::move(algorithm), ep, id) {} // parses out "nodes" and keeps traversing diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 82ecf2bed..3ff8b7e8a 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -463,7 +463,7 @@ void node::direct_request(udp::endpoint const& ep, entry& e // not really a traversal auto algo = std::make_shared(*this, node_id(), f); - auto o = m_rpc.allocate_observer(algo, ep, node_id()); + auto o = m_rpc.allocate_observer(std::move(algo), ep, node_id()); if (!o) return; #if TORRENT_USE_ASSERTS o->m_in_constructor = false; @@ -603,9 +603,9 @@ void node::sample_infohashes(udp::endpoint const& ep, sha1_hash const& target struct ping_observer : observer { ping_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : observer(algorithm, ep, id) + : observer(std::move(algorithm), ep, id) {} // parses out "nodes" @@ -675,8 +675,8 @@ void node::send_single_refresh(udp::endpoint const& ep, int const bucket target |= m_id & mask; // create a dummy traversal_algorithm - auto const algo = std::make_shared(*this, node_id()); - auto o = m_rpc.allocate_observer(algo, ep, id); + auto algo = std::make_shared(*this, node_id()); + auto o = m_rpc.allocate_observer(std::move(algo), ep, id); if (!o) return; #if TORRENT_USE_ASSERTS o->m_in_constructor = false; diff --git a/src/kademlia/sample_infohashes.cpp b/src/kademlia/sample_infohashes.cpp index c07322ec0..58a36aee2 100644 --- a/src/kademlia/sample_infohashes.cpp +++ b/src/kademlia/sample_infohashes.cpp @@ -62,9 +62,9 @@ void sample_infohashes::got_samples(time_duration interval } sample_infohashes_observer::sample_infohashes_observer( - std::shared_ptr const& algorithm + std::shared_ptr algorithm , udp::endpoint const& ep, node_id const& id) - : traversal_observer(algorithm, ep, id) {} + : traversal_observer(std::move(algorithm), ep, id) {} void sample_infohashes_observer::reply(msg const& m) { diff --git a/test/test_dht.cpp b/test/test_dht.cpp index 89348f37d..20f64687f 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -3372,7 +3372,7 @@ TORRENT_TEST(rpc_invalid_error_msg) g_sent_packets.clear(); auto algo = std::make_shared(node, node_id()); - auto o = rpc.allocate_observer(algo, source, node_id()); + auto o = rpc.allocate_observer(std::move(algo), source, node_id()); #if TORRENT_USE_ASSERTS o->m_in_constructor = false; #endif