optimized hashdb keys

This commit is contained in:
debris 2017-08-26 19:09:32 +02:00
parent 9083923f27
commit 5395f13528
6 changed files with 75 additions and 46 deletions

View File

@ -17,6 +17,7 @@
//! Disk-backed `HashDB` implementation. //! Disk-backed `HashDB` implementation.
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::sync::Arc; use std::sync::Arc;
use rlp::*; use rlp::*;
use hashdb::*; use hashdb::*;
@ -66,23 +67,28 @@ impl ArchiveDB {
impl HashDB for ArchiveDB { impl HashDB for ArchiveDB {
fn keys(&self) -> HashMap<H256, i32> { fn keys(&self) -> HashMap<H256, i32> {
let mut ret: HashMap<H256, i32> = HashMap::new(); let mut ret: HashMap<H256, i32> = self.backing.iter(self.column)
for (key, _) in self.backing.iter(self.column) { .map(|(key, _)| (H256::from_slice(&*key), 1))
let h = H256::from_slice(&*key); .collect();
ret.insert(h, 1);
}
for (key, refs) in self.overlay.keys() { for (key, refs) in self.overlay.keys() {
let refs = *ret.get(&key).unwrap_or(&0) + refs; match ret.entry(key) {
ret.insert(key, refs); Entry::Occupied(mut entry) => {
*entry.get_mut() += refs;
},
Entry::Vacant(entry) => {
*entry.insert(refs);
}
}
} }
ret ret
} }
fn get(&self, key: &H256) -> Option<DBValue> { fn get(&self, key: &H256) -> Option<DBValue> {
let k = self.overlay.raw(key); if let Some((d, rc)) = self.overlay.raw(key) {
if let Some((d, rc)) = k { if rc > 0 {
if rc > 0 { return Some(d); } return Some(d);
}
} }
self.payload(key) self.payload(key)
} }

View File

@ -18,6 +18,7 @@
use std::fmt; use std::fmt;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::sync::Arc; use std::sync::Arc;
use parking_lot::RwLock; use parking_lot::RwLock;
use heapsize::HeapSizeOf; use heapsize::HeapSizeOf;
@ -311,23 +312,28 @@ impl EarlyMergeDB {
impl HashDB for EarlyMergeDB { impl HashDB for EarlyMergeDB {
fn keys(&self) -> HashMap<H256, i32> { fn keys(&self) -> HashMap<H256, i32> {
let mut ret: HashMap<H256, i32> = HashMap::new(); let mut ret: HashMap<H256, i32> = self.backing.iter(self.column)
for (key, _) in self.backing.iter(self.column) { .map(|(key, _)| (H256::from_slice(&*key), 1))
let h = H256::from_slice(&*key); .collect();
ret.insert(h, 1);
}
for (key, refs) in self.overlay.keys() { for (key, refs) in self.overlay.keys() {
let refs = *ret.get(&key).unwrap_or(&0) + refs; match ret.entry(key) {
ret.insert(key, refs); Entry::Occupied(mut entry) => {
*entry.get_mut() += refs;
},
Entry::Vacant(entry) => {
*entry.insert(refs);
}
}
} }
ret ret
} }
fn get(&self, key: &H256) -> Option<DBValue> { fn get(&self, key: &H256) -> Option<DBValue> {
let k = self.overlay.raw(key); if let Some((d, rc)) = self.overlay.raw(key) {
if let Some((d, rc)) = k { if rc > 0 {
if rc > 0 { return Some(d) } return Some(d)
}
} }
self.payload(key) self.payload(key)
} }

View File

@ -17,6 +17,7 @@
//! `JournalDB` over in-memory overlay //! `JournalDB` over in-memory overlay
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::sync::Arc; use std::sync::Arc;
use parking_lot::RwLock; use parking_lot::RwLock;
use heapsize::HeapSizeOf; use heapsize::HeapSizeOf;
@ -407,23 +408,28 @@ impl JournalDB for OverlayRecentDB {
impl HashDB for OverlayRecentDB { impl HashDB for OverlayRecentDB {
fn keys(&self) -> HashMap<H256, i32> { fn keys(&self) -> HashMap<H256, i32> {
let mut ret: HashMap<H256, i32> = HashMap::new(); let mut ret: HashMap<H256, i32> = self.backing.iter(self.column)
for (key, _) in self.backing.iter(self.column) { .map(|(key, _)| (H256::from_slice(&*key), 1))
let h = H256::from_slice(&*key); .collect();
ret.insert(h, 1);
}
for (key, refs) in self.transaction_overlay.keys() { for (key, refs) in self.transaction_overlay.keys() {
let refs = *ret.get(&key).unwrap_or(&0) + refs; match ret.entry(key) {
ret.insert(key, refs); Entry::Occupied(mut entry) => {
*entry.get_mut() += refs;
},
Entry::Vacant(entry) => {
*entry.insert(refs);
}
}
} }
ret ret
} }
fn get(&self, key: &H256) -> Option<DBValue> { fn get(&self, key: &H256) -> Option<DBValue> {
let k = self.transaction_overlay.raw(key); if let Some((d, rc)) = self.transaction_overlay.raw(key) {
if let Some((d, rc)) = k { if rc > 0 {
if rc > 0 { return Some(d) } return Some(d)
}
} }
let v = { let v = {
let journal_overlay = self.journal_overlay.read(); let journal_overlay = self.journal_overlay.read();

View File

@ -198,7 +198,7 @@ impl JournalDB for RefCountedDB {
fn consolidate(&mut self, mut with: MemoryDB) { fn consolidate(&mut self, mut with: MemoryDB) {
for (key, (value, rc)) in with.drain() { for (key, (value, rc)) in with.drain() {
for _ in 0..rc { for _ in 0..rc {
self.emplace(key.clone(), value.clone()); self.emplace(key, value.clone());
} }
for _ in rc..0 { for _ in rc..0 {

View File

@ -16,14 +16,14 @@
//! Reference-counted memory-based `HashDB` implementation. //! Reference-counted memory-based `HashDB` implementation.
use hash::*;
use rlp::*;
use sha3::*;
use hashdb::*;
use heapsize::*;
use std::mem; use std::mem;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use heapsize::HeapSizeOf;
use hash::{H256FastMap, H256};
use rlp::NULL_RLP;
use sha3::*;
use hashdb::*;
/// Reference-counted memory-based `HashDB` implementation. /// Reference-counted memory-based `HashDB` implementation.
/// ///

View File

@ -16,13 +16,14 @@
//! Disk-backed `HashDB` implementation. //! Disk-backed `HashDB` implementation.
use std::sync::Arc;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use error::*; use error::*;
use hash::*; use hash::*;
use rlp::*; use rlp::*;
use hashdb::*; use hashdb::*;
use memorydb::*; use memorydb::*;
use std::sync::*;
use std::collections::HashMap;
use kvdb::{KeyValueDB, DBTransaction}; use kvdb::{KeyValueDB, DBTransaction};
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay. /// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay.
@ -125,19 +126,27 @@ impl OverlayDB {
impl HashDB for OverlayDB { impl HashDB for OverlayDB {
fn keys(&self) -> HashMap<H256, i32> { fn keys(&self) -> HashMap<H256, i32> {
let mut ret: HashMap<H256, i32> = HashMap::new(); let mut ret: HashMap<H256, i32> = self.backing.iter(self.column)
for (key, _) in self.backing.iter(self.column) { .map(|(key, _)| {
let h = H256::from_slice(&*key); let h = H256::from_slice(&*key);
let r = self.payload(&h).unwrap().1; let r = self.payload(&h).unwrap().1;
ret.insert(h, r as i32); (h, r as i32)
} })
.collect();
for (key, refs) in self.overlay.keys() { for (key, refs) in self.overlay.keys() {
let refs = *ret.get(&key).unwrap_or(&0) + refs; match ret.entry(key) {
ret.insert(key, refs); Entry::Occupied(mut entry) => {
*entry.get_mut() += refs;
},
Entry::Vacant(entry) => {
*entry.insert(refs);
}
}
} }
ret ret
} }
fn get(&self, key: &H256) -> Option<DBValue> { fn get(&self, key: &H256) -> Option<DBValue> {
// return ok if positive; if negative, check backing - might be enough references there to make // return ok if positive; if negative, check backing - might be enough references there to make
// it positive again. // it positive again.
@ -165,6 +174,7 @@ impl HashDB for OverlayDB {
_ => None, _ => None,
} }
} }
fn contains(&self, key: &H256) -> bool { fn contains(&self, key: &H256) -> bool {
// return ok if positive; if negative, check backing - might be enough references there to make // return ok if positive; if negative, check backing - might be enough references there to make
// it positive again. // it positive again.
@ -185,6 +195,7 @@ impl HashDB for OverlayDB {
} }
} }
} }
fn insert(&mut self, value: &[u8]) -> H256 { self.overlay.insert(value) } fn insert(&mut self, value: &[u8]) -> H256 { self.overlay.insert(value) }
fn emplace(&mut self, key: H256, value: DBValue) { self.overlay.emplace(key, value); } fn emplace(&mut self, key: H256, value: DBValue) { self.overlay.emplace(key, value); }
fn remove(&mut self, key: &H256) { self.overlay.remove(key); } fn remove(&mut self, key: &H256) { self.overlay.remove(key); }