Revert collecting trie stats.

This commit is contained in:
Gav Wood 2016-02-10 18:26:39 +01:00
parent e8aaf26ab4
commit 0e679fbee5

View File

@ -14,8 +14,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::RwLock;
use std::cell::RefCell;
use hash::*; use hash::*;
use sha3::*; use sha3::*;
use hashdb::*; use hashdb::*;
@ -23,26 +21,11 @@ use rlp::*;
use super::triedbmut::*; use super::triedbmut::*;
use super::trietraits::*; use super::trietraits::*;
lazy_static! {
static ref COMMIT_COUNT: RwLock<usize> = RwLock::new(0);
static ref UPDATE_COUNT: RwLock<usize> = RwLock::new(0);
}
/// Get mean number of updates per commit so far.
pub fn updates_per_commit() -> f64 {
let cc = *COMMIT_COUNT.read().unwrap();
if cc > 0 {
(*UPDATE_COUNT.read().unwrap() as f64) / (cc as f64)
} else { 0.0 }
}
/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database. /// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
/// ///
/// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing TrieDBMut object. /// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing TrieDBMut object.
pub struct SecTrieDBMut<'db> { pub struct SecTrieDBMut<'db> {
raw: TrieDBMut<'db>, raw: TrieDBMut<'db>
/// Get number of updates done on this trie so far.
pub update_count: RefCell<usize>,
} }
impl<'db> SecTrieDBMut<'db> { impl<'db> SecTrieDBMut<'db> {
@ -50,13 +33,13 @@ impl<'db> SecTrieDBMut<'db> {
/// Initialise to the state entailed by the genesis block. /// Initialise to the state entailed by the genesis block.
/// This guarantees the trie is built correctly. /// This guarantees the trie is built correctly.
pub fn new(db: &'db mut HashDB, root: &'db mut H256) -> Self { pub fn new(db: &'db mut HashDB, root: &'db mut H256) -> Self {
SecTrieDBMut { raw: TrieDBMut::new(db, root), update_count: RefCell::new(0) } SecTrieDBMut { raw: TrieDBMut::new(db, root) }
} }
/// Create a new trie with the backing database `db` and `root` /// Create a new trie with the backing database `db` and `root`
/// Panics, if `root` does not exist /// Panics, if `root` does not exist
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self { pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self {
SecTrieDBMut { raw: TrieDBMut::from_existing(db, root), update_count: RefCell::new(0) } SecTrieDBMut { raw: TrieDBMut::from_existing(db, root) }
} }
/// Get the backing database. /// Get the backing database.
@ -67,9 +50,7 @@ impl<'db> SecTrieDBMut<'db> {
} }
impl<'db> Trie for SecTrieDBMut<'db> { impl<'db> Trie for SecTrieDBMut<'db> {
fn root(&self) -> &H256 { fn root(&self) -> &H256 { self.raw.root() }
self.raw.root()
}
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {
self.raw.contains(&key.sha3()) self.raw.contains(&key.sha3())
@ -82,27 +63,14 @@ impl<'db> Trie for SecTrieDBMut<'db> {
impl<'db> TrieMut for SecTrieDBMut<'db> { impl<'db> TrieMut for SecTrieDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) { fn insert(&mut self, key: &[u8], value: &[u8]) {
*self.update_count.borrow_mut() += 1;
self.raw.insert(&key.sha3(), value); self.raw.insert(&key.sha3(), value);
} }
fn remove(&mut self, key: &[u8]) { fn remove(&mut self, key: &[u8]) {
*self.update_count.borrow_mut() += 1;
self.raw.remove(&key.sha3()); self.raw.remove(&key.sha3());
} }
} }
impl<'db> Drop for SecTrieDBMut<'db> {
fn drop(&mut self) {
let uc = *self.update_count.borrow();
if uc > 0 {
*COMMIT_COUNT.write().unwrap() += 1;
*UPDATE_COUNT.write().unwrap() += uc;
*self.update_count.borrow_mut() = 0;
}
}
}
#[test] #[test]
fn sectrie_to_trie() { fn sectrie_to_trie() {
use memorydb::*; use memorydb::*;