Skip to content

Commit

Permalink
use HashMap::with_capacity (#11106)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Sep 22, 2024
1 parent 159bf2c commit 52c72a3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,8 @@ mod tests {
assert_eq!(*tree.state.block_indices.blocks_to_chain(), block_to_chain);
}
if let Some(fork_to_child) = self.fork_to_child {
let mut x: HashMap<BlockHash, LinkedHashSet<BlockHash>> = HashMap::new();
let mut x: HashMap<BlockHash, LinkedHashSet<BlockHash>> =
HashMap::with_capacity(fork_to_child.len());
for (key, hash_set) in fork_to_child {
x.insert(key, hash_set.into_iter().collect());
}
Expand Down
7 changes: 4 additions & 3 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2736,11 +2736,12 @@ mod tests {
}

fn with_blocks(mut self, blocks: Vec<ExecutedBlock>) -> Self {
let mut blocks_by_hash = HashMap::new();
let mut blocks_by_hash = HashMap::with_capacity(blocks.len());
let mut blocks_by_number = BTreeMap::new();
let mut state_by_hash = HashMap::new();
let mut state_by_hash = HashMap::with_capacity(blocks.len());
let mut hash_by_number = BTreeMap::new();
let mut parent_to_child: HashMap<B256, HashSet<B256>> = HashMap::new();
let mut parent_to_child: HashMap<B256, HashSet<B256>> =
HashMap::with_capacity(blocks.len());
let mut parent_hash = B256::ZERO;

for block in &blocks {
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ mod tests {
let l1_block_contract_account =
Account { balance: U256::ZERO, bytecode_hash: None, nonce: 1 };

let mut l1_block_storage = HashMap::new();
let mut l1_block_storage = HashMap::with_capacity(4);
// base fee
l1_block_storage.insert(StorageKey::with_last_byte(1), StorageValue::from(1000000000));
// l1 fee overhead
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2844,7 +2844,8 @@ impl<TX: DbTxMut + DbTx, Spec: Send + Sync> HashingWriter for DatabaseProvider<T
hashed_storages.sort_by_key(|(ha, hk, _)| (*ha, *hk));

// Apply values to HashedState, and remove the account if it's None.
let mut hashed_storage_keys: HashMap<B256, BTreeSet<B256>> = HashMap::new();
let mut hashed_storage_keys: HashMap<B256, BTreeSet<B256>> =
HashMap::with_capacity(hashed_storages.len());
let mut hashed_storage = self.tx.cursor_dup_write::<tables::HashedStorages>()?;
for (hashed_address, key, value) in hashed_storages.into_iter().rev() {
hashed_storage_keys.entry(hashed_address).or_default().insert(key);
Expand Down

0 comments on commit 52c72a3

Please sign in to comment.