perf: reduce more allocs (#3176)

This commit is contained in:
Bjerg
2023-06-15 15:43:34 +02:00
committed by GitHub
parent 49adb8de0f
commit e8a5ba1d5f

View File

@ -215,22 +215,16 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> {
&self,
range: RangeInclusive<BlockNumber>,
) -> std::result::Result<BTreeMap<Address, BTreeSet<H256>>, TransactionError> {
Ok(self
.tx
self.tx
.cursor_read::<tables::StorageChangeSet>()?
.walk_range(BlockNumberAddress::range(range))?
.collect::<std::result::Result<Vec<_>, _>>()?
.into_iter()
// fold all storages and save its old state so we can remove it from HashedStorage
// it is needed as it is dup table.
.fold(
BTreeMap::new(),
|mut accounts: BTreeMap<Address, BTreeSet<H256>>,
(BlockNumberAddress((_, address)), storage_entry)| {
accounts.entry(address).or_default().insert(storage_entry.key);
accounts
},
))
.try_fold(BTreeMap::new(), |mut accounts: BTreeMap<Address, BTreeSet<H256>>, entry| {
let (BlockNumberAddress((_, address)), storage_entry) = entry?;
accounts.entry(address).or_default().insert(storage_entry.key);
Ok(accounts)
})
}
/// Get plainstate storages
@ -313,17 +307,14 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> {
&self,
range: RangeInclusive<BlockNumber>,
) -> std::result::Result<BTreeSet<Address>, TransactionError> {
Ok(self
.tx
.cursor_read::<tables::AccountChangeSet>()?
.walk_range(range)?
.collect::<std::result::Result<Vec<_>, _>>()?
.into_iter()
// fold all account to one set of changed accounts
.fold(BTreeSet::new(), |mut accounts: BTreeSet<Address>, (_, account_before)| {
self.tx.cursor_read::<tables::AccountChangeSet>()?.walk_range(range)?.try_fold(
BTreeSet::new(),
|mut accounts: BTreeSet<Address>, entry| {
let (_, account_before) = entry?;
accounts.insert(account_before.address);
accounts
}))
Ok(accounts)
},
)
}
/// Get plainstate account from iterator
@ -1099,8 +1090,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
id: StageId,
checkpoint: StageCheckpoint,
) -> std::result::Result<(), DatabaseError> {
self.tx.put::<tables::SyncStage>(id.to_string(), checkpoint)?;
Ok(())
self.tx.put::<tables::SyncStage>(id.to_string(), checkpoint)
}
/// Get stage checkpoint progress.
@ -1117,8 +1107,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> {
id: StageId,
checkpoint: Vec<u8>,
) -> std::result::Result<(), DatabaseError> {
self.tx.put::<tables::SyncStageProgress>(id.to_string(), checkpoint)?;
Ok(())
self.tx.put::<tables::SyncStageProgress>(id.to_string(), checkpoint)
}
/// Get lastest block number.
@ -1452,18 +1441,15 @@ impl<'this, TX: DbTx<'this>> AccountExtProvider for DatabaseProvider<'this, TX>
&self,
range: impl RangeBounds<BlockNumber>,
) -> Result<BTreeSet<Address>> {
Ok(self
.tx
self.tx
.cursor_read::<tables::AccountChangeSet>()?
.walk_range(range)?
.collect::<std::result::Result<Vec<_>, _>>()?
.into_iter()
// fold all account to one set of changed accounts
.fold(BTreeSet::new(), |mut accounts: BTreeSet<Address>, (_, account_before)| {
accounts.insert(account_before.address);
accounts
}))
.map(|entry| {
entry.map(|(_, account_before)| account_before.address).map_err(Into::into)
})
.collect()
}
fn basic_accounts(
&self,
iter: impl IntoIterator<Item = Address>,
@ -1647,9 +1633,9 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX
&self,
tx_hash: TxHash,
) -> Result<Option<(TransactionSigned, TransactionMeta)>> {
let mut transaction_cursor = self.tx.cursor_read::<tables::TransactionBlock>()?;
if let Some(transaction_id) = self.transaction_id(tx_hash)? {
if let Some(transaction) = self.transaction_by_id(transaction_id)? {
let mut transaction_cursor = self.tx.cursor_read::<tables::TransactionBlock>()?;
if let Some(block_number) =
transaction_cursor.seek(transaction_id).map(|b| b.map(|(_, bn)| bn))?
{
@ -1689,13 +1675,13 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX
&self,
id: BlockHashOrNumber,
) -> Result<Option<Vec<TransactionSigned>>> {
let mut tx_cursor = self.tx.cursor_read::<tables::Transactions>()?;
if let Some(block_number) = self.convert_hash_or_number(id)? {
if let Some(body) = self.block_body_indices(block_number)? {
let tx_range = body.tx_num_range();
return if tx_range.is_empty() {
Ok(Some(Vec::new()))
} else {
let mut tx_cursor = self.tx.cursor_read::<tables::Transactions>()?;
let transactions = tx_cursor
.walk_range(tx_range)?
.map(|result| result.map(|(_, tx)| tx.into()))
@ -1711,14 +1697,14 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX
&self,
range: impl RangeBounds<BlockNumber>,
) -> Result<Vec<Vec<TransactionSigned>>> {
let mut results = Vec::default();
let mut results = Vec::new();
let mut body_cursor = self.tx.cursor_read::<tables::BlockBodyIndices>()?;
let mut tx_cursor = self.tx.cursor_read::<tables::Transactions>()?;
for entry in body_cursor.walk_range(range)? {
let (_, body) = entry?;
let tx_num_range = body.tx_num_range();
if tx_num_range.is_empty() {
results.push(Vec::default());
results.push(Vec::new());
} else {
results.push(
tx_cursor
@ -1787,13 +1773,9 @@ impl<'this, TX: DbTx<'this>> WithdrawalsProvider for DatabaseProvider<'this, TX>
}
fn latest_withdrawal(&self) -> Result<Option<Withdrawal>> {
let latest_block_withdrawal = self.tx.cursor_read::<tables::BlockWithdrawals>()?.last();
latest_block_withdrawal
.map(|block_withdrawal_pair| {
block_withdrawal_pair
.and_then(|(_, block_withdrawal)| block_withdrawal.withdrawals.last().cloned())
})
.map_err(Into::into)
let latest_block_withdrawal = self.tx.cursor_read::<tables::BlockWithdrawals>()?.last()?;
Ok(latest_block_withdrawal
.and_then(|(_, mut block_withdrawal)| block_withdrawal.withdrawals.pop()))
}
}