From 64392209e95189a98181fab68c86b241c63708ca Mon Sep 17 00:00:00 2001 From: Bjerg Date: Thu, 15 Jun 2023 13:57:46 +0200 Subject: [PATCH] perf: reduce allocations in changeset helpers (#3169) --- .../src/providers/database/provider.rs | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index a355a09e63..5a1cd977a1 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -265,23 +265,22 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> { &self, range: RangeInclusive, ) -> std::result::Result>, TransactionError> { - let storage_changeset = self - .tx - .cursor_read::()? - .walk_range(BlockNumberAddress::range(range))? - .collect::, _>>()?; + let mut changeset_cursor = self.tx.cursor_read::()?; - // fold all storages to one set of changes - let storage_changeset_lists = storage_changeset.into_iter().fold( - BTreeMap::new(), - |mut storages: BTreeMap<(Address, H256), Vec>, (index, storage)| { - storages - .entry((index.address(), storage.key)) - .or_default() - .push(index.block_number()); - storages - }, - ); + let storage_changeset_lists = + changeset_cursor.walk_range(BlockNumberAddress::range(range))?.try_fold( + BTreeMap::new(), + |mut storages: BTreeMap<(Address, H256), Vec>, + entry| + -> std::result::Result<_, TransactionError> { + let (index, storage) = entry?; + storages + .entry((index.address(), storage.key)) + .or_default() + .push(index.block_number()); + Ok(storages) + }, + )?; Ok(storage_changeset_lists) } @@ -293,22 +292,18 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> { &self, range: RangeInclusive, ) -> std::result::Result>, TransactionError> { - let account_changesets = self - .tx - .cursor_read::()? - .walk_range(range)? - .collect::, _>>()?; + let mut changeset_cursor = self.tx.cursor_read::()?; - let account_transtions = account_changesets - .into_iter() - // fold all account to one set of changed accounts - .fold( - BTreeMap::new(), - |mut accounts: BTreeMap>, (index, account)| { - accounts.entry(account.address).or_default().push(index); - accounts - }, - ); + let account_transtions = changeset_cursor.walk_range(range)?.try_fold( + BTreeMap::new(), + |mut accounts: BTreeMap>, + entry| + -> std::result::Result<_, TransactionError> { + let (index, account) = entry?; + accounts.entry(account.address).or_default().push(index); + Ok(accounts) + }, + )?; Ok(account_transtions) }