From adfca10c3364f43afde110ed601230f464bd6697 Mon Sep 17 00:00:00 2001 From: Renato Dinhani <101204870+dinhani-cw@users.noreply.github.com> Date: Fri, 12 Apr 2024 20:50:54 -0300 Subject: [PATCH] feat: put everything related to slot cache behing a feature-flag (#589) --- src/eth/evm/revm.rs | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/eth/evm/revm.rs b/src/eth/evm/revm.rs index dcf1619c4..33b611dc7 100644 --- a/src/eth/evm/revm.rs +++ b/src/eth/evm/revm.rs @@ -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; @@ -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>, - /// Metrics collected during EVM execution. metrics: ExecutionMetrics, + + #[cfg(feature = "evm-slot-prefetch")] + /// Slots cached during account load. + account_slots_cache: std::collections::HashMap>, } impl RevmSession { @@ -174,8 +174,9 @@ 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(), } } @@ -183,8 +184,9 @@ impl RevmSession { 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(); } } @@ -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() + } } };