Reduce number of allocations in Headers message read
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
@@ -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 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();
+ }
}
// Now check we read the correct total number of bytes off the stream.
