make patricia-trie more idiomatic and remove redundant code (#8056)
This commit is contained in:
parent
eeee90def5
commit
ff722cac72
@ -68,7 +68,6 @@ fn trie_insertions_32_mir_1k(b: &mut Bencher) {
|
|||||||
count: 1000,
|
count: 1000,
|
||||||
};
|
};
|
||||||
let d = st.make();
|
let d = st.make();
|
||||||
let mut hash_count = 0usize;
|
|
||||||
b.iter(&mut ||{
|
b.iter(&mut ||{
|
||||||
let mut memdb = MemoryDB::new();
|
let mut memdb = MemoryDB::new();
|
||||||
let mut root = H256::new();
|
let mut root = H256::new();
|
||||||
@ -76,9 +75,7 @@ fn trie_insertions_32_mir_1k(b: &mut Bencher) {
|
|||||||
for i in d.iter() {
|
for i in d.iter() {
|
||||||
t.insert(&i.0, &i.1).unwrap();
|
t.insert(&i.0, &i.1).unwrap();
|
||||||
}
|
}
|
||||||
hash_count = t.hash_count;
|
|
||||||
});
|
});
|
||||||
// println!("hash_count: {}", hash_count);
|
|
||||||
}
|
}
|
||||||
#[bench]
|
#[bench]
|
||||||
fn trie_iter(b: &mut Bencher) {
|
fn trie_iter(b: &mut Bencher) {
|
||||||
@ -117,7 +114,6 @@ fn trie_insertions_32_ran_1k(b: &mut Bencher) {
|
|||||||
count: 1000,
|
count: 1000,
|
||||||
};
|
};
|
||||||
let d = st.make();
|
let d = st.make();
|
||||||
let mut hash_count = 0usize;
|
|
||||||
let mut r = H256::new();
|
let mut r = H256::new();
|
||||||
b.iter(&mut ||{
|
b.iter(&mut ||{
|
||||||
let mut memdb = MemoryDB::new();
|
let mut memdb = MemoryDB::new();
|
||||||
@ -126,7 +122,6 @@ fn trie_insertions_32_ran_1k(b: &mut Bencher) {
|
|||||||
for i in d.iter() {
|
for i in d.iter() {
|
||||||
t.insert(&i.0, &i.1).unwrap();
|
t.insert(&i.0, &i.1).unwrap();
|
||||||
}
|
}
|
||||||
hash_count = t.hash_count;
|
|
||||||
r = t.root().clone();
|
r = t.root().clone();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -117,30 +117,6 @@ pub enum OwnedNode {
|
|||||||
Branch([NodeKey; 16], Option<DBValue>),
|
Branch([NodeKey; 16], Option<DBValue>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for OwnedNode {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
match *self {
|
|
||||||
OwnedNode::Empty => OwnedNode::Empty,
|
|
||||||
OwnedNode::Leaf(ref k, ref v) => OwnedNode::Leaf(k.clone(), v.clone()),
|
|
||||||
OwnedNode::Extension(ref k, ref c) => OwnedNode::Extension(k.clone(), c.clone()),
|
|
||||||
OwnedNode::Branch(ref c, ref v) => {
|
|
||||||
let mut children = [
|
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
|
||||||
];
|
|
||||||
|
|
||||||
for (owned, borrowed) in children.iter_mut().zip(c.iter()) {
|
|
||||||
*owned = borrowed.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
OwnedNode::Branch(children, v.as_ref().cloned())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<Node<'a>> for OwnedNode {
|
impl<'a> From<Node<'a>> for OwnedNode {
|
||||||
fn from(node: Node<'a>) -> Self {
|
fn from(node: Node<'a>) -> Self {
|
||||||
match node {
|
match node {
|
||||||
@ -148,17 +124,13 @@ impl<'a> From<Node<'a>> for OwnedNode {
|
|||||||
Node::Leaf(k, v) => OwnedNode::Leaf(k.into(), DBValue::from_slice(v)),
|
Node::Leaf(k, v) => OwnedNode::Leaf(k.into(), DBValue::from_slice(v)),
|
||||||
Node::Extension(k, child) => OwnedNode::Extension(k.into(), DBValue::from_slice(child)),
|
Node::Extension(k, child) => OwnedNode::Extension(k.into(), DBValue::from_slice(child)),
|
||||||
Node::Branch(c, val) => {
|
Node::Branch(c, val) => {
|
||||||
let mut children = [
|
let children = [
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
NodeKey::from_slice(c[0]), NodeKey::from_slice(c[1]), NodeKey::from_slice(c[2]), NodeKey::from_slice(c[3]),
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
NodeKey::from_slice(c[4]), NodeKey::from_slice(c[5]), NodeKey::from_slice(c[6]), NodeKey::from_slice(c[7]),
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
NodeKey::from_slice(c[8]), NodeKey::from_slice(c[9]), NodeKey::from_slice(c[10]), NodeKey::from_slice(c[11]),
|
||||||
NodeKey::new(), NodeKey::new(), NodeKey::new(), NodeKey::new(),
|
NodeKey::from_slice(c[12]), NodeKey::from_slice(c[13]), NodeKey::from_slice(c[14]), NodeKey::from_slice(c[15]),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (owned, borrowed) in children.iter_mut().zip(c.iter()) {
|
|
||||||
*owned = NodeKey::from_slice(borrowed)
|
|
||||||
}
|
|
||||||
|
|
||||||
OwnedNode::Branch(children, val.map(DBValue::from_slice))
|
OwnedNode::Branch(children, val.map(DBValue::from_slice))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ pub struct TrieDB<'db> {
|
|||||||
db: &'db HashDB,
|
db: &'db HashDB,
|
||||||
root: &'db H256,
|
root: &'db H256,
|
||||||
/// The number of hashes performed so far in operations on this trie.
|
/// The number of hashes performed so far in operations on this trie.
|
||||||
pub hash_count: usize,
|
hash_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'db> TrieDB<'db> {
|
impl<'db> TrieDB<'db> {
|
||||||
@ -180,7 +180,7 @@ enum Status {
|
|||||||
Exiting,
|
Exiting,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq)]
|
#[derive(Eq, PartialEq)]
|
||||||
struct Crumb {
|
struct Crumb {
|
||||||
node: OwnedNode,
|
node: OwnedNode,
|
||||||
status: Status,
|
status: Status,
|
||||||
@ -200,7 +200,6 @@ impl Crumb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Iterator for going through all values in the trie.
|
/// Iterator for going through all values in the trie.
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct TrieDBIterator<'a> {
|
pub struct TrieDBIterator<'a> {
|
||||||
db: &'a TrieDB<'a>,
|
db: &'a TrieDB<'a>,
|
||||||
trail: Vec<Crumb>,
|
trail: Vec<Crumb>,
|
||||||
@ -340,11 +339,7 @@ impl<'a> Iterator for TrieDBIterator<'a> {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let iter_step = {
|
let iter_step = {
|
||||||
match self.trail.last_mut() {
|
self.trail.last_mut()?.increment();
|
||||||
Some(b) => { b.increment(); },
|
|
||||||
None => return None,
|
|
||||||
}
|
|
||||||
|
|
||||||
let b = self.trail.last().expect("trail.last_mut().is_some(); qed");
|
let b = self.trail.last().expect("trail.last_mut().is_some(); qed");
|
||||||
|
|
||||||
match (b.status.clone(), &b.node) {
|
match (b.status.clone(), &b.node) {
|
||||||
|
@ -105,16 +105,23 @@ impl Node {
|
|||||||
RlpNode::Extension(key, cb) => {
|
RlpNode::Extension(key, cb) => {
|
||||||
Node::Extension(key.encoded(false), Self::inline_or_hash(cb, db, storage))
|
Node::Extension(key.encoded(false), Self::inline_or_hash(cb, db, storage))
|
||||||
}
|
}
|
||||||
RlpNode::Branch(children_rlp, val) => {
|
RlpNode::Branch(ref children_rlp, val) => {
|
||||||
let mut children = empty_children();
|
let mut child = |i| {
|
||||||
|
|
||||||
for i in 0..16 {
|
|
||||||
let raw = children_rlp[i];
|
let raw = children_rlp[i];
|
||||||
let child_rlp = Rlp::new(raw);
|
let child_rlp = Rlp::new(raw);
|
||||||
if !child_rlp.is_empty() {
|
if !child_rlp.is_empty() {
|
||||||
children[i] = Some(Self::inline_or_hash(raw, db, storage));
|
Some(Self::inline_or_hash(raw, db, storage))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
let children = Box::new([
|
||||||
|
child(0), child(1), child(2), child(3),
|
||||||
|
child(4), child(5), child(6), child(7),
|
||||||
|
child(8), child(9), child(10), child(11),
|
||||||
|
child(12), child(13), child(14), child(15),
|
||||||
|
]);
|
||||||
|
|
||||||
Node::Branch(children, val.map(DBValue::from_slice))
|
Node::Branch(children, val.map(DBValue::from_slice))
|
||||||
}
|
}
|
||||||
@ -294,7 +301,7 @@ pub struct TrieDBMut<'a> {
|
|||||||
death_row: HashSet<H256>,
|
death_row: HashSet<H256>,
|
||||||
/// The number of hash operations this trie has performed.
|
/// The number of hash operations this trie has performed.
|
||||||
/// Note that none are performed until changes are committed.
|
/// Note that none are performed until changes are committed.
|
||||||
pub hash_count: usize,
|
hash_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TrieDBMut<'a> {
|
impl<'a> TrieDBMut<'a> {
|
||||||
|
Loading…
Reference in New Issue
Block a user