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.
This commit is contained in:
Lijun Wang 2022-03-17 13:31:11 -07:00 committed by GitHub
parent 6b0d34d70d
commit 8b230b86cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 15 deletions

View File

@ -65,21 +65,21 @@ impl TpuConnection for QuicTpuConnection {
&self.client.addr
}
fn send_wire_transaction(&self, data: Vec<u8>) -> 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<Transaction>) -> 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::<Vec<_>>();
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)),
)
});

View File

@ -606,7 +606,7 @@ impl<C: 'static + TpuConnection> AsyncClient for ThinClient<C> {
}
fn async_send_batch(&self, transactions: Vec<Transaction>) -> TransportResult<()> {
self.tpu_connection().send_batch(transactions)
self.tpu_connection().send_batch(&transactions)
}
fn async_send_message<T: Signers>(

View File

@ -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<u8>) -> TransportResult<()>;
fn send_wire_transaction(&self, data: &[u8]) -> TransportResult<()>;
fn send_batch(&self, transactions: Vec<Transaction>) -> TransportResult<()>;
fn send_batch(&self, transactions: &[Transaction]) -> TransportResult<()>;
}

View File

@ -24,17 +24,17 @@ impl TpuConnection for UdpTpuConnection {
&self.addr
}
fn send_wire_transaction(&self, data: Vec<u8>) -> 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<Transaction>) -> 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(())