perf: reduce allocations in changeset helpers (#3169)

This commit is contained in:
Bjerg
2023-06-15 13:57:46 +02:00
committed by GitHub
parent 5bb090cd9f
commit 64392209e9

View File

@ -265,23 +265,22 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> {
&self, &self,
range: RangeInclusive<BlockNumber>, range: RangeInclusive<BlockNumber>,
) -> std::result::Result<BTreeMap<(Address, H256), Vec<u64>>, TransactionError> { ) -> std::result::Result<BTreeMap<(Address, H256), Vec<u64>>, TransactionError> {
let storage_changeset = self let mut changeset_cursor = self.tx.cursor_read::<tables::StorageChangeSet>()?;
.tx
.cursor_read::<tables::StorageChangeSet>()?
.walk_range(BlockNumberAddress::range(range))?
.collect::<std::result::Result<Vec<_>, _>>()?;
// fold all storages to one set of changes let storage_changeset_lists =
let storage_changeset_lists = storage_changeset.into_iter().fold( changeset_cursor.walk_range(BlockNumberAddress::range(range))?.try_fold(
BTreeMap::new(), BTreeMap::new(),
|mut storages: BTreeMap<(Address, H256), Vec<u64>>, (index, storage)| { |mut storages: BTreeMap<(Address, H256), Vec<u64>>,
storages entry|
.entry((index.address(), storage.key)) -> std::result::Result<_, TransactionError> {
.or_default() let (index, storage) = entry?;
.push(index.block_number()); storages
storages .entry((index.address(), storage.key))
}, .or_default()
); .push(index.block_number());
Ok(storages)
},
)?;
Ok(storage_changeset_lists) Ok(storage_changeset_lists)
} }
@ -293,22 +292,18 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> {
&self, &self,
range: RangeInclusive<BlockNumber>, range: RangeInclusive<BlockNumber>,
) -> std::result::Result<BTreeMap<Address, Vec<u64>>, TransactionError> { ) -> std::result::Result<BTreeMap<Address, Vec<u64>>, TransactionError> {
let account_changesets = self let mut changeset_cursor = self.tx.cursor_read::<tables::AccountChangeSet>()?;
.tx
.cursor_read::<tables::AccountChangeSet>()?
.walk_range(range)?
.collect::<std::result::Result<Vec<_>, _>>()?;
let account_transtions = account_changesets let account_transtions = changeset_cursor.walk_range(range)?.try_fold(
.into_iter() BTreeMap::new(),
// fold all account to one set of changed accounts |mut accounts: BTreeMap<Address, Vec<u64>>,
.fold( entry|
BTreeMap::new(), -> std::result::Result<_, TransactionError> {
|mut accounts: BTreeMap<Address, Vec<u64>>, (index, account)| { let (index, account) = entry?;
accounts.entry(account.address).or_default().push(index); accounts.entry(account.address).or_default().push(index);
accounts Ok(accounts)
}, },
); )?;
Ok(account_transtions) Ok(account_transtions)
} }