From ebe612a5601e89a4865c5f721e7e5185e74092a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Thu, 14 Apr 2022 17:44:34 +0200 Subject: [PATCH] udp: TorrentMap cleaning: improve code, do less work --- aquatic_udp/src/workers/request.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/aquatic_udp/src/workers/request.rs b/aquatic_udp/src/workers/request.rs index 12eb6b9..d6041f9 100644 --- a/aquatic_udp/src/workers/request.rs +++ b/aquatic_udp/src/workers/request.rs @@ -98,9 +98,9 @@ impl TorrentMaps { let num_leechers = &mut torrent.num_leechers; torrent.peers.retain(|_, peer| { - let keep = peer.valid_until.0 > now; - - if !keep { + if peer.valid_until.0 > now { + true + } else { match peer.status { PeerStatus::Seeding => { *num_seeders -= 1; @@ -110,14 +110,18 @@ impl TorrentMaps { } _ => (), }; - } - keep + false + } }); - torrent.peers.shrink_to_fit(); + if torrent.peers.is_empty() { + false + } else { + torrent.peers.shrink_to_fit(); - !torrent.peers.is_empty() + true + } } }