From: Matt Corallo Date: Mon, 11 Oct 2021 05:03:45 +0000 (+0000) Subject: Keep the same read buffer unless the last message was overly large X-Git-Tag: v0.0.107~31^2~6 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=eb17464e78ca3af0c80b5262b2d14fbebff40f10;p=rust-lightning Keep the same read buffer unless the last message was overly large This avoids repeatedly deallocating-allocating a Vec for the peer read buffer after every message/header. --- diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 0685785db..f771cf4ff 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -972,7 +972,7 @@ impl P if peer.pending_read_is_header { let msg_len = try_potential_handleerror!(peer, peer.channel_encryptor.decrypt_length_header(&peer.pending_read_buffer[..])); - peer.pending_read_buffer = Vec::with_capacity(msg_len as usize + 16); + if peer.pending_read_buffer.capacity() > 8192 { peer.pending_read_buffer = Vec::new(); } peer.pending_read_buffer.resize(msg_len as usize + 16, 0); if msg_len < 2 { // Need at least the message type tag return Err(PeerHandleError{ no_connection_possible: false }); @@ -984,7 +984,8 @@ impl P assert!(msg_data.len() >= 2); // Reset read buffer - peer.pending_read_buffer = [0; 18].to_vec(); + if peer.pending_read_buffer.capacity() > 8192 { peer.pending_read_buffer = Vec::new(); } + peer.pending_read_buffer.resize(18, 0); peer.pending_read_is_header = true; let mut reader = io::Cursor::new(&msg_data[..]);