From 1ea683eb9d216baa8a17c0075f00e920389be22b Mon Sep 17 00:00:00 2001 From: Alex Pyattaev Date: Wed, 29 Jan 2025 10:47:16 +0000 Subject: [PATCH 1/2] reduce memory copying in transaction batches --- tpu-client-next/src/transaction_batch.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tpu-client-next/src/transaction_batch.rs b/tpu-client-next/src/transaction_batch.rs index a81461a9526efc..7fe4bf2aa6ecc7 100644 --- a/tpu-client-next/src/transaction_batch.rs +++ b/tpu-client-next/src/transaction_batch.rs @@ -1,19 +1,20 @@ //! This module holds [`TransactionBatch`] structure. -use solana_time_utils::timestamp; +use {solana_time_utils::timestamp, tokio_util::bytes::Bytes}; /// Batch of generated transactions timestamp is used to discard batches which /// are too old to have valid blockhash. #[derive(Clone, PartialEq)] pub struct TransactionBatch { wired_transactions: Vec, + // Time of creation of this batch, used for batch timeouts timestamp: u64, } -type WiredTransaction = Vec; +type WiredTransaction = Bytes; impl IntoIterator for TransactionBatch { - type Item = Vec; + type Item = Bytes; type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.wired_transactions.into_iter() @@ -21,9 +22,17 @@ impl IntoIterator for TransactionBatch { } impl TransactionBatch { - pub fn new(wired_transactions: Vec) -> Self { + pub fn new(wired_transactions: Vec) -> Self + where + T: AsRef<[u8]> + Send + 'static, + { + let txs = wired_transactions + .into_iter() + .map(|v| Bytes::from_owner(v)) + .collect(); + Self { - wired_transactions, + wired_transactions: txs, timestamp: timestamp(), } } From 53eca9829fadb048d9ea8cdce1dee155d6303bba Mon Sep 17 00:00:00 2001 From: Alex Pyattaev Date: Wed, 29 Jan 2025 16:20:35 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Better naming for intermediate variable Co-authored-by: kirill lykov --- tpu-client-next/src/transaction_batch.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tpu-client-next/src/transaction_batch.rs b/tpu-client-next/src/transaction_batch.rs index 7fe4bf2aa6ecc7..a10973bbbbf4f9 100644 --- a/tpu-client-next/src/transaction_batch.rs +++ b/tpu-client-next/src/transaction_batch.rs @@ -26,13 +26,13 @@ impl TransactionBatch { where T: AsRef<[u8]> + Send + 'static, { - let txs = wired_transactions + let wired_transactions = wired_transactions .into_iter() .map(|v| Bytes::from_owner(v)) .collect(); Self { - wired_transactions: txs, + wired_transactions, timestamp: timestamp(), } }