announce handler: keep mutable access to torrent map less time

This commit is contained in:
Joakim Frostegård 2020-04-08 13:32:18 +02:00
parent b40d205ccd
commit 910accad31
3 changed files with 29 additions and 28 deletions

View file

@ -17,9 +17,6 @@
* Performance * Performance
* cpu-target=native good? * cpu-target=native good?
* mialloc good? * mialloc good?
* TorrentMap: mutable access only to insert peer, then drop reference
and get read-only access to gather peers. This could speed up
multi-threaded performance a lot
* Use less bytes from PeerId for hashing? Would need to implement * Use less bytes from PeerId for hashing? Would need to implement
"faulty" PartialEq too (on PeerMapKey, which would be OK) "faulty" PartialEq too (on PeerMapKey, which would be OK)
* bittorrent_udp * bittorrent_udp

View file

@ -59,10 +59,6 @@ pub fn handle_announce_requests(
return None; return None;
} }
let mut torrent_data = state.torrents
.entry(request.info_hash)
.or_insert_with(|| TorrentData::default());
let peer_key = PeerMapKey { let peer_key = PeerMapKey {
ip: src.ip(), ip: src.ip(),
peer_id: request.peer_id, peer_id: request.peer_id,
@ -70,12 +66,25 @@ pub fn handle_announce_requests(
let peer = Peer::from_announce_and_ip(&request, src.ip()); let peer = Peer::from_announce_and_ip(&request, src.ip());
let peer_status = peer.status; let peer_status = peer.status;
let opt_removed_peer = if peer.status == PeerStatus::Stopped { let opt_removed_peer_status = {
torrent_data.peers.remove(&peer_key) let mut torrent_data = state.torrents
} else { .entry(request.info_hash)
torrent_data.peers.insert(peer_key, peer) .or_insert_with(|| TorrentData::default());
if peer_status == PeerStatus::Stopped {
torrent_data.peers.remove(&peer_key)
.map(|peer| peer.status)
} else {
torrent_data.peers.insert(peer_key, peer)
.map(|peer| peer.status)
}
}; };
let max_num_peers_to_take = (request.peers_wanted.0.max(0) as usize)
.min(config.max_response_peers);
let torrent_data = state.torrents.get(&request.info_hash).unwrap();
match peer_status { match peer_status {
PeerStatus::Leeching => { PeerStatus::Leeching => {
@ -87,21 +96,16 @@ pub fn handle_announce_requests(
PeerStatus::Stopped => {} PeerStatus::Stopped => {}
}; };
if let Some(removed_peer) = opt_removed_peer { match opt_removed_peer_status {
match removed_peer.status { Some(PeerStatus::Leeching) => {
PeerStatus::Leeching => { torrent_data.num_leechers.fetch_sub(1, Ordering::SeqCst);
torrent_data.num_leechers.fetch_sub(1, Ordering::SeqCst); },
}, Some(PeerStatus::Seeding) => {
PeerStatus::Seeding => { torrent_data.num_seeders.fetch_sub(1, Ordering::SeqCst);
torrent_data.num_seeders.fetch_sub(1, Ordering::SeqCst); },
}, _ => {}
PeerStatus::Stopped => {}
}
} }
let max_num_peers_to_take = (request.peers_wanted.0.max(0) as usize)
.min(config.max_response_peers);
let response_peers = extract_response_peers( let response_peers = extract_response_peers(
&mut rng, &mut rng,
&torrent_data.peers, &torrent_data.peers,

View file

@ -4,9 +4,9 @@
//! ``` //! ```
//! ## Average results over 10 rounds //! ## Average results over 10 rounds
//! //!
//! Connect handler: 2 351 042 requests/second, 425.85 ns/request //! Connect handler: 2 330 506 requests/second, 429.30 ns/request
//! Announce handler: 266 609 requests/second, 3754.52 ns/request //! Announce handler: 264 596 requests/second, 3782.88 ns/request
//! Scrape handler: 463 222 requests/second, 2161.85 ns/request //! Scrape handler: 461 530 requests/second, 2169.41 ns/request
//! ``` //! ```
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};