From e8e087fc37b6e5aa78bd15eb32f8fb7dec3bb6d9 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 14 Jan 2019 15:33:10 +0100 Subject: [PATCH] Skip locking in statedb for non-canon blocks (#10141) --- ethcore/src/state_db.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/ethcore/src/state_db.rs b/ethcore/src/state_db.rs index 6b3186452..1ceb8ff0c 100644 --- a/ethcore/src/state_db.rs +++ b/ethcore/src/state_db.rs @@ -379,14 +379,7 @@ impl StateDB { /// Check if the account can be returned from cache by matching current block parent hash against canonical /// state and filtering out account modified in later blocks. - fn is_allowed(addr: &Address, parent_hash: &Option, modifications: &VecDeque) -> bool { - let mut parent = match *parent_hash { - None => { - trace!("Cache lookup skipped for {:?}: no parent hash", addr); - return false; - } - Some(ref parent) => parent, - }; + fn is_allowed(addr: &Address, parent_hash: &H256, modifications: &VecDeque) -> bool { if modifications.is_empty() { return true; } @@ -395,6 +388,7 @@ impl StateDB { // We search for our parent in that list first and then for // all its parent until we hit the canonical block, // checking against all the intermediate modifications. + let mut parent = parent_hash; for m in modifications { if &m.hash == parent { if m.is_canon { @@ -434,20 +428,25 @@ impl state::Backend for StateDB { } fn get_cached_account(&self, addr: &Address) -> Option> { - let mut cache = self.account_cache.lock(); - if !Self::is_allowed(addr, &self.parent_hash, &cache.modifications) { - return None; - } - cache.accounts.get_mut(addr).map(|a| a.as_ref().map(|a| a.clone_basic())) + self.parent_hash.as_ref().and_then(|parent_hash| { + let mut cache = self.account_cache.lock(); + if !Self::is_allowed(addr, parent_hash, &cache.modifications) { + return None; + } + cache.accounts.get_mut(addr).map(|a| a.as_ref().map(|a| a.clone_basic())) + }) } fn get_cached(&self, a: &Address, f: F) -> Option - where F: FnOnce(Option<&mut Account>) -> U { - let mut cache = self.account_cache.lock(); - if !Self::is_allowed(a, &self.parent_hash, &cache.modifications) { - return None; - } - cache.accounts.get_mut(a).map(|c| f(c.as_mut())) + where F: FnOnce(Option<&mut Account>) -> U + { + self.parent_hash.as_ref().and_then(|parent_hash| { + let mut cache = self.account_cache.lock(); + if !Self::is_allowed(a, parent_hash, &cache.modifications) { + return None; + } + cache.accounts.get_mut(a).map(|c| f(c.as_mut())) + }) } fn get_cached_code(&self, hash: &H256) -> Option>> {