perf: use Vec::with_capacity and reserve_exact (#11904)

This commit is contained in:
nk_ysg
2024-10-21 19:59:09 +08:00
committed by GitHub
parent aba4991d0a
commit f25cceb9f9
13 changed files with 22 additions and 17 deletions

View File

@ -305,7 +305,7 @@ pub fn calculate_reward_percentiles_for_block(
// the percentiles are monotonically increasing.
let mut tx_index = 0;
let mut cumulative_gas_used = transactions.first().map(|tx| tx.gas_used).unwrap_or_default();
let mut rewards_in_block = Vec::new();
let mut rewards_in_block = Vec::with_capacity(percentiles.len());
for percentile in percentiles {
// Empty blocks should return in a zero row
if transactions.is_empty() {

View File

@ -183,7 +183,7 @@ pub fn build_block<T: TransactionCompat>(
) -> Result<SimulatedBlock<Block<T::Transaction>>, EthApiError> {
let mut calls: Vec<SimCallResult> = Vec::with_capacity(results.len());
let mut senders = Vec::with_capacity(results.len());
let mut receipts = Vec::new();
let mut receipts = Vec::with_capacity(results.len());
let mut log_index = 0;
for (transaction_index, ((sender, result), tx)) in

View File

@ -421,8 +421,8 @@ mod tests {
let mut rng = generators::rng();
// Build mock data
let mut gas_used_ratios = Vec::new();
let mut base_fees_per_gas = Vec::new();
let mut gas_used_ratios = Vec::with_capacity(block_count as usize);
let mut base_fees_per_gas = Vec::with_capacity(block_count as usize);
let mut last_header = None;
let mut parent_hash = B256::default();
@ -444,8 +444,9 @@ mod tests {
last_header = Some(header.clone());
parent_hash = hash;
let mut transactions = vec![];
for _ in 0..100 {
const TOTAL_TRANSACTIONS: usize = 100;
let mut transactions = Vec::with_capacity(TOTAL_TRANSACTIONS);
for _ in 0..TOTAL_TRANSACTIONS {
let random_fee: u128 = rng.gen();
if let Some(base_fee_per_gas) = header.base_fee_per_gas {

View File

@ -40,7 +40,7 @@ impl DevSigner {
/// Generates provided number of random dev signers
/// which satisfy [`EthSigner`] trait
pub fn random_signers(num: u32) -> Vec<Box<dyn EthSigner + 'static>> {
let mut signers = Vec::new();
let mut signers = Vec::with_capacity(num as usize);
for _ in 0..num {
let sk = PrivateKeySigner::random_with(&mut rand::thread_rng());