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
This commit is contained in:
hashmap 2020-04-18 15:07:26 +02:00 committed by GitHub
parent 0da88f7046
commit e64e90623b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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.