reth icon indicating copy to clipboard operation
reth copied to clipboard

Allocate less memory when validating new mempool transactions

Open hai-rise opened this issue 11 months ago • 3 comments
trafficstars

Describe the feature

Not sure how accurate bytehound is but it reports that validating new mempool transactions allocates (and deallocates) ~100 GB of RAM for ~10M transactions in ~10 minutes (~15k new transactions per second).

Image

#50 [rise] <reth_transaction_pool::validate::task::TransactionValidationTaskExecutor<V> as reth_transaction_pool::validate::TransactionValidator>::validate_transaction::{{closure}}::{{closure}} [task.rs:186]
#51 [rise] <rise_mempool::validator::RiseTransactionValidator<Client,Tx> as reth_transaction_pool::validate::TransactionValidator>::validate_transaction::{{closure}} [validator.rs:81]
#52 [rise] <reth_optimism_node::txpool::OpTransactionValidator<Client,Tx> as reth_transaction_pool::validate::TransactionValidator>::validate_transaction::{{closure}} [txpool.rs:205]
#53 [rise] reth_optimism_node::txpool::OpTransactionValidator<Client,Tx>::validate_one [txpool.rs:125]
#54 [rise] reth_transaction_pool::validate::eth::EthTransactionValidator<Client,Tx>::validate_one [eth.rs:66]
#55 [rise] reth_transaction_pool::validate::eth::EthTransactionValidatorInner<Client,Tx>::validate_one [eth.rs:329]
#56 [rise] <reth_provider::providers::blockchain_provider::BlockchainProvider2<N> as reth_storage_api::state::StateProviderFactory>::latest [blockchain_provider.rs:532]
#57 [rise] reth_provider::providers::blockchain_provider::BlockchainProvider2<N>::block_state_provider [blockchain_provider.rs:136]
#58 [rise] reth_provider::providers::database::ProviderFactory<N>::history_by_block_hash [mod.rs:184]

This matches samply's complaint in https://github.com/paradigmxyz/reth/pull/12629 about copying the 32-byte tx-cost being a big overhead in the same code path.

This issue traces the whole transaction validation process and removes unneeded allocations. For instance, one of the earliest checks, ensure_max_init_code_size, copies the 21-byte TxKind (kind()) to check if the new transaction is a Create one. https://github.com/paradigmxyz/reth/blob/02824da4fcd0794d45442adbdbf6b953be04d4f3/crates/transaction-pool/src/traits.rs#L1067-L1071

This check alone would allocate 200MB to validate 10M transactions. Intuitively, this should be a single reference byte check of the &TxKind enum.

More to come 🙏.

Additional context

No response

hai-rise avatar Nov 25 '24 03:11 hai-rise