perf(trie): use smallvec as the Nibbles representation (#5641)

This commit is contained in:
DaniPopes
2023-12-01 23:45:15 +01:00
committed by GitHub
parent 5ac4a3d4cb
commit 542639cc6f
13 changed files with 456 additions and 167 deletions

View File

@ -94,12 +94,12 @@ fn generate_test_data(size: usize) -> (Vec<Nibbles>, Vec<Nibbles>, Vec<bool>) {
let mut preload = vec(vec(any::<u8>(), 32), size).new_tree(&mut runner).unwrap().current();
preload.dedup();
preload.sort();
let preload = preload.into_iter().map(Nibbles::new_unchecked).collect::<Vec<_>>();
let preload = preload.into_iter().map(Nibbles::from_nibbles_unchecked).collect::<Vec<_>>();
let mut input = vec(vec(any::<u8>(), 0..=32), size).new_tree(&mut runner).unwrap().current();
input.dedup();
input.sort();
let input = input.into_iter().map(Nibbles::new_unchecked).collect::<Vec<_>>();
let input = input.into_iter().map(Nibbles::from_nibbles_unchecked).collect::<Vec<_>>();
let expected = input
.iter()

View File

@ -26,8 +26,8 @@ pub use loader::{LoadedPrefixSets, PrefixSetLoader};
/// use reth_trie::prefix_set::PrefixSetMut;
///
/// let mut prefix_set = PrefixSetMut::default();
/// prefix_set.insert(Nibbles::new_unchecked(&[0xa, 0xb]));
/// prefix_set.insert(Nibbles::new_unchecked(&[0xa, 0xb, 0xc]));
/// prefix_set.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb]));
/// prefix_set.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb, 0xc]));
/// assert!(prefix_set.contains(&[0xa, 0xb]));
/// assert!(prefix_set.contains(&[0xa, 0xb, 0xc]));
/// ```
@ -158,10 +158,10 @@ mod tests {
#[test]
fn test_contains_with_multiple_inserts_and_duplicates() {
let mut prefix_set = PrefixSetMut::default();
prefix_set.insert(Nibbles::new_unchecked(b"123"));
prefix_set.insert(Nibbles::new_unchecked(b"124"));
prefix_set.insert(Nibbles::new_unchecked(b"456"));
prefix_set.insert(Nibbles::new_unchecked(b"123")); // Duplicate
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"123"));
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"124"));
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"456"));
prefix_set.insert(Nibbles::from_nibbles_unchecked(b"123")); // Duplicate
assert!(prefix_set.contains(b"12"));
assert!(prefix_set.contains(b"45"));

View File

@ -41,7 +41,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use reth_db::{
cursor::{DbCursorRO, DbCursorRW},

View File

@ -39,7 +39,7 @@ impl From<StoredSubNode> for CursorSubNode {
Some(n) => n as i8,
None => -1,
};
Self { key: Nibbles::new_unchecked(value.key), nibble, node: value.node }
Self { key: Nibbles::from_nibbles_unchecked(value.key), nibble, node: value.node }
}
}

View File

@ -131,7 +131,7 @@ impl<C: TrieCursor> TrieWalker<C> {
assert!(!node.state_mask.is_empty());
}
Ok(entry.map(|(k, v)| (Nibbles::new_unchecked(k), v)))
Ok(entry.map(|(k, v)| (Nibbles::from_nibbles_unchecked(k), v)))
}
/// Consumes the next node in the trie, updating the stack.
@ -313,7 +313,7 @@ mod tests {
// We're traversing the path in lexigraphical order.
for expected in expected {
let got = walker.advance().unwrap();
assert_eq!(got.unwrap(), Nibbles::new_unchecked(expected.clone()));
assert_eq!(got.unwrap(), Nibbles::from_nibbles_unchecked(expected.clone()));
}
// There should be 8 paths traversed in total from 3 branches.
@ -361,26 +361,26 @@ mod tests {
// No changes
let mut cursor = TrieWalker::new(&mut trie, Default::default());
assert_eq!(cursor.key(), Some(Nibbles::new_unchecked([]))); // root
assert_eq!(cursor.key(), Some(Nibbles::from_nibbles_unchecked([]))); // root
assert!(cursor.can_skip_current_node); // due to root_hash
cursor.advance().unwrap(); // skips to the end of trie
assert_eq!(cursor.key(), None);
// We insert something that's not part of the existing trie/prefix.
let mut changed = PrefixSetMut::default();
changed.insert(Nibbles::new_unchecked([0xF, 0x1]));
changed.insert(Nibbles::from_nibbles_unchecked([0xF, 0x1]));
let mut cursor = TrieWalker::new(&mut trie, changed.freeze());
// Root node
assert_eq!(cursor.key(), Some(Nibbles::new_unchecked([])));
assert_eq!(cursor.key(), Some(Nibbles::from_nibbles_unchecked([])));
// Should not be able to skip state due to the changed values
assert!(!cursor.can_skip_current_node);
cursor.advance().unwrap();
assert_eq!(cursor.key(), Some(Nibbles::new_unchecked([0x2])));
assert_eq!(cursor.key(), Some(Nibbles::from_nibbles_unchecked([0x2])));
cursor.advance().unwrap();
assert_eq!(cursor.key(), Some(Nibbles::new_unchecked([0x2, 0x1])));
assert_eq!(cursor.key(), Some(Nibbles::from_nibbles_unchecked([0x2, 0x1])));
cursor.advance().unwrap();
assert_eq!(cursor.key(), Some(Nibbles::new_unchecked([0x4])));
assert_eq!(cursor.key(), Some(Nibbles::from_nibbles_unchecked([0x4])));
cursor.advance().unwrap();
assert_eq!(cursor.key(), None); // the end of trie