diff --git a/crates/rpc/rpc-eth-api/src/helpers/fee.rs b/crates/rpc/rpc-eth-api/src/helpers/fee.rs index 9f179aad342..acb2fd9419f 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/fee.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/fee.rs @@ -137,13 +137,18 @@ pub trait EthFees: let start_block = end_block_plus - block_count; // Collect base fees, gas usage ratios and (optionally) reward percentile data - let mut base_fee_per_gas: Vec = Vec::new(); - let mut gas_used_ratio: Vec = Vec::new(); + // Pre-allocate capacity: base_fee and blob_fee need +1 for the next block's fee + let mut base_fee_per_gas: Vec = Vec::with_capacity(block_count as usize + 1); + let mut gas_used_ratio: Vec = Vec::with_capacity(block_count as usize); - let mut base_fee_per_blob_gas: Vec = Vec::new(); - let mut blob_gas_used_ratio: Vec = Vec::new(); + let mut base_fee_per_blob_gas: Vec = Vec::with_capacity(block_count as usize + 1); + let mut blob_gas_used_ratio: Vec = Vec::with_capacity(block_count as usize); - let mut rewards: Vec> = Vec::new(); + let mut rewards: Vec> = if reward_percentiles.is_some() { + Vec::with_capacity(block_count as usize) + } else { + Vec::new() + }; // Check if the requested range is within the cache bounds let fee_entries = self.fee_history_cache().get_history(start_block, end_block).await;