Skip to content

Commit

Permalink
Keep the same read buffer unless the last message was overly large
Browse files Browse the repository at this point in the history
This avoids repeatedly deallocating-allocating a Vec for the peer
read buffer after every message/header.
  • Loading branch information
TheBlueMatt committed Oct 11, 2021
1 parent 3fd2d26 commit 4fad4ef
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> 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 });
Expand All @@ -936,7 +936,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> 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[..]);
Expand Down

0 comments on commit 4fad4ef

Please sign in to comment.