Skip to content

Commit

Permalink
Reuse buffer in p2p handling (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta authored Jul 23, 2023
1 parent b041adf commit fae1e61
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use bitcoin::blockdata::block::Header as BlockHeader;
use bitcoin::consensus::Encodable;
use bitcoin::{
consensus::{
encode::{self, ReadExt, VarInt},
Expand Down Expand Up @@ -173,6 +174,7 @@ impl Connection {
);

let stream = Arc::clone(&conn);
let mut buffer = vec![];
crate::thread::spawn("p2p_send", move || loop {
use std::net::Shutdown;
let msg = match send_duration.observe_duration("wait", || tx_recv.recv()) {
Expand All @@ -193,8 +195,12 @@ impl Connection {
magic,
payload: msg,
};
buffer.clear();
raw_msg
.consensus_encode(&mut buffer)
.expect("in-memory writers don't error");
(&*stream)
.write_all(encode::serialize(&raw_msg).as_slice())
.write_all(buffer.as_slice())
.context("p2p failed to send")
})?;
});
Expand Down

0 comments on commit fae1e61

Please sign in to comment.