use HashMap::with_capacity (#11106)

This commit is contained in:
nk_ysg
2024-09-22 23:26:54 +08:00
committed by GitHub
parent 159bf2cf16
commit 52c72a3b1d
4 changed files with 9 additions and 6 deletions

View File

@@ -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());
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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);