Repository: paritytech/substrate
File: client/network/sync/src/lib.rs (Rust)

Code before:
				.disconnect_peer(id, self.block_announce_protocol_name.clone());
			self.network_service.report_peer(id, repu);
		}
	}

	fn process_outbound_requests(&mut self) {
		for (id, request) in self.block_requests() {
			self.send_block_request(id, request);
		}

		if let Some((id, request)) = self.state_request() {
			self.send_state_request(id, request);
		}

		for (id, request) in self.justification_requests().collect::<Vec<_>>() {
			self.send_block_request(id, request);
		}

		if let Some((id, request)) = self.warp_sync_request() {
			self.send_warp_sync_request(id, request);
		}
	}

	fn poll_pending_responses(&mut self, cx: &mut std::task::Context) -> Poll<ImportResult<B>> {
		let ready_responses = self
			.pending_responses
			.iter_mut()
			.filter_map(|(_, future)| match future.poll_unpin(cx) {
				Poll::Pending => None,
				Poll::Ready(result) => Some(result),
			})
			.collect::<Vec<_>>();

		for (id, request, response) in ready_responses {
			self.pending_responses
				.remove(&id)
				.expect("Peer was just iterated and exist; qed");

			match response {
				Ok(Ok(resp)) => match request {
					PeerRequest::Block(req) => {
						let response = match Self::decode_block_response(&resp[..]) {
							Ok(proto) => proto,
							Err(e) => {
								debug!(
									target: "sync",
									"Failed to decode block response from peer {:?}: {:?}.",
									id,
									e
								);
								self.network_service.report_peer(id, rep::BAD_MESSAGE);

Review comment (performance):
Just a thought, this hashmap will be iterated quite often. Maybe it's worth looking at [indexmap](https://docs.rs/indexmap/latest/indexmap/)  for iteration performance? But I am not sure how many elements there are typically in `pending_responses`, so maybe this is a premature optimization.

Code after:
				.disconnect_peer(id, self.block_announce_protocol_name.clone());
			self.network_service.report_peer(id, repu);
		}
	}

	fn process_outbound_requests(&mut self) {
		for (id, request) in self.block_requests() {
			self.send_block_request(id, request);
		}

		if let Some((id, request)) = self.state_request() {
			self.send_state_request(id, request);
		}

		for (id, request) in self.justification_requests().collect::<Vec<_>>() {
			self.send_block_request(id, request);
		}

		if let Some((id, request)) = self.warp_sync_request() {
			self.send_warp_sync_request(id, request);
		}
	}

	fn poll_pending_responses(&mut self, cx: &mut std::task::Context) -> Poll<ImportResult<B>> {
		let ready_responses = self
			.pending_responses
			.values_mut()
			.filter_map(|future| match future.poll_unpin(cx) {
				Poll::Pending => None,
				Poll::Ready(result) => Some(result),
			})
			.collect::<Vec<_>>();

		for (id, request, response) in ready_responses {
			self.pending_responses
				.remove(&id)
				.expect("Logic error: peer id from pending response is missing in the map.");

			match response {
				Ok(Ok(resp)) => match request {
					PeerRequest::Block(req) => {
						let response = match Self::decode_block_response(&resp[..]) {
							Ok(proto) => proto,
							Err(e) => {
								debug!(
									target: "sync",
									"Failed to decode block response from peer {:?}: {:?}.",
									id,
									e
								);
								self.network_service.report_peer(id, rep::BAD_MESSAGE);