From 8b230b86cc816378f307931a0b11851ea6897106 Mon Sep 17 00:00:00 2001 From: Lijun Wang <83639177+lijunwangs@users.noreply.github.com> Date: Thu, 17 Mar 2022 13:31:11 -0700 Subject: [PATCH] Use borrow instead of move in interfaces defined by TpuConnection (#23734) * Use borrow instead of move in interfaces defined by TpuConnection to avoid data copy * Removed a few more unnecessary whole array slicing. --- client/src/quic_client.rs | 12 ++++++------ client/src/thin_client.rs | 2 +- client/src/tpu_connection.rs | 6 +++--- client/src/udp_client.rs | 10 +++++----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/client/src/quic_client.rs b/client/src/quic_client.rs index 8abb8ba2fd..c5bfb4855b 100644 --- a/client/src/quic_client.rs +++ b/client/src/quic_client.rs @@ -65,21 +65,21 @@ impl TpuConnection for QuicTpuConnection { &self.client.addr } - fn send_wire_transaction(&self, data: Vec) -> TransportResult<()> { + fn send_wire_transaction(&self, data: &[u8]) -> TransportResult<()> { let _guard = self.client.runtime.enter(); - let send_buffer = self.client.send_buffer(&data[..]); + let send_buffer = self.client.send_buffer(data); self.client.runtime.block_on(send_buffer)?; Ok(()) } - fn send_batch(&self, transactions: Vec) -> TransportResult<()> { + fn send_batch(&self, transactions: &[Transaction]) -> TransportResult<()> { let buffers = transactions .into_par_iter() .map(|tx| bincode::serialize(&tx).expect("serialize Transaction in send_batch")) .collect::>(); let _guard = self.client.runtime.enter(); - let send_batch = self.client.send_batch(&buffers[..]); + let send_batch = self.client.send_batch(&buffers); self.client.runtime.block_on(send_batch)?; Ok(()) } @@ -182,7 +182,7 @@ impl QuicClient { if buffers.is_empty() { return Ok(()); } - let connection = self._send_buffer(&buffers[0][..]).await?; + let connection = self._send_buffer(&buffers[0]).await?; // Used to avoid dereferencing the Arc multiple times below // by just getting a reference to the NewConnection once @@ -196,7 +196,7 @@ impl QuicClient { join_all( buffs .into_iter() - .map(|buf| Self::_send_buffer_using_conn(&buf[..], connection_ref)), + .map(|buf| Self::_send_buffer_using_conn(buf, connection_ref)), ) }); diff --git a/client/src/thin_client.rs b/client/src/thin_client.rs index 0321f3f733..27b1536d0f 100644 --- a/client/src/thin_client.rs +++ b/client/src/thin_client.rs @@ -606,7 +606,7 @@ impl AsyncClient for ThinClient { } fn async_send_batch(&self, transactions: Vec) -> TransportResult<()> { - self.tpu_connection().send_batch(transactions) + self.tpu_connection().send_batch(&transactions) } fn async_send_message( diff --git a/client/src/tpu_connection.rs b/client/src/tpu_connection.rs index 4c9295b333..c9036c5d31 100644 --- a/client/src/tpu_connection.rs +++ b/client/src/tpu_connection.rs @@ -12,10 +12,10 @@ pub trait TpuConnection { fn send_transaction(&self, tx: &Transaction) -> TransportResult<()> { let data = bincode::serialize(tx).expect("serialize Transaction in send_transaction"); - self.send_wire_transaction(data) + self.send_wire_transaction(&data) } - fn send_wire_transaction(&self, data: Vec) -> TransportResult<()>; + fn send_wire_transaction(&self, data: &[u8]) -> TransportResult<()>; - fn send_batch(&self, transactions: Vec) -> TransportResult<()>; + fn send_batch(&self, transactions: &[Transaction]) -> TransportResult<()>; } diff --git a/client/src/udp_client.rs b/client/src/udp_client.rs index ef3ebeb45e..bf761eed99 100644 --- a/client/src/udp_client.rs +++ b/client/src/udp_client.rs @@ -24,17 +24,17 @@ impl TpuConnection for UdpTpuConnection { &self.addr } - fn send_wire_transaction(&self, data: Vec) -> TransportResult<()> { - self.socket.send_to(&data[..], self.addr)?; + fn send_wire_transaction(&self, data: &[u8]) -> TransportResult<()> { + self.socket.send_to(data, self.addr)?; Ok(()) } - fn send_batch(&self, transactions: Vec) -> TransportResult<()> { + fn send_batch(&self, transactions: &[Transaction]) -> TransportResult<()> { transactions - .into_iter() + .iter() .map(|tx| bincode::serialize(&tx).expect("serialize Transaction in send_batch")) .try_for_each(|buff| -> TransportResult<()> { - self.socket.send_to(&buff[..], self.addr)?; + self.socket.send_to(&buff, self.addr)?; Ok(()) })?; Ok(())