Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emplacing wherever possible and other misc enhancements #2498

Merged
merged 4 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ std::shared_ptr<nano::stat_entry> nano::stat::get_entry_impl (uint32_t key, size
auto entry = entries.find (key);
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
{
Expand Down
4 changes: 2 additions & 2 deletions nano/lib/threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ io_guard (boost::asio::make_work_guard (io_ctx_a))
nano::thread_attributes::set (attrs);
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);
try
{
Expand All @@ -135,7 +135,7 @@ io_guard (boost::asio::make_work_guard (io_ctx_a))
throw;
#endif
}
}));
});
}
}

Expand Down
7 changes: 3 additions & 4 deletions nano/lib/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ opencl (opencl_a)
}
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::work_thread_reprioritize ();
loop (i);
}));
threads.push_back (std::move (thread));
});
}
}

Expand Down Expand Up @@ -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);
pending.push_back ({ root_a, callback_a, difficulty_a });
pending.emplace_back (root_a, callback_a, difficulty_a);
}
producer_condition.notify_all ();
}
Expand Down
4 changes: 2 additions & 2 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ bool nano::active_transactions::add (std::shared_ptr<nano::block> block_a, bool
error = nano::work_validate (*block_a, &difficulty);
release_assert (!error);
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);
election->insert_inactive_votes_cache (hash);
}
Expand Down Expand Up @@ -882,7 +882,7 @@ bool nano::active_transactions::publish (std::shared_ptr<nano::block> block_a)
result = election->publish (block_a);
if (!result && !election->confirmed)
{
blocks.insert (std::make_pair (block_a->hash (), election));
blocks.emplace (block_a->hash (), election);
}
}
return result;
Expand Down
8 changes: 4 additions & 4 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ skip_delay (skip_delay_a),
confirmed (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 () }));
blocks.insert (std::make_pair (block_a->hash (), block_a));
last_votes.emplace (node.network_params.random.not_an_account, nano::vote_info{ std::chrono::steady_clock::now (), 0, block_a->hash () });
blocks.emplace (block_a->hash (), block_a);
update_dependent ();
}

Expand Down Expand Up @@ -92,7 +92,7 @@ nano::tally_t nano::election::tally ()
auto block (blocks.find (item.first));
if (block != blocks.end ())
{
result.insert (std::make_pair (item.second, block->second));
result.emplace (item.second, block->second);
}
}
return result;
Expand Down Expand Up @@ -217,7 +217,7 @@ bool nano::election::publish (std::shared_ptr<nano::block> block_a)
{
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 ());
confirm_if_quorum ();
node.network.flood_block (block_a, false);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::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);
while (!items.empty ())
Expand Down
9 changes: 3 additions & 6 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ disconnect_observer ([]() {})
nano::thread_attributes::set (attrs);
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);
try
{
Expand Down Expand Up @@ -52,7 +52,7 @@ disconnect_observer ([]() {})
{
this->node.logger.try_log ("Exiting packet processing thread");
}
}));
});
}
}

Expand Down Expand Up @@ -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))
auto peers (node.network.list (std::min (static_cast<size_t> (100), 2 * node.network.size_sqrt ())));
list->clear ();
for (auto & peer : peers)
{
list->push_back (peer);
}
list->insert (list->end (), peers.begin (), peers.end ());
}

/*
Expand Down
2 changes: 1 addition & 1 deletion nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ nano::error nano::node_config::deserialize_json (bool & upgraded_a, nano::jsonco
if (!result)
{
auto address (entry.substr (0, port_position));
this->work_peers.push_back (std::make_pair (address, port));
this->work_peers.emplace_back (address, port);
}
}
});
Expand Down
5 changes: 1 addition & 4 deletions nano/node/online_reps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ std::vector<nano::account> nano::online_reps::list ()
{
std::vector<nano::account> result;
nano::lock_guard<std::mutex> lock (mutex);
for (auto & i : reps)
{
result.push_back (i);
}
result.insert (result.end (), reps.begin (), reps.end ());
return result;
}

Expand Down
10 changes: 5 additions & 5 deletions nano/node/repcrawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void nano::rep_crawler::response (std::shared_ptr<nano::transport::channel> & ch
{
if (active.count (*i) != 0)
{
responses.push_back (std::make_pair (channel_a, vote_a));
responses.emplace_back (channel_a, vote_a);
break;
}
}
Expand Down Expand Up @@ -230,13 +230,13 @@ void nano::rep_crawler::cleanup_reps ()
{
// Check known rep channels
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
for (auto i : channels)
for (auto const & i : channels)
{
bool equal (false);
if (i->get_type () == nano::transport::transport_type::tcp)
Expand Down Expand Up @@ -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;
auto reps (representatives (count_a));
for (auto rep : reps)
for (auto const & rep : reps)
{
result.push_back (rep.channel);
}
Expand Down
11 changes: 5 additions & 6 deletions nano/node/transport/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<endpoint_tag> ().insert ({ channel_a, socket_a, bootstrap_server_a });
channels.get<endpoint_tag> ().emplace (channel_a, socket_a, bootstrap_server_a);
error = false;
lock.unlock ();
node.network.channel_observer (channel_a);
Expand Down Expand Up @@ -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
error |= find_channel (tcp_endpoint) != nullptr;
nano::lock_guard<std::mutex> lock (mutex);
auto existing (attempts.find (tcp_endpoint));
error |= existing != attempts.end ();
attempts.insert ({ tcp_endpoint, std::chrono::steady_clock::now () });
auto inserted (attempts.emplace (tcp_endpoint));
error |= !inserted.second;
}
return error;
}
Expand Down Expand Up @@ -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)
{
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);
}
}

Expand Down
11 changes: 10 additions & 1 deletion nano/node/transport/tcp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ namespace transport
std::shared_ptr<nano::transport::channel_tcp> channel;
std::shared_ptr<nano::socket> socket;
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
{
return channel->get_tcp_endpoint ();
Expand Down Expand Up @@ -164,7 +168,12 @@ namespace transport
{
public:
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;
// clang-format off
Expand Down
11 changes: 5 additions & 6 deletions nano/node/transport/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ std::shared_ptr<nano::transport::channel_udp> nano::transport::udp_channels::ins
else
{
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 ();
node.network.channel_observer (result);
}
Expand Down Expand Up @@ -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
error |= channel (endpoint_l) != nullptr;
nano::lock_guard<std::mutex> lock (mutex);
auto existing (attempts.find (endpoint_l));
error |= existing != attempts.end ();
attempts.insert ({ endpoint_l, std::chrono::steady_clock::now () });
auto inserted (attempts.emplace (endpoint_l));
error |= !inserted.second;
}
return error;
}
Expand Down Expand Up @@ -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)
{
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);
}
}

Expand Down
11 changes: 10 additions & 1 deletion nano/node/transport/udp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ namespace transport
{
public:
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
{
return channel->get_endpoint ();
Expand All @@ -143,7 +147,12 @@ namespace transport
{
public:
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;
// clang-format off
Expand Down
6 changes: 3 additions & 3 deletions nano/node/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void nano::vote_processor::vote (std::shared_ptr<nano::vote> vote_a, std::shared
}
if (process)
{
votes.push_back (std::make_pair (vote_a, channel_a));
votes.emplace_back (vote_a, channel_a);

lock.unlock ();
condition.notify_all ();
Expand All @@ -169,7 +169,7 @@ void nano::vote_processor::verify_votes (std::deque<std::pair<std::shared_ptr<na
signatures.reserve (size);
std::vector<int> verifications;
verifications.resize (size);
for (auto & vote : votes_a)
for (auto const & vote : votes_a)
{
hashes.push_back (vote.first->hash ());
messages.push_back (hashes.back ().bytes.data ());
Expand All @@ -180,7 +180,7 @@ void nano::vote_processor::verify_votes (std::deque<std::pair<std::shared_ptr<na
checker.verify (check);
std::remove_reference_t<decltype (votes_a)> result;
auto i (0);
for (auto & vote : votes_a)
for (auto const & vote : votes_a)
{
assert (verifications[i] == 1 || verifications[i] == 0);
if (verifications[i] == 1)
Expand Down
2 changes: 1 addition & 1 deletion nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 ();
}
Expand Down
2 changes: 1 addition & 1 deletion nano/node/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ void nano::websocket::session::handle_message (boost::property_tree::ptree const
}
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.increase_subscriber_count (topic_l);
}
Expand Down
2 changes: 1 addition & 1 deletion nano/qt_system/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main (int argc, char ** argv)
auto wallet (system.nodes[i]->wallets.create (nano::random_wallet_id ()));
nano::keypair key;
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->show ();
Expand Down