Skip to content

Commit 4a567cd

Browse files
committed
refactor: extract PeerList
Extract a type for a collection of peers. The performance adter the exatract is similar: ```output Requests out: 415067.21/second Responses in: 369397.08/second - Connect responses: 183049.81 - Announce responses: 182717.15 - Scrape responses: 3630.12 - Error responses: 0.00 Peers per announce response: 0.00 Announce responses per info hash: - p10: 1 - p25: 1 - p50: 1 - p75: 1 - p90: 2 - p95: 3 - p99: 104 - p99.9: 297 - p100: 375 ```
1 parent 52b7e3a commit 4a567cd

File tree

10 files changed

+88
-54
lines changed

10 files changed

+88
-54
lines changed

packages/torrent-repository/src/entry/mod.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,72 @@ pub trait EntryAsync {
8282
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
8383
pub struct Torrent {
8484
/// The swarm: a network of peers that are all trying to download the torrent associated to this entry
85-
// #[serde(skip)]
86-
pub(crate) peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
85+
pub(crate) peers: PeerList,
8786
/// The number of peers that have ever completed downloading the torrent associated to this entry
8887
pub(crate) downloaded: u32,
8988
}
89+
90+
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
91+
pub struct PeerList {
92+
peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
93+
}
94+
95+
impl PeerList {
96+
fn len(&self) -> usize {
97+
self.peers.len()
98+
}
99+
100+
fn is_empty(&self) -> bool {
101+
self.peers.is_empty()
102+
}
103+
104+
fn insert(&mut self, key: peer::Id, value: Arc<peer::Peer>) -> Option<Arc<peer::Peer>> {
105+
self.peers.insert(key, value)
106+
}
107+
108+
fn remove(&mut self, key: &peer::Id) -> Option<Arc<peer::Peer>> {
109+
self.peers.remove(key)
110+
}
111+
112+
fn retain<F>(&mut self, f: F)
113+
where
114+
F: FnMut(&peer::Id, &mut Arc<peer::Peer>) -> bool,
115+
{
116+
self.peers.retain(f);
117+
}
118+
119+
fn seeders_and_leechers(&self) -> (usize, usize) {
120+
let seeders = self.peers.values().filter(|peer| peer.is_seeder()).count();
121+
let leechers = self.len() - seeders;
122+
123+
(seeders, leechers)
124+
}
125+
126+
fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
127+
match limit {
128+
Some(limit) => self.peers.values().take(limit).cloned().collect(),
129+
None => self.peers.values().cloned().collect(),
130+
}
131+
}
132+
133+
fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
134+
match limit {
135+
Some(limit) => self
136+
.peers
137+
.values()
138+
// Take peers which are not the client peer
139+
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
140+
// Limit the number of peers on the result
141+
.take(limit)
142+
.cloned()
143+
.collect(),
144+
None => self
145+
.peers
146+
.values()
147+
// Take peers which are not the client peer
148+
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
149+
.cloned()
150+
.collect(),
151+
}
152+
}
153+
}

packages/torrent-repository/src/entry/single.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ use crate::EntrySingle;
1313
impl Entry for EntrySingle {
1414
#[allow(clippy::cast_possible_truncation)]
1515
fn get_swarm_metadata(&self) -> SwarmMetadata {
16-
let complete: u32 = self.peers.values().filter(|peer| peer.is_seeder()).count() as u32;
17-
let incomplete: u32 = self.peers.len() as u32 - complete;
16+
let (seeders, leechers) = self.peers.seeders_and_leechers();
1817

1918
SwarmMetadata {
2019
downloaded: self.downloaded,
21-
complete,
22-
incomplete,
20+
complete: seeders as u32,
21+
incomplete: leechers as u32,
2322
}
2423
}
2524

@@ -42,32 +41,13 @@ impl Entry for EntrySingle {
4241
fn get_peers_len(&self) -> usize {
4342
self.peers.len()
4443
}
44+
4545
fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
46-
match limit {
47-
Some(limit) => self.peers.values().take(limit).cloned().collect(),
48-
None => self.peers.values().cloned().collect(),
49-
}
46+
self.peers.get_peers(limit)
5047
}
5148

5249
fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
53-
match limit {
54-
Some(limit) => self
55-
.peers
56-
.values()
57-
// Take peers which are not the client peer
58-
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
59-
// Limit the number of peers on the result
60-
.take(limit)
61-
.cloned()
62-
.collect(),
63-
None => self
64-
.peers
65-
.values()
66-
// Take peers which are not the client peer
67-
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
68-
.cloned()
69-
.collect(),
70-
}
50+
self.peers.get_peers_for_client(client, limit)
7151
}
7252

7353
fn upsert_peer(&mut self, peer: &peer::Peer) -> bool {

packages/torrent-repository/src/repository/dash_map_mutex_std.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::BTreeMap;
21
use std::sync::Arc;
32

43
use dashmap::DashMap;
@@ -10,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
109
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1110

1211
use super::Repository;
13-
use crate::entry::{Entry, EntrySync};
12+
use crate::entry::{Entry, EntrySync, PeerList};
1413
use crate::{EntryMutexStd, EntrySingle};
1514

1615
#[derive(Default, Debug)]
@@ -82,7 +81,7 @@ where
8281

8382
let entry = EntryMutexStd::new(
8483
EntrySingle {
85-
peers: BTreeMap::default(),
84+
peers: PeerList::default(),
8685
downloaded: *completed,
8786
}
8887
.into(),

packages/torrent-repository/src/repository/rw_lock_std.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::BTreeMap;
2-
31
use torrust_tracker_configuration::TrackerPolicy;
42
use torrust_tracker_primitives::info_hash::InfoHash;
53
use torrust_tracker_primitives::pagination::Pagination;
@@ -8,7 +6,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
86
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
97

108
use super::Repository;
11-
use crate::entry::Entry;
9+
use crate::entry::{Entry, PeerList};
1210
use crate::{EntrySingle, TorrentsRwLockStd};
1311

1412
#[derive(Default, Debug)]
@@ -102,7 +100,7 @@ where
102100
}
103101

104102
let entry = EntrySingle {
105-
peers: BTreeMap::default(),
103+
peers: PeerList::default(),
106104
downloaded: *downloaded,
107105
};
108106

packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::BTreeMap;
21
use std::sync::Arc;
32

43
use torrust_tracker_configuration::TrackerPolicy;
@@ -9,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
98
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
109

1110
use super::Repository;
12-
use crate::entry::{Entry, EntrySync};
11+
use crate::entry::{Entry, EntrySync, PeerList};
1312
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd};
1413

1514
impl TorrentsRwLockStdMutexStd {
@@ -97,7 +96,7 @@ where
9796

9897
let entry = EntryMutexStd::new(
9998
EntrySingle {
100-
peers: BTreeMap::default(),
99+
peers: PeerList::default(),
101100
downloaded: *completed,
102101
}
103102
.into(),

packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::BTreeMap;
21
use std::iter::zip;
32
use std::pin::Pin;
43
use std::sync::Arc;
@@ -13,7 +12,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
1312
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1413

1514
use super::RepositoryAsync;
16-
use crate::entry::{Entry, EntryAsync};
15+
use crate::entry::{Entry, EntryAsync, PeerList};
1716
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio};
1817

1918
impl TorrentsRwLockStdMutexTokio {
@@ -106,7 +105,7 @@ where
106105

107106
let entry = EntryMutexTokio::new(
108107
EntrySingle {
109-
peers: BTreeMap::default(),
108+
peers: PeerList::default(),
110109
downloaded: *completed,
111110
}
112111
.into(),

packages/torrent-repository/src/repository/rw_lock_tokio.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::BTreeMap;
2-
31
use torrust_tracker_configuration::TrackerPolicy;
42
use torrust_tracker_primitives::info_hash::InfoHash;
53
use torrust_tracker_primitives::pagination::Pagination;
@@ -8,7 +6,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
86
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
97

108
use super::RepositoryAsync;
11-
use crate::entry::Entry;
9+
use crate::entry::{Entry, PeerList};
1210
use crate::{EntrySingle, TorrentsRwLockTokio};
1311

1412
#[derive(Default, Debug)]
@@ -106,7 +104,7 @@ where
106104
}
107105

108106
let entry = EntrySingle {
109-
peers: BTreeMap::default(),
107+
peers: PeerList::default(),
110108
downloaded: *completed,
111109
};
112110

packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::BTreeMap;
21
use std::sync::Arc;
32

43
use torrust_tracker_configuration::TrackerPolicy;
@@ -9,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
98
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
109

1110
use super::RepositoryAsync;
12-
use crate::entry::{Entry, EntrySync};
11+
use crate::entry::{Entry, EntrySync, PeerList};
1312
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockTokioMutexStd};
1413

1514
impl TorrentsRwLockTokioMutexStd {
@@ -97,7 +96,7 @@ where
9796

9897
let entry = EntryMutexStd::new(
9998
EntrySingle {
100-
peers: BTreeMap::default(),
99+
peers: PeerList::default(),
101100
downloaded: *completed,
102101
}
103102
.into(),

packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::BTreeMap;
21
use std::sync::Arc;
32

43
use torrust_tracker_configuration::TrackerPolicy;
@@ -9,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
98
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
109

1110
use super::RepositoryAsync;
12-
use crate::entry::{Entry, EntryAsync};
11+
use crate::entry::{Entry, EntryAsync, PeerList};
1312
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockTokioMutexTokio};
1413

1514
impl TorrentsRwLockTokioMutexTokio {
@@ -100,7 +99,7 @@ where
10099

101100
let entry = EntryMutexTokio::new(
102101
EntrySingle {
103-
peers: BTreeMap::default(),
102+
peers: PeerList::default(),
104103
downloaded: *completed,
105104
}
106105
.into(),

packages/torrent-repository/src/repository/skip_map_mutex_std.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::BTreeMap;
21
use std::sync::Arc;
32

43
use crossbeam_skiplist::SkipMap;
@@ -10,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
109
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1110

1211
use super::Repository;
13-
use crate::entry::{Entry, EntrySync};
12+
use crate::entry::{Entry, EntrySync, PeerList};
1413
use crate::{EntryMutexStd, EntrySingle};
1514

1615
#[derive(Default, Debug)]
@@ -76,7 +75,7 @@ where
7675

7776
let entry = EntryMutexStd::new(
7877
EntrySingle {
79-
peers: BTreeMap::default(),
78+
peers: PeerList::default(),
8079
downloaded: *completed,
8180
}
8281
.into(),

0 commit comments

Comments
 (0)