From e11e1f30fa306f48a1498dc2f025bfb7063289f6 Mon Sep 17 00:00:00 2001 From: kien-rise <157339831+kien-rise@users.noreply.github.com> Date: Tue, 28 Jan 2025 18:00:05 +0700 Subject: [PATCH] perf: remove empty HashMap instances from TrieUpdates and HashedPostState (#13976) --- crates/trie/common/src/updates.rs | 3 +++ crates/trie/trie/src/state.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/trie/common/src/updates.rs b/crates/trie/common/src/updates.rs index 99e2c908ca..c98d26f60c 100644 --- a/crates/trie/common/src/updates.rs +++ b/crates/trie/common/src/updates.rs @@ -76,6 +76,9 @@ impl TrieUpdates { hashed_address: B256, storage_updates: StorageTrieUpdates, ) { + if storage_updates.is_empty() { + return; + } let existing = self.storage_tries.insert(hashed_address, storage_updates); debug_assert!(existing.is_none()); } diff --git a/crates/trie/trie/src/state.rs b/crates/trie/trie/src/state.rs index 74b5fd9d19..81b8d6b03a 100644 --- a/crates/trie/trie/src/state.rs +++ b/crates/trie/trie/src/state.rs @@ -48,7 +48,9 @@ impl HashedPostState { let mut storages = HashMap::with_capacity_and_hasher(hashed.len(), Default::default()); for (address, (account, storage)) in hashed { accounts.insert(address, account); - storages.insert(address, storage); + if !storage.is_empty() { + storages.insert(address, storage); + } } Self { accounts, storages } } @@ -193,6 +195,11 @@ impl HashedStorage { Self { wiped, storage: HashMap::default() } } + /// Check if self is empty. + pub fn is_empty(&self) -> bool { + !self.wiped && self.storage.is_empty() + } + /// Create new hashed storage from iterator. pub fn from_iter(wiped: bool, iter: impl IntoIterator) -> Self { Self { wiped, storage: HashMap::from_iter(iter) }