perf: wrap tx with Arc to avoid deep cloning (#19350)

This commit is contained in:
Karl Yu
2025-10-29 16:30:29 +08:00
committed by GitHub
parent 10d9a7e3c6
commit 3827e5cb1d
4 changed files with 8 additions and 8 deletions

View File

@@ -318,7 +318,7 @@ where
let (execute_tx, execute_rx) = mpsc::channel();
self.executor.spawn_blocking(move || {
for tx in transactions {
let tx = tx.map(|tx| WithTxEnv { tx_env: tx.to_tx_env(), tx });
let tx = tx.map(|tx| WithTxEnv { tx_env: tx.to_tx_env(), tx: Arc::new(tx) });
// only send Ok(_) variants to prewarming task
if let Ok(tx) = &tx {
let _ = prewarm_tx.send(tx.clone());

View File

@@ -521,7 +521,7 @@ where
done_tx: Sender<()>,
) -> mpsc::Sender<IndexedTransaction<Tx>>
where
Tx: ExecutableTxFor<Evm> + Clone + Send + 'static,
Tx: ExecutableTxFor<Evm> + Send + 'static,
{
let (tx, rx) = mpsc::channel();
let ctx = self.clone();

View File

@@ -23,14 +23,14 @@ pub trait ExecutableTxIterator<Evm: ConfigureEvm>:
Iterator<Item = Result<Self::Tx, Self::Error>> + Send + 'static
{
/// The executable transaction type iterator yields.
type Tx: ExecutableTxFor<Evm> + Clone + Send + 'static;
type Tx: ExecutableTxFor<Evm> + Clone + Send + Sync + 'static;
/// Errors that may occur while recovering or decoding transactions.
type Error: core::error::Error + Send + Sync + 'static;
}
impl<Evm: ConfigureEvm, Tx, Err, T> ExecutableTxIterator<Evm> for T
where
Tx: ExecutableTxFor<Evm> + Clone + Send + 'static,
Tx: ExecutableTxFor<Evm> + Clone + Send + Sync + 'static,
Err: core::error::Error + Send + Sync + 'static,
T: Iterator<Item = Result<Tx, Err>> + Send + 'static,
{

View File

@@ -1,7 +1,7 @@
//! Traits for execution.
use crate::{ConfigureEvm, Database, OnStateHook, TxEnvFor};
use alloc::{boxed::Box, vec::Vec};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use alloy_consensus::{BlockHeader, Header};
use alloy_eips::eip2718::WithEncoded;
pub use alloy_evm::block::{BlockExecutor, BlockExecutorFactory};
@@ -447,7 +447,7 @@ impl<Executor: BlockExecutor> ExecutorTx<Executor> for Recovered<Executor::Trans
impl<T, Executor> ExecutorTx<Executor>
for WithTxEnv<<<Executor as BlockExecutor>::Evm as Evm>::Tx, T>
where
T: ExecutorTx<Executor>,
T: ExecutorTx<Executor> + Clone,
Executor: BlockExecutor,
<<Executor as BlockExecutor>::Evm as Evm>::Tx: Clone,
Self: RecoveredTx<Executor::Transaction>,
@@ -457,7 +457,7 @@ where
}
fn into_recovered(self) -> Recovered<Executor::Transaction> {
self.tx.into_recovered()
Arc::unwrap_or_clone(self.tx).into_recovered()
}
}
@@ -641,7 +641,7 @@ pub struct WithTxEnv<TxEnv, T> {
/// The transaction environment for EVM.
pub tx_env: TxEnv,
/// The recovered transaction.
pub tx: T,
pub tx: Arc<T>,
}
impl<TxEnv, Tx, T: RecoveredTx<Tx>> RecoveredTx<Tx> for WithTxEnv<TxEnv, T> {