From 1a2432733e9e90687c628569fbaae039504bcdc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sun, 17 May 2020 21:22:32 +0200 Subject: [PATCH] aquatic_ws: in scrape handler, limit HashMap preallocation --- aquatic_ws/src/lib/handler.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aquatic_ws/src/lib/handler.rs b/aquatic_ws/src/lib/handler.rs index 15d0d93..82875c5 100644 --- a/aquatic_ws/src/lib/handler.rs +++ b/aquatic_ws/src/lib/handler.rs @@ -218,17 +218,17 @@ pub fn handle_scrape_requests( requests: Drain<(ConnectionMeta, ScrapeRequest)>, ){ messages_out.extend(requests.map(|(meta, request)| { - let num_info_hashes = request.info_hashes.len(); + let num_to_take = request.info_hashes.len().min( + config.network.max_scrape_torrents + ); let mut response = ScrapeResponse { - files: HashMap::with_capacity(num_info_hashes), + files: HashMap::with_capacity(num_to_take), }; - let max_torrents = config.network.max_scrape_torrents; - // If request.info_hashes is empty, don't return scrape for all // torrents, even though reference server does it. It is too expensive. - for info_hash in request.info_hashes.into_iter().take(max_torrents){ + for info_hash in request.info_hashes.into_iter().take(num_to_take){ if let Some(torrent_data) = torrents.get(&info_hash){ let stats = ScrapeStatistics { complete: torrent_data.num_seeders,