Skip to content

Commit

Permalink
feat: put everything related to slot cache behing a feature-flag (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Apr 12, 2024
1 parent 4056cc5 commit adfca10
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/eth/evm/revm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use revm::primitives::Address as RevmAddress;
use revm::primitives::Bytecode as RevmBytecode;
use revm::primitives::CreateScheme;
use revm::primitives::ExecutionResult as RevmExecutionResult;
use revm::primitives::HashMap;
use revm::primitives::ResultAndState as RevmResultAndState;
use revm::primitives::SpecId;
use revm::primitives::State as RevmState;
Expand Down Expand Up @@ -160,11 +159,12 @@ struct RevmSession {
/// Changes made to the storage during the execution of the transaction.
storage_changes: ExecutionChanges,

/// Slots cached during account load.
account_slots_cache: HashMap<Address, HashMap<SlotIndex, Slot>>,

/// Metrics collected during EVM execution.
metrics: ExecutionMetrics,

#[cfg(feature = "evm-slot-prefetch")]
/// Slots cached during account load.
account_slots_cache: std::collections::HashMap<Address, std::collections::HashMap<SlotIndex, Slot>>,
}

impl RevmSession {
Expand All @@ -174,17 +174,19 @@ impl RevmSession {
storage,
input: Default::default(),
storage_changes: Default::default(),
account_slots_cache: Default::default(),
metrics: Default::default(),
#[cfg(feature = "evm-slot-prefetch")]
account_slots_cache: Default::default(),
}
}

/// Resets the session to be used with a new transaction.
pub fn reset(&mut self, input: EvmInput) {
self.input = input;
self.storage_changes = Default::default();
self.account_slots_cache.clear();
self.metrics = Default::default();
#[cfg(feature = "evm-slot-prefetch")]
self.account_slots_cache.clear();
}
}

Expand Down Expand Up @@ -237,16 +239,23 @@ impl Database for RevmSession {
let address: Address = revm_address.into();
let index: SlotIndex = revm_index.into();

// load slot from storage
#[cfg(not(feature = "evm-slot-prefetch"))]
let slot = handle.block_on(self.storage.read_slot(&address, &index, &self.input.point_in_time))?;

// load slot from cache or storage
let cached_slot = self.account_slots_cache.get(&address).and_then(|slot_cache| slot_cache.get(&index));
let slot = match cached_slot {
// not found, query storage
None => handle.block_on(self.storage.read_slot(&address, &index, &self.input.point_in_time))?,

// cached
Some(slot) => {
self.metrics.slot_reads_cached += 1;
slot.clone()
#[cfg(feature = "evm-slot-prefetch")]
let slot = {
let cached_slot = self.account_slots_cache.get(&address).and_then(|slot_cache| slot_cache.get(&index));
match cached_slot {
// not found, query storage
None => handle.block_on(self.storage.read_slot(&address, &index, &self.input.point_in_time))?,

// cached
Some(slot) => {
self.metrics.slot_reads_cached += 1;
slot.clone()
}
}
};

Expand Down

0 comments on commit adfca10

Please sign in to comment.