Skip to content

Commit ae95b77

Browse files
authored
perf: avoid intermediate BAL account allocation (bluealloy#3775)
1 parent 84fe60c commit ae95b77

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

crates/state/src/bal/alloy.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ impl Bal {
2323
/// length or unsupported version.
2424
#[inline]
2525
pub fn try_from_alloy(alloy_bal: AlloyBal) -> Result<Self, BytecodeDecodeError> {
26-
let accounts = AddressIndexMap::from_iter(
27-
alloy_bal
28-
.into_iter()
29-
.map(AccountBal::try_from_alloy)
30-
.collect::<Result<Vec<_>, _>>()?,
31-
);
26+
let mut accounts =
27+
AddressIndexMap::with_capacity_and_hasher(alloy_bal.len(), Default::default());
28+
for alloy_account in alloy_bal {
29+
let (address, account_bal) = AccountBal::try_from_alloy(alloy_account)?;
30+
accounts.insert(address, account_bal);
31+
}
3232

3333
Ok(Self { accounts })
3434
}
@@ -45,12 +45,12 @@ impl Bal {
4545
pub fn clone_from_alloy(
4646
alloy_bal: &[AlloyAccountChanges],
4747
) -> Result<Self, BytecodeDecodeError> {
48-
let accounts = AddressIndexMap::from_iter(
49-
alloy_bal
50-
.iter()
51-
.map(AccountBal::clone_from_alloy)
52-
.collect::<Result<Vec<_>, _>>()?,
53-
);
48+
let mut accounts =
49+
AddressIndexMap::with_capacity_and_hasher(alloy_bal.len(), Default::default());
50+
for alloy_account in alloy_bal {
51+
let (address, account_bal) = AccountBal::clone_from_alloy(alloy_account)?;
52+
accounts.insert(address, account_bal);
53+
}
5454

5555
Ok(Self { accounts })
5656
}

0 commit comments

Comments
 (0)