Skip to content

Commit

Permalink
perf: cache analysed bytecodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw committed May 4, 2024
1 parent 337516a commit c10acf5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/eth/evm/revm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::sync::Arc;
use anyhow::anyhow;
use anyhow::Context;
use itertools::Itertools;
use revm::interpreter::analysis::to_analysed;
use revm::primitives::AccountInfo;
use revm::primitives::Address as RevmAddress;
use revm::primitives::Bytecode as RevmBytecode;
Expand Down Expand Up @@ -192,6 +193,9 @@ struct RevmSession {
/// Slots cached during account load.
account_slots_cache: HashMap<Address, HashMap<SlotIndex, Slot>>,

/// Analysed bytecodes cached.
bytecode_cache: HashMap<Address, RevmBytecode>,

/// Metrics collected during EVM execution.
metrics: ExecutionMetrics,
}
Expand All @@ -204,8 +208,9 @@ impl RevmSession {
config,
input: Default::default(),
storage_changes: Default::default(),
metrics: Default::default(),
account_slots_cache: Default::default(),
bytecode_cache: Default::default(),
metrics: Default::default(),
}
}

Expand Down Expand Up @@ -246,7 +251,19 @@ impl Database for RevmSession {
}

// early convert response because account will be moved
let revm_account: AccountInfo = (&account).into();
let mut revm_account: AccountInfo = (&account).into();
if let Some(raw_bytecode) = revm_account.code {
match self.bytecode_cache.get(&address) {
// bytecode cached, use it
Some(analysed_bytecode) => revm_account.code = Some(analysed_bytecode.clone()),
// bytecode not cached, analyse it and use it
None => {
let analysed_bytecode = to_analysed(raw_bytecode);
self.bytecode_cache.insert(address, analysed_bytecode.clone());
revm_account.code = Some(analysed_bytecode)
}
}
}

// track original value, except if ignored address
if not(account.address.is_ignored()) {
Expand Down
3 changes: 1 addition & 2 deletions src/eth/primitives/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::fmt::Display;
use std::ops::Deref;

use ethers_core::types::Bytes as EthersBytes;
use revm::interpreter::analysis::to_analysed;
use revm::primitives::Bytecode as RevmBytecode;
use revm::primitives::Bytes as RevmBytes;
use revm::primitives::Output as RevmOutput;
Expand Down Expand Up @@ -136,6 +135,6 @@ impl From<Bytes> for RevmBytes {

impl From<Bytes> for RevmBytecode {
fn from(value: Bytes) -> Self {
to_analysed(RevmBytecode::new_raw(value.0.into()))
RevmBytecode::new_raw(value.0.into())
}
}

0 comments on commit c10acf5

Please sign in to comment.