Emplacing wherever possible and other misc enhancements (#2498)

* Emplacing wherever possible and other misc enhancements

- Using emplace/emplace_back where possible. Had to make explicit constructors for [tcp_]endpoint_attempt and channel wrappers
- Looping with range for-loops and using const refs
- Avoiding one hash and lookup in ::reachout
- Using insert/emplace to insert multiple elements in some places, which is known to be faster for many elements

* One case where emplace is incorrect

* Keep using push/insert when the element is a smart pointer, as there is no difference

* Use const ref in constructors
This commit is contained in:
Guilherme Lawless 2020-01-20 13:16:39 +00:00 committed by GitHub
commit d5ab41e397
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 59 additions and 50 deletions

View file

@ -213,7 +213,7 @@ std::shared_ptr<nano::stat_entry> nano::stat::get_entry_impl (uint32_t key, size
auto entry = entries.find (key); auto entry = entries.find (key);
if (entry == entries.end ()) if (entry == entries.end ())
{ {
res = entries.insert (std::make_pair (key, std::make_shared<nano::stat_entry> (capacity, interval))).first->second; res = entries.emplace (key, std::make_shared<nano::stat_entry> (capacity, interval)).first->second;
} }
else else
{ {

View file

@ -111,7 +111,7 @@ io_guard (boost::asio::make_work_guard (io_ctx_a))
nano::thread_attributes::set (attrs); nano::thread_attributes::set (attrs);
for (auto i (0u); i < service_threads_a; ++i) for (auto i (0u); i < service_threads_a; ++i)
{ {
threads.push_back (boost::thread (attrs, [&io_ctx_a]() { threads.emplace_back (attrs, [&io_ctx_a]() {
nano::thread_role::set (nano::thread_role::name::io); nano::thread_role::set (nano::thread_role::name::io);
try try
{ {
@ -135,7 +135,7 @@ io_guard (boost::asio::make_work_guard (io_ctx_a))
throw; throw;
#endif #endif
} }
})); });
} }
} }

View file

@ -50,12 +50,11 @@ opencl (opencl_a)
} }
for (auto i (0u); i < count; ++i) for (auto i (0u); i < count; ++i)
{ {
auto thread (boost::thread (attrs, [this, i]() { threads.emplace_back (attrs, [this, i]() {
nano::thread_role::set (nano::thread_role::name::work); nano::thread_role::set (nano::thread_role::name::work);
nano::work_thread_reprioritize (); nano::work_thread_reprioritize ();
loop (i); loop (i);
})); });
threads.push_back (std::move (thread));
} }
} }
@ -204,7 +203,7 @@ void nano::work_pool::generate (nano::root const & root_a, std::function<void(bo
{ {
{ {
nano::lock_guard<std::mutex> lock (mutex); nano::lock_guard<std::mutex> lock (mutex);
pending.push_back ({ root_a, callback_a, difficulty_a }); pending.emplace_back (root_a, callback_a, difficulty_a);
} }
producer_condition.notify_all (); producer_condition.notify_all ();
} }

View file

@ -542,7 +542,7 @@ bool nano::active_transactions::add (std::shared_ptr<nano::block> block_a, bool
error = nano::work_validate (*block_a, &difficulty); error = nano::work_validate (*block_a, &difficulty);
release_assert (!error); release_assert (!error);
roots.get<tag_root> ().emplace (nano::conflict_info{ root, difficulty, difficulty, election }); roots.get<tag_root> ().emplace (nano::conflict_info{ root, difficulty, difficulty, election });
blocks.insert (std::make_pair (hash, election)); blocks.emplace (hash, election);
adjust_difficulty (hash); adjust_difficulty (hash);
election->insert_inactive_votes_cache (hash); election->insert_inactive_votes_cache (hash);
} }
@ -882,7 +882,7 @@ bool nano::active_transactions::publish (std::shared_ptr<nano::block> block_a)
result = election->publish (block_a); result = election->publish (block_a);
if (!result && !election->confirmed) if (!result && !election->confirmed)
{ {
blocks.insert (std::make_pair (block_a->hash (), election)); blocks.emplace (block_a->hash (), election);
} }
} }
return result; return result;

View file

@ -18,8 +18,8 @@ skip_delay (skip_delay_a),
confirmed (false), confirmed (false),
stopped (false) stopped (false)
{ {
last_votes.insert (std::make_pair (node.network_params.random.not_an_account, nano::vote_info{ std::chrono::steady_clock::now (), 0, block_a->hash () })); last_votes.emplace (node.network_params.random.not_an_account, nano::vote_info{ std::chrono::steady_clock::now (), 0, block_a->hash () });
blocks.insert (std::make_pair (block_a->hash (), block_a)); blocks.emplace (block_a->hash (), block_a);
update_dependent (); update_dependent ();
} }
@ -92,7 +92,7 @@ nano::tally_t nano::election::tally ()
auto block (blocks.find (item.first)); auto block (blocks.find (item.first));
if (block != blocks.end ()) if (block != blocks.end ())
{ {
result.insert (std::make_pair (item.second, block->second)); result.emplace (item.second, block->second);
} }
} }
return result; return result;
@ -217,7 +217,7 @@ bool nano::election::publish (std::shared_ptr<nano::block> block_a)
{ {
if (blocks.find (block_a->hash ()) == blocks.end ()) if (blocks.find (block_a->hash ()) == blocks.end ())
{ {
blocks.insert (std::make_pair (block_a->hash (), block_a)); blocks.emplace (block_a->hash (), block_a);
insert_inactive_votes_cache (block_a->hash ()); insert_inactive_votes_cache (block_a->hash ());
confirm_if_quorum (); confirm_if_quorum ();
node.network.flood_block (block_a, false); node.network.flood_block (block_a, false);

View file

@ -324,7 +324,7 @@ void nano::mdb_store::upgrade_v3_to_v4 (nano::write_transaction const & transact
{ {
nano::block_hash const & hash (i->first); nano::block_hash const & hash (i->first);
nano::pending_info_v3 const & info (i->second); nano::pending_info_v3 const & info (i->second);
items.push (std::make_pair (nano::pending_key (info.destination, hash), nano::pending_info_v14 (info.source, info.amount, nano::epoch::epoch_0))); items.emplace (nano::pending_key (info.destination, hash), nano::pending_info_v14 (info.source, info.amount, nano::epoch::epoch_0));
} }
mdb_drop (env.tx (transaction_a), pending_v0, 0); mdb_drop (env.tx (transaction_a), pending_v0, 0);
while (!items.empty ()) while (!items.empty ())

View file

@ -22,7 +22,7 @@ disconnect_observer ([]() {})
nano::thread_attributes::set (attrs); nano::thread_attributes::set (attrs);
for (size_t i = 0; i < node.config.network_threads; ++i) for (size_t i = 0; i < node.config.network_threads; ++i)
{ {
packet_processing_threads.push_back (boost::thread (attrs, [this]() { packet_processing_threads.emplace_back (attrs, [this]() {
nano::thread_role::set (nano::thread_role::name::packet_processing); nano::thread_role::set (nano::thread_role::name::packet_processing);
try try
{ {
@ -52,7 +52,7 @@ disconnect_observer ([]() {})
{ {
this->node.logger.try_log ("Exiting packet processing thread"); this->node.logger.try_log ("Exiting packet processing thread");
} }
})); });
} }
} }
@ -265,10 +265,7 @@ void nano::network::broadcast_confirm_req (std::shared_ptr<nano::block> block_a)
// broadcast request to all peers (with max limit 2 * sqrt (peers count)) // broadcast request to all peers (with max limit 2 * sqrt (peers count))
auto peers (node.network.list (std::min (static_cast<size_t> (100), 2 * node.network.size_sqrt ()))); auto peers (node.network.list (std::min (static_cast<size_t> (100), 2 * node.network.size_sqrt ())));
list->clear (); list->clear ();
for (auto & peer : peers) list->insert (list->end (), peers.begin (), peers.end ());
{
list->push_back (peer);
}
} }
/* /*

View file

@ -642,7 +642,7 @@ nano::error nano::node_config::deserialize_json (bool & upgraded_a, nano::jsonco
if (!result) if (!result)
{ {
auto address (entry.substr (0, port_position)); auto address (entry.substr (0, port_position));
this->work_peers.push_back (std::make_pair (address, port)); this->work_peers.emplace_back (address, port);
} }
} }
}); });

View file

@ -79,10 +79,7 @@ std::vector<nano::account> nano::online_reps::list ()
{ {
std::vector<nano::account> result; std::vector<nano::account> result;
nano::lock_guard<std::mutex> lock (mutex); nano::lock_guard<std::mutex> lock (mutex);
for (auto & i : reps) result.insert (result.end (), reps.begin (), reps.end ());
{
result.push_back (i);
}
return result; return result;
} }

View file

@ -183,7 +183,7 @@ void nano::rep_crawler::response (std::shared_ptr<nano::transport::channel> & ch
{ {
if (active.count (*i) != 0) if (active.count (*i) != 0)
{ {
responses.push_back (std::make_pair (channel_a, vote_a)); responses.emplace_back (channel_a, vote_a);
break; break;
} }
} }
@ -230,13 +230,13 @@ void nano::rep_crawler::cleanup_reps ()
{ {
// Check known rep channels // Check known rep channels
nano::lock_guard<std::mutex> lock (probable_reps_mutex); nano::lock_guard<std::mutex> lock (probable_reps_mutex);
for (auto i (probable_reps.get<tag_last_request> ().begin ()), n (probable_reps.get<tag_last_request> ().end ()); i != n; ++i) for (auto const & rep : probable_reps.get<tag_last_request> ())
{ {
channels.push_back (i->channel); channels.push_back (rep.channel);
} }
} }
// Remove reps with inactive channels // Remove reps with inactive channels
for (auto i : channels) for (auto const & i : channels)
{ {
bool equal (false); bool equal (false);
if (i->get_type () == nano::transport::transport_type::tcp) if (i->get_type () == nano::transport::transport_type::tcp)
@ -306,7 +306,7 @@ std::vector<std::shared_ptr<nano::transport::channel>> nano::rep_crawler::repres
{ {
std::vector<std::shared_ptr<nano::transport::channel>> result; std::vector<std::shared_ptr<nano::transport::channel>> result;
auto reps (representatives (count_a)); auto reps (representatives (count_a));
for (auto rep : reps) for (auto const & rep : reps)
{ {
result.push_back (rep.channel); result.push_back (rep.channel);
} }

View file

@ -118,7 +118,7 @@ bool nano::transport::tcp_channels::insert (std::shared_ptr<nano::transport::cha
{ {
channels.get<node_id_tag> ().erase (node_id); channels.get<node_id_tag> ().erase (node_id);
} }
channels.get<endpoint_tag> ().insert ({ channel_a, socket_a, bootstrap_server_a }); channels.get<endpoint_tag> ().emplace (channel_a, socket_a, bootstrap_server_a);
error = false; error = false;
lock.unlock (); lock.unlock ();
node.network.channel_observer (channel_a); node.network.channel_observer (channel_a);
@ -361,9 +361,8 @@ bool nano::transport::tcp_channels::reachout (nano::endpoint const & endpoint_a)
// Don't keepalive to nodes that already sent us something // Don't keepalive to nodes that already sent us something
error |= find_channel (tcp_endpoint) != nullptr; error |= find_channel (tcp_endpoint) != nullptr;
nano::lock_guard<std::mutex> lock (mutex); nano::lock_guard<std::mutex> lock (mutex);
auto existing (attempts.find (tcp_endpoint)); auto inserted (attempts.emplace (tcp_endpoint));
error |= existing != attempts.end (); error |= !inserted.second;
attempts.insert ({ tcp_endpoint, std::chrono::steady_clock::now () });
} }
return error; return error;
} }
@ -450,9 +449,9 @@ void nano::transport::tcp_channels::ongoing_keepalive ()
void nano::transport::tcp_channels::list (std::deque<std::shared_ptr<nano::transport::channel>> & deque_a) void nano::transport::tcp_channels::list (std::deque<std::shared_ptr<nano::transport::channel>> & deque_a)
{ {
nano::lock_guard<std::mutex> lock (mutex); nano::lock_guard<std::mutex> lock (mutex);
for (auto i (channels.begin ()), j (channels.end ()); i != j; ++i) for (auto const & channel : channels.get<random_access_tag> ())
{ {
deque_a.push_back (i->channel); deque_a.push_back (channel.channel);
} }
} }

View file

@ -137,6 +137,10 @@ namespace transport
std::shared_ptr<nano::transport::channel_tcp> channel; std::shared_ptr<nano::transport::channel_tcp> channel;
std::shared_ptr<nano::socket> socket; std::shared_ptr<nano::socket> socket;
std::shared_ptr<nano::bootstrap_server> response_server; std::shared_ptr<nano::bootstrap_server> response_server;
channel_tcp_wrapper (std::shared_ptr<nano::transport::channel_tcp> const & channel_a, std::shared_ptr<nano::socket> const & socket_a, std::shared_ptr<nano::bootstrap_server> const & server_a) :
channel (channel_a), socket (socket_a), response_server (server_a)
{
}
nano::tcp_endpoint endpoint () const nano::tcp_endpoint endpoint () const
{ {
return channel->get_tcp_endpoint (); return channel->get_tcp_endpoint ();
@ -164,7 +168,12 @@ namespace transport
{ {
public: public:
nano::tcp_endpoint endpoint; nano::tcp_endpoint endpoint;
std::chrono::steady_clock::time_point last_attempt; std::chrono::steady_clock::time_point last_attempt{ std::chrono::steady_clock::now () };
explicit tcp_endpoint_attempt (nano::tcp_endpoint const & endpoint_a) :
endpoint (endpoint_a)
{
}
}; };
mutable std::mutex mutex; mutable std::mutex mutex;
// clang-format off // clang-format off

View file

@ -108,7 +108,7 @@ std::shared_ptr<nano::transport::channel_udp> nano::transport::udp_channels::ins
else else
{ {
result = std::make_shared<nano::transport::channel_udp> (*this, endpoint_a, network_version_a); result = std::make_shared<nano::transport::channel_udp> (*this, endpoint_a, network_version_a);
channels.get<endpoint_tag> ().insert ({ result }); channels.get<endpoint_tag> ().insert (result);
lock.unlock (); lock.unlock ();
node.network.channel_observer (result); node.network.channel_observer (result);
} }
@ -598,9 +598,8 @@ bool nano::transport::udp_channels::reachout (nano::endpoint const & endpoint_a)
// Don't keepalive to nodes that already sent us something // Don't keepalive to nodes that already sent us something
error |= channel (endpoint_l) != nullptr; error |= channel (endpoint_l) != nullptr;
nano::lock_guard<std::mutex> lock (mutex); nano::lock_guard<std::mutex> lock (mutex);
auto existing (attempts.find (endpoint_l)); auto inserted (attempts.emplace (endpoint_l));
error |= existing != attempts.end (); error |= !inserted.second;
attempts.insert ({ endpoint_l, std::chrono::steady_clock::now () });
} }
return error; return error;
} }
@ -660,9 +659,9 @@ void nano::transport::udp_channels::ongoing_keepalive ()
void nano::transport::udp_channels::list (std::deque<std::shared_ptr<nano::transport::channel>> & deque_a) void nano::transport::udp_channels::list (std::deque<std::shared_ptr<nano::transport::channel>> & deque_a)
{ {
nano::lock_guard<std::mutex> lock (mutex); nano::lock_guard<std::mutex> lock (mutex);
for (auto i (channels.begin ()), j (channels.end ()); i != j; ++i) for (auto const & channel : channels.get<random_access_tag> ())
{ {
deque_a.push_back (i->channel); deque_a.push_back (channel.channel);
} }
} }

View file

@ -118,6 +118,10 @@ namespace transport
{ {
public: public:
std::shared_ptr<nano::transport::channel_udp> channel; std::shared_ptr<nano::transport::channel_udp> channel;
channel_udp_wrapper (std::shared_ptr<nano::transport::channel_udp> const & channel_a) :
channel (channel_a)
{
}
nano::endpoint endpoint () const nano::endpoint endpoint () const
{ {
return channel->get_endpoint (); return channel->get_endpoint ();
@ -143,7 +147,12 @@ namespace transport
{ {
public: public:
nano::endpoint endpoint; nano::endpoint endpoint;
std::chrono::steady_clock::time_point last_attempt; std::chrono::steady_clock::time_point last_attempt{ std::chrono::steady_clock::now () };
explicit endpoint_attempt (nano::endpoint const & endpoint_a) :
endpoint (endpoint_a)
{
}
}; };
mutable std::mutex mutex; mutable std::mutex mutex;
// clang-format off // clang-format off

View file

@ -142,7 +142,7 @@ void nano::vote_processor::vote (std::shared_ptr<nano::vote> vote_a, std::shared
} }
if (process) if (process)
{ {
votes.push_back (std::make_pair (vote_a, channel_a)); votes.emplace_back (vote_a, channel_a);
lock.unlock (); lock.unlock ();
condition.notify_all (); condition.notify_all ();
@ -169,7 +169,7 @@ void nano::vote_processor::verify_votes (std::deque<std::pair<std::shared_ptr<na
signatures.reserve (size); signatures.reserve (size);
std::vector<int> verifications; std::vector<int> verifications;
verifications.resize (size); verifications.resize (size);
for (auto & vote : votes_a) for (auto const & vote : votes_a)
{ {
hashes.push_back (vote.first->hash ()); hashes.push_back (vote.first->hash ());
messages.push_back (hashes.back ().bytes.data ()); messages.push_back (hashes.back ().bytes.data ());
@ -180,7 +180,7 @@ void nano::vote_processor::verify_votes (std::deque<std::pair<std::shared_ptr<na
checker.verify (check); checker.verify (check);
std::remove_reference_t<decltype (votes_a)> result; std::remove_reference_t<decltype (votes_a)> result;
auto i (0); auto i (0);
for (auto & vote : votes_a) for (auto const & vote : votes_a)
{ {
assert (verifications[i] == 1 || verifications[i] == 0); assert (verifications[i] == 1 || verifications[i] == 0);
if (verifications[i] == 1) if (verifications[i] == 1)

View file

@ -1743,7 +1743,7 @@ void nano::wallets::queue_wallet_action (nano::uint128_t const & amount_a, std::
{ {
{ {
nano::lock_guard<std::mutex> action_lock (action_mutex); nano::lock_guard<std::mutex> action_lock (action_mutex);
actions.insert (std::make_pair (amount_a, std::make_pair (wallet_a, std::move (action_a)))); actions.emplace (amount_a, std::make_pair (wallet_a, std::move (action_a)));
} }
condition.notify_all (); condition.notify_all ();
} }

View file

@ -430,7 +430,7 @@ void nano::websocket::session::handle_message (boost::property_tree::ptree const
} }
else else
{ {
subscriptions.insert (std::make_pair (topic_l, std::move (options_l))); subscriptions.emplace (topic_l, std::move (options_l));
ws_listener.get_logger ().always_log ("Websocket: new subscription to topic: ", from_topic (topic_l)); ws_listener.get_logger ().always_log ("Websocket: new subscription to topic: ", from_topic (topic_l));
ws_listener.increase_subscriber_count (topic_l); ws_listener.increase_subscriber_count (topic_l);
} }

View file

@ -28,7 +28,7 @@ int main (int argc, char ** argv)
auto wallet (system.nodes[i]->wallets.create (nano::random_wallet_id ())); auto wallet (system.nodes[i]->wallets.create (nano::random_wallet_id ()));
nano::keypair key; nano::keypair key;
wallet->insert_adhoc (key.prv); wallet->insert_adhoc (key.prv);
guis.push_back (std::unique_ptr<nano_qt::wallet> (new nano_qt::wallet (application, processor, *system.nodes[i], wallet, key.pub))); guis.push_back (std::make_unique<nano_qt::wallet> (application, processor, *system.nodes[i], wallet, key.pub));
client_tabs->addTab (guis.back ()->client_window, boost::str (boost::format ("Wallet %1%") % i).c_str ()); client_tabs->addTab (guis.back ()->client_window, boost::str (boost::format ("Wallet %1%") % i).c_str ());
} }
client_tabs->show (); client_tabs->show ();