mirror of
https://github.com/mimblewimble/grin.git
synced 2025-04-22 18:31:15 +03:00
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
This commit is contained in:
parent
0da88f7046
commit
e64e90623b
1 changed files with 10 additions and 9 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue