From e64e90623b1095d9e24213a96f3b4c47e766dc08 Mon Sep 17 00:00:00 2001
From: hashmap <hashmap@gmail.com>
Date: Sat, 18 Apr 2020 15:07:26 +0200
Subject: [PATCH] Reduce number of allocations in Headers message read (#3301)

We allocate 17 vectors (in the heap) per 512 headers, this PR reduces the number to 1 by resuing the buffer for headers and eliminating the need in the vector of indices
---
 p2p/src/protocol.rs | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/p2p/src/protocol.rs b/p2p/src/protocol.rs
index 0345e9e5d..b8cd75b58 100644
--- a/p2p/src/protocol.rs
+++ b/p2p/src/protocol.rs
@@ -229,16 +229,17 @@ impl MessageHandler for Protocol {
 				total_bytes_read += bytes_read;
 
 				// Read chunks of headers off the stream and pass them off to the adapter.
-				let chunk_size = 32;
-				for chunk in (0..count).collect::<Vec<_>>().chunks(chunk_size) {
-					let mut headers = vec![];
-					for _ in chunk {
-						let (header, bytes_read) =
-							msg.streaming_read::<core::UntrustedBlockHeader>()?;
-						headers.push(header.into());
-						total_bytes_read += bytes_read;
+				let chunk_size = 32u16;
+				let mut headers = Vec::with_capacity(chunk_size as usize);
+				for i in 1..=count {
+					let (header, bytes_read) =
+						msg.streaming_read::<core::UntrustedBlockHeader>()?;
+					headers.push(header.into());
+					total_bytes_read += bytes_read;
+					if i % chunk_size == 0 || i == count {
+						adapter.headers_received(&headers, &self.peer_info)?;
+						headers.clear();
 					}
-					adapter.headers_received(&headers, &self.peer_info)?;
 				}
 
 				// Now check we read the correct total number of bytes off the stream.