Skip to content

Commit

Permalink
Use erase_if pattern (#4429)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev authored Feb 13, 2024
1 parent b824932 commit 1b9807a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 59 deletions.
16 changes: 4 additions & 12 deletions nano/node/bootstrap/bootstrap_connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,10 @@ void nano::bootstrap_connections::clear_pulls (uint64_t bootstrap_id_a)
{
{
nano::lock_guard<nano::mutex> 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 ();
}
Expand Down
14 changes: 7 additions & 7 deletions nano/node/bootstrap_ascending/peer_scoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tag_channel> ();
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) {
Expand Down
15 changes: 4 additions & 11 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion nano/node/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
11 changes: 4 additions & 7 deletions nano/node/transport/tcp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <nano/lib/config.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/utility.hpp>
#include <nano/node/node.hpp>
#include <nano/node/transport/message_deserializer.hpp>
#include <nano/node/transport/tcp.hpp>
Expand Down Expand Up @@ -463,13 +464,9 @@ void nano::transport::tcp_channels::purge (std::chrono::steady_clock::time_point
nano::lock_guard<nano::mutex> 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<last_packet_sent_tag> ().lower_bound (cutoff_a));
channels.get<last_packet_sent_tag> ().erase (channels.get<last_packet_sent_tag> ().begin (), disconnect_cutoff);
Expand Down
13 changes: 4 additions & 9 deletions nano/node/transport/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::transport::socket> const & socket_a)
Expand Down
15 changes: 3 additions & 12 deletions nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::container_info_component> nano::vote_cache::collect_container_info (const std::string & name) const
Expand Down

0 comments on commit 1b9807a

Please sign in to comment.