Skip to content

Commit 0792ca2

Browse files
committed
Memory stack state optimizations
1 parent 02e0fe9 commit 0792ca2

1 file changed

Lines changed: 34 additions & 50 deletions

File tree

evm/src/executor/stack/memory.rs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -223,48 +223,42 @@ impl<'config> MemoryStackSubstate<'config> {
223223
Ok(())
224224
}
225225

226+
/// Get known account from the current state. If it's `None` just take a look
227+
/// recursively in the parent state.
228+
#[must_use]
226229
pub fn known_account(&self, address: H160) -> Option<&MemoryStackAccount> {
227-
self.accounts.get(&address).map_or_else(
228-
|| {
229-
self.parent
230-
.as_ref()
231-
.and_then(|parent| parent.known_account(address))
232-
},
233-
Some,
234-
)
230+
self.accounts.get(&address).or_else(|| {
231+
self.parent
232+
.as_ref()
233+
.and_then(|parent| parent.known_account(address))
234+
})
235235
}
236236

237+
/// Get known basic data from the current accounts state.
238+
/// If it's `None` just take a look.
237239
#[must_use]
238240
pub fn known_basic(&self, address: H160) -> Option<Basic> {
239241
self.known_account(address).map(|acc| acc.basic.clone())
240242
}
241243

244+
/// Get known code from the current accounts state.
245+
/// If it's `None` just take a look.
242246
#[must_use]
243247
pub fn known_code(&self, address: H160) -> Option<Vec<u8>> {
244248
self.known_account(address).and_then(|acc| acc.code.clone())
245249
}
246250

251+
/// Get known empty status of the account from the current accounts state.
252+
/// If it's `None` just take a look.
247253
#[must_use]
248254
pub fn known_empty(&self, address: H160) -> Option<bool> {
249-
if let Some(account) = self.known_account(address) {
250-
if account.basic.balance != U256_ZERO {
251-
return Some(false);
252-
}
253-
254-
if account.basic.nonce != U256_ZERO {
255-
return Some(false);
256-
}
257-
258-
if let Some(code) = &account.code {
259-
return Some(
260-
account.basic.balance == U256_ZERO
261-
&& account.basic.nonce == U256_ZERO
262-
&& code.is_empty(),
263-
);
255+
self.known_account(address).and_then(|account| {
256+
if account.basic.balance != U256_ZERO || account.basic.nonce != U256_ZERO {
257+
Some(false)
258+
} else {
259+
account.code.as_ref().map(Vec::is_empty)
264260
}
265-
}
266-
267-
None
261+
})
268262
}
269263

270264
#[must_use]
@@ -312,25 +306,18 @@ impl<'config> MemoryStackSubstate<'config> {
312306
}
313307

314308
fn recursive_is_cold<F: Fn(&Accessed) -> bool>(&self, f: &F) -> bool {
315-
let local_is_accessed = self.metadata.accessed().as_ref().is_some_and(f);
316-
if local_is_accessed {
317-
false
318-
} else {
319-
self.parent.as_ref().is_none_or(|p| p.recursive_is_cold(f))
320-
}
309+
!self.metadata.accessed().as_ref().is_some_and(f)
310+
&& self.parent.as_ref().is_none_or(|p| p.recursive_is_cold(f))
321311
}
322312

313+
/// Check if the account was deleted in the current substate or any of its parents.
323314
#[must_use]
324315
pub fn deleted(&self, address: H160) -> bool {
325-
if self.deletes.contains(&address) {
326-
return true;
327-
}
328-
329-
if let Some(parent) = self.parent.as_ref() {
330-
return parent.deleted(address);
331-
}
332-
333-
false
316+
self.deletes.contains(&address)
317+
|| self
318+
.parent
319+
.as_ref()
320+
.is_some_and(|parent| parent.deleted(address))
334321
}
335322

336323
#[allow(clippy::map_entry)]
@@ -403,17 +390,14 @@ impl<'config> MemoryStackSubstate<'config> {
403390
self.creates.insert(address);
404391
}
405392

393+
/// Check if the account was created in the current substate or any of its parents.
406394
#[must_use]
407395
pub fn is_created(&self, address: H160) -> bool {
408-
if self.creates.contains(&address) {
409-
return true;
410-
}
411-
412-
if let Some(parent) = self.parent.as_ref() {
413-
return parent.is_created(address);
414-
}
415-
416-
false
396+
self.creates.contains(&address)
397+
|| self
398+
.parent
399+
.as_ref()
400+
.is_some_and(|parent| parent.is_created(address))
417401
}
418402

419403
pub fn set_code<B: Backend>(&mut self, address: H160, code: Vec<u8>, backend: &B) {

0 commit comments

Comments
 (0)