Skip locking in statedb for non-canon blocks (#10141)

This commit is contained in:
Wei Tang 2019-01-14 15:33:10 +01:00 committed by Andrew Jones
parent 181738a736
commit e8e087fc37

View File

@ -379,14 +379,7 @@ impl StateDB {
/// Check if the account can be returned from cache by matching current block parent hash against canonical /// 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. /// state and filtering out account modified in later blocks.
fn is_allowed(addr: &Address, parent_hash: &Option<H256>, modifications: &VecDeque<BlockChanges>) -> bool { fn is_allowed(addr: &Address, parent_hash: &H256, modifications: &VecDeque<BlockChanges>) -> bool {
let mut parent = match *parent_hash {
None => {
trace!("Cache lookup skipped for {:?}: no parent hash", addr);
return false;
}
Some(ref parent) => parent,
};
if modifications.is_empty() { if modifications.is_empty() {
return true; return true;
} }
@ -395,6 +388,7 @@ impl StateDB {
// We search for our parent in that list first and then for // We search for our parent in that list first and then for
// all its parent until we hit the canonical block, // all its parent until we hit the canonical block,
// checking against all the intermediate modifications. // checking against all the intermediate modifications.
let mut parent = parent_hash;
for m in modifications { for m in modifications {
if &m.hash == parent { if &m.hash == parent {
if m.is_canon { if m.is_canon {
@ -434,20 +428,25 @@ impl state::Backend for StateDB {
} }
fn get_cached_account(&self, addr: &Address) -> Option<Option<Account>> { fn get_cached_account(&self, addr: &Address) -> Option<Option<Account>> {
let mut cache = self.account_cache.lock(); self.parent_hash.as_ref().and_then(|parent_hash| {
if !Self::is_allowed(addr, &self.parent_hash, &cache.modifications) { let mut cache = self.account_cache.lock();
return None; 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())) }
cache.accounts.get_mut(addr).map(|a| a.as_ref().map(|a| a.clone_basic()))
})
} }
fn get_cached<F, U>(&self, a: &Address, f: F) -> Option<U> fn get_cached<F, U>(&self, a: &Address, f: F) -> Option<U>
where F: FnOnce(Option<&mut Account>) -> U { where F: FnOnce(Option<&mut Account>) -> U
let mut cache = self.account_cache.lock(); {
if !Self::is_allowed(a, &self.parent_hash, &cache.modifications) { self.parent_hash.as_ref().and_then(|parent_hash| {
return None; let mut cache = self.account_cache.lock();
} if !Self::is_allowed(a, parent_hash, &cache.modifications) {
cache.accounts.get_mut(a).map(|c| f(c.as_mut())) return None;
}
cache.accounts.get_mut(a).map(|c| f(c.as_mut()))
})
} }
fn get_cached_code(&self, hash: &H256) -> Option<Arc<Vec<u8>>> { fn get_cached_code(&self, hash: &H256) -> Option<Arc<Vec<u8>>> {