Skip to content

Commit

Permalink
perf(AllTransactions-iter): do not clone all transactions by default (
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise authored Dec 6, 2024
1 parent e615010 commit c608679
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 14 additions & 3 deletions crates/transaction-pool/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,21 @@ where

/// Returns _all_ transactions in the pool.
pub fn pooled_transactions(&self) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
self.get_pool_data().all().transactions_iter().filter(|tx| tx.propagate).collect()
self.get_pool_data().all().transactions_iter().filter(|tx| tx.propagate).cloned().collect()
}

/// Returns only the first `max` transactions in the pool.
pub fn pooled_transactions_max(
&self,
max: usize,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
self.get_pool_data().all().transactions_iter().filter(|tx| tx.propagate).take(max).collect()
self.get_pool_data()
.all()
.transactions_iter()
.filter(|tx| tx.propagate)
.take(max)
.cloned()
.collect()
}

/// Converts the internally tracked transaction to the pooled format.
Expand Down Expand Up @@ -857,7 +863,12 @@ where
&self,
origin: TransactionOrigin,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
self.get_pool_data().all().transactions_iter().filter(|tx| tx.origin == origin).collect()
self.get_pool_data()
.all()
.transactions_iter()
.filter(|tx| tx.origin == origin)
.cloned()
.collect()
}

/// Returns all pending transactions filted by [`TransactionOrigin`]
Expand Down
6 changes: 3 additions & 3 deletions crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,11 +1095,11 @@ impl<T: PoolTransaction> AllTransactions<T> {
self.by_hash.keys().copied()
}

/// Returns an iterator over all _unique_ hashes in the pool
/// Returns an iterator over all transactions in the pool
pub(crate) fn transactions_iter(
&self,
) -> impl Iterator<Item = Arc<ValidPoolTransaction<T>>> + '_ {
self.by_hash.values().cloned()
) -> impl Iterator<Item = &Arc<ValidPoolTransaction<T>>> + '_ {
self.by_hash.values()
}

/// Returns if the transaction for the given hash is already included in this pool
Expand Down

0 comments on commit c608679

Please sign in to comment.