Skip to content

Commit 632c586

Browse files
authored
perf(anvil): optimize internal data passing in log queries (#11896)
1 parent bfd5c44 commit 632c586

File tree

1 file changed

+8
-5
lines changed
  • crates/anvil/src/eth/backend/mem

1 file changed

+8
-5
lines changed

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ impl Backend {
20932093
hash: B256,
20942094
) -> Result<Vec<Log>, BlockchainError> {
20952095
if let Some(block) = self.blockchain.get_block_by_hash(&hash) {
2096-
return Ok(self.mined_logs_for_block(filter, block));
2096+
return Ok(self.mined_logs_for_block(filter, block, hash));
20972097
}
20982098

20992099
if let Some(fork) = self.get_fork() {
@@ -2104,9 +2104,8 @@ impl Backend {
21042104
}
21052105

21062106
/// Returns all `Log`s mined by the node that were emitted in the `block` and match the `Filter`
2107-
fn mined_logs_for_block(&self, filter: Filter, block: Block) -> Vec<Log> {
2107+
fn mined_logs_for_block(&self, filter: Filter, block: Block, block_hash: B256) -> Vec<Log> {
21082108
let mut all_logs = Vec::new();
2109-
let block_hash = block.header.hash_slow();
21102109
let mut block_log_index = 0u32;
21112110

21122111
let storage = self.blockchain.storage.read();
@@ -2167,8 +2166,12 @@ impl Backend {
21672166
}
21682167

21692168
for number in from..=to {
2170-
if let Some(block) = self.get_block(number) {
2171-
all_logs.extend(self.mined_logs_for_block(filter.clone(), block));
2169+
let storage = self.blockchain.storage.read();
2170+
if let Some(&block_hash) = storage.hashes.get(&number)
2171+
&& let Some(block) = storage.blocks.get(&block_hash).cloned()
2172+
{
2173+
drop(storage);
2174+
all_logs.extend(self.mined_logs_for_block(filter.clone(), block, block_hash));
21722175
}
21732176
}
21742177

0 commit comments

Comments
 (0)