perf: query accounts with &Address to avoid copying address (#13554)

This commit is contained in:
Hai | RISE
2024-12-25 19:31:28 +07:00
committed by GitHub
parent 14c1c0be69
commit 031f430b8f
27 changed files with 74 additions and 71 deletions

View File

@ -748,7 +748,7 @@ impl<N: ProviderNodeTypes> ChangeSetReader for BlockchainProvider2<N> {
impl<N: ProviderNodeTypes> AccountReader for BlockchainProvider2<N> {
/// Get basic account information.
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
self.consistent_provider()?.basic_account(address)
}
}

View File

@ -68,9 +68,9 @@ impl<SP: StateProvider, EDP: ExecutionDataProvider> BlockHashReader
}
impl<SP: StateProvider, EDP: ExecutionDataProvider> AccountReader for BundleStateProvider<SP, EDP> {
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
if let Some(account) =
self.block_execution_data_provider.execution_outcome().account(&address)
self.block_execution_data_provider.execution_outcome().account(address)
{
Ok(account)
} else {

View File

@ -234,7 +234,7 @@ impl<N: ProviderNodeTypes> ConsistentProvider<N> {
let AccountBeforeTx { info: old_info, address } = account_before;
match state.entry(address) {
hash_map::Entry::Vacant(entry) => {
let new_info = state_provider.basic_account(address)?;
let new_info = state_provider.basic_account(&address)?;
entry.insert((old_info, new_info, HashMap::new()));
}
hash_map::Entry::Occupied(mut entry) => {
@ -252,7 +252,7 @@ impl<N: ProviderNodeTypes> ConsistentProvider<N> {
// get account state or insert from plain state.
let account_state = match state.entry(address) {
hash_map::Entry::Vacant(entry) => {
let present_info = state_provider.basic_account(address)?;
let present_info = state_provider.basic_account(&address)?;
entry.insert((present_info, present_info, HashMap::new()))
}
hash_map::Entry::Occupied(entry) => entry.into_mut(),
@ -1441,7 +1441,7 @@ impl<N: ProviderNodeTypes> ChangeSetReader for ConsistentProvider<N> {
impl<N: ProviderNodeTypes> AccountReader for ConsistentProvider<N> {
/// Get basic account information.
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
// use latest state provider
let state_provider = self.latest_ref()?;
state_provider.basic_account(address)

View File

@ -862,8 +862,8 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypes> DatabaseProvider<TX, N> {
}
impl<TX: DbTx, N: NodeTypes> AccountReader for DatabaseProvider<TX, N> {
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
Ok(self.tx.get::<tables::PlainAccountState>(address)?)
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
Ok(self.tx.get_by_encoded_key::<tables::PlainAccountState>(address)?)
}
}

View File

@ -949,7 +949,7 @@ impl<N: ProviderNodeTypes> ChangeSetReader for BlockchainProvider<N> {
impl<N: ProviderNodeTypes> AccountReader for BlockchainProvider<N> {
/// Get basic account information.
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
self.database.provider()?.basic_account(address)
}
}

View File

@ -249,21 +249,21 @@ impl<Provider: DBProvider + BlockNumReader + StateCommitmentProvider> AccountRea
for HistoricalStateProviderRef<'_, Provider>
{
/// Get basic account information.
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
match self.account_history_lookup(address)? {
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
match self.account_history_lookup(*address)? {
HistoryInfo::NotYetWritten => Ok(None),
HistoryInfo::InChangeset(changeset_block_number) => Ok(self
.tx()
.cursor_dup_read::<tables::AccountChangeSets>()?
.seek_by_key_subkey(changeset_block_number, address)?
.filter(|acc| acc.address == address)
.seek_by_key_subkey(changeset_block_number, *address)?
.filter(|acc| &acc.address == address)
.ok_or(ProviderError::AccountChangesetNotFound {
block_number: changeset_block_number,
address,
address: *address,
})?
.info),
HistoryInfo::InPlainState | HistoryInfo::MaybeInPlainState => {
Ok(self.tx().get::<tables::PlainAccountState>(address)?)
Ok(self.tx().get_by_encoded_key::<tables::PlainAccountState>(address)?)
}
}
}
@ -633,43 +633,46 @@ mod tests {
let db = factory.provider().unwrap();
// run
assert_eq!(HistoricalStateProviderRef::new(&db, 1).basic_account(ADDRESS), Ok(None));
assert_eq!(HistoricalStateProviderRef::new(&db, 1).basic_account(&ADDRESS), Ok(None));
assert_eq!(
HistoricalStateProviderRef::new(&db, 2).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 2).basic_account(&ADDRESS),
Ok(Some(acc_at3))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 3).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 3).basic_account(&ADDRESS),
Ok(Some(acc_at3))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 4).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 4).basic_account(&ADDRESS),
Ok(Some(acc_at7))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 7).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 7).basic_account(&ADDRESS),
Ok(Some(acc_at7))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 9).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 9).basic_account(&ADDRESS),
Ok(Some(acc_at10))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 10).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 10).basic_account(&ADDRESS),
Ok(Some(acc_at10))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 11).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 11).basic_account(&ADDRESS),
Ok(Some(acc_at15))
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 16).basic_account(ADDRESS),
HistoricalStateProviderRef::new(&db, 16).basic_account(&ADDRESS),
Ok(Some(acc_plain))
);
assert_eq!(HistoricalStateProviderRef::new(&db, 1).basic_account(HIGHER_ADDRESS), Ok(None));
assert_eq!(
HistoricalStateProviderRef::new(&db, 1000).basic_account(HIGHER_ADDRESS),
HistoricalStateProviderRef::new(&db, 1).basic_account(&HIGHER_ADDRESS),
Ok(None)
);
assert_eq!(
HistoricalStateProviderRef::new(&db, 1000).basic_account(&HIGHER_ADDRESS),
Ok(Some(higher_acc_plain))
);
}

View File

@ -43,8 +43,8 @@ impl<'b, Provider: DBProvider> LatestStateProviderRef<'b, Provider> {
impl<Provider: DBProvider> AccountReader for LatestStateProviderRef<'_, Provider> {
/// Get basic account information.
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
self.tx().get::<tables::PlainAccountState>(address).map_err(Into::into)
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
self.tx().get_by_encoded_key::<tables::PlainAccountState>(address).map_err(Into::into)
}
}

View File

@ -31,7 +31,7 @@ macro_rules! delegate_provider_impls {
$crate::providers::state::macros::delegate_impls_to_as_ref!(
for $target =>
AccountReader $(where [$($generics)*])? {
fn basic_account(&self, address: alloy_primitives::Address) -> reth_storage_errors::provider::ProviderResult<Option<reth_primitives::Account>>;
fn basic_account(&self, address: &alloy_primitives::Address) -> reth_storage_errors::provider::ProviderResult<Option<reth_primitives::Account>>;
}
BlockHashReader $(where [$($generics)*])? {
fn block_hash(&self, number: u64) -> reth_storage_errors::provider::ProviderResult<Option<alloy_primitives::B256>>;

View File

@ -577,8 +577,8 @@ impl BlockReaderIdExt for MockEthProvider {
}
impl AccountReader for MockEthProvider {
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
Ok(self.accounts.lock().get(&address).cloned().map(|a| a.account))
fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
Ok(self.accounts.lock().get(address).cloned().map(|a| a.account))
}
}

View File

@ -361,12 +361,12 @@ mod tests {
// Check plain state
assert_eq!(
provider.basic_account(address_a).expect("Could not read account state"),
provider.basic_account(&address_a).expect("Could not read account state"),
Some(reth_account_a),
"Account A state is wrong"
);
assert_eq!(
provider.basic_account(address_b).expect("Could not read account state"),
provider.basic_account(&address_b).expect("Could not read account state"),
Some(reth_account_b_changed),
"Account B state is wrong"
);
@ -422,7 +422,7 @@ mod tests {
// Check new plain state for account B
assert_eq!(
provider.basic_account(address_b).expect("Could not read account state"),
provider.basic_account(&address_b).expect("Could not read account state"),
None,
"Account B should be deleted"
);