From 1b9807af6d46874772ad61051a4b53421bcb1198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20W=C3=B3jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 13 Feb 2024 14:38:34 +0100 Subject: [PATCH] Use erase_if pattern (#4429) --- nano/node/bootstrap/bootstrap_connections.cpp | 16 ++++------------ nano/node/bootstrap_ascending/peer_scoring.cpp | 14 +++++++------- nano/node/election.cpp | 15 ++++----------- nano/node/telemetry.cpp | 2 +- nano/node/transport/tcp.cpp | 11 ++++------- nano/node/transport/tcp_server.cpp | 13 ++++--------- nano/node/vote_cache.cpp | 15 +++------------ 7 files changed, 27 insertions(+), 59 deletions(-) diff --git a/nano/node/bootstrap/bootstrap_connections.cpp b/nano/node/bootstrap/bootstrap_connections.cpp index ccb47c06..c843148d 100644 --- a/nano/node/bootstrap/bootstrap_connections.cpp +++ b/nano/node/bootstrap/bootstrap_connections.cpp @@ -443,18 +443,10 @@ void nano::bootstrap_connections::clear_pulls (uint64_t bootstrap_id_a) { { nano::lock_guard lock{ mutex }; - auto i (pulls.begin ()); - while (i != pulls.end ()) - { - if (i->bootstrap_id == bootstrap_id_a) - { - i = pulls.erase (i); - } - else - { - ++i; - } - } + + erase_if (pulls, [bootstrap_id_a] (auto const & pull) { + return pull.bootstrap_id == bootstrap_id_a; + }); } condition.notify_all (); } diff --git a/nano/node/bootstrap_ascending/peer_scoring.cpp b/nano/node/bootstrap_ascending/peer_scoring.cpp index faeb4fe3..e9b10596 100644 --- a/nano/node/bootstrap_ascending/peer_scoring.cpp +++ b/nano/node/bootstrap_ascending/peer_scoring.cpp @@ -82,18 +82,18 @@ std::size_t nano::bootstrap_ascending::peer_scoring::size () const void nano::bootstrap_ascending::peer_scoring::timeout () { auto & index = scoring.get (); - for (auto score = index.begin (), n = index.end (); score != n;) - { - if (auto channel = score->shared ()) + + erase_if (index, [] (auto const & score) { + if (auto channel = score.shared ()) { if (channel->alive ()) { - ++score; - continue; + return false; // Keep } } - score = index.erase (score); - } + return true; + }); + for (auto score = scoring.begin (), n = scoring.end (); score != n; ++score) { scoring.modify (score, [] (auto & score_a) { diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 56e86f1b..d967201d 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -647,17 +647,10 @@ void nano::election::remove_block (nano::block_hash const & hash_a) { if (auto existing = last_blocks.find (hash_a); existing != last_blocks.end ()) { - for (auto i (last_votes.begin ()); i != last_votes.end ();) - { - if (i->second.hash == hash_a) - { - i = last_votes.erase (i); - } - else - { - ++i; - } - } + erase_if (last_votes, [hash_a] (auto const & entry) { + return entry.second.hash == hash_a; + }); + node.network.publish_filter.clear (existing->second); last_blocks.erase (hash_a); } diff --git a/nano/node/telemetry.cpp b/nano/node/telemetry.cpp index 5260fbfa..e1fe6ece 100644 --- a/nano/node/telemetry.cpp +++ b/nano/node/telemetry.cpp @@ -242,7 +242,7 @@ void nano::telemetry::cleanup () { debug_assert (!mutex.try_lock ()); - nano::erase_if (telemetries, [this] (entry const & entry) { + erase_if (telemetries, [this] (entry const & entry) { // Remove if telemetry data is stale if (!check_timeout (entry)) { diff --git a/nano/node/transport/tcp.cpp b/nano/node/transport/tcp.cpp index c9a6c19d..a085dbfc 100644 --- a/nano/node/transport/tcp.cpp +++ b/nano/node/transport/tcp.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -463,13 +464,9 @@ void nano::transport::tcp_channels::purge (std::chrono::steady_clock::time_point nano::lock_guard lock{ mutex }; // Remove channels with dead underlying sockets - for (auto it = channels.begin (); it != channels.end (); ++it) - { - if (!it->socket->alive ()) - { - it = channels.erase (it); - } - } + erase_if (channels, [] (auto const & entry) { + return !entry.channel->alive (); + }); auto disconnect_cutoff (channels.get ().lower_bound (cutoff_a)); channels.get ().erase (channels.get ().begin (), disconnect_cutoff); diff --git a/nano/node/transport/tcp_server.cpp b/nano/node/transport/tcp_server.cpp index af163701..7cfee602 100644 --- a/nano/node/transport/tcp_server.cpp +++ b/nano/node/transport/tcp_server.cpp @@ -231,15 +231,10 @@ void nano::transport::tcp_listener::on_connection_requeue_delayed (std::function void nano::transport::tcp_listener::evict_dead_connections () { debug_assert (strand.running_in_this_thread ()); - for (auto it = connections_per_address.begin (); it != connections_per_address.end ();) - { - if (it->second.expired ()) - { - it = connections_per_address.erase (it); - continue; - } - ++it; - } + + erase_if (connections_per_address, [] (auto const & entry) { + return entry.second.expired (); + }); } void nano::transport::tcp_listener::accept_action (boost::system::error_code const & ec, std::shared_ptr const & socket_a) diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index 527ea0a0..78c6fa86 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -245,18 +245,9 @@ void nano::vote_cache::cleanup () auto const cutoff = std::chrono::steady_clock::now () - config.age_cutoff; - auto it = cache.begin (); - while (it != cache.end ()) - { - if (it->last_vote () < cutoff) - { - it = cache.erase (it); - } - else - { - ++it; - } - } + erase_if (cache, [cutoff] (auto const & entry) { + return entry.last_vote () < cutoff; + }); } std::unique_ptr nano::vote_cache::collect_container_info (const std::string & name) const