mirror of
https://github.com/element-hq/synapse
synced 2024-08-20 03:50:20 +00:00
Stuff
This commit is contained in:
parent
c03785e121
commit
dcd574b89c
2 changed files with 43 additions and 4 deletions
|
@ -58,3 +58,20 @@ fn bench_tree_cache_length(b: &mut Bencher) {
|
||||||
|
|
||||||
b.iter(|| cache.len());
|
b.iter(|| cache.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn tree_cache_iterate(b: &mut Bencher) {
|
||||||
|
let mut cache: TreeCache<u32, u32> = TreeCache::new();
|
||||||
|
|
||||||
|
for c1 in 0..=10 {
|
||||||
|
for c2 in 0..=10 {
|
||||||
|
for c3 in 0..=10 {
|
||||||
|
for c4 in 0..=10 {
|
||||||
|
cache.set([c1, c2, c3, c4], 1).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.iter(|| cache.items().count());
|
||||||
|
}
|
||||||
|
|
|
@ -99,7 +99,11 @@ impl<'a, K: Eq + Hash + 'a, V> TreeCacheNode<K, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn items(&'a self) -> impl Iterator<Item = (Vec<&K>, &V)> {
|
pub fn items(&'a self) -> impl Iterator<Item = (Vec<&K>, &V)> {
|
||||||
let mut stack = vec![(Vec::new(), self)];
|
// To avoid a lot of mallocs we guess the length of the key. Ideally
|
||||||
|
// we'd know this.
|
||||||
|
let capacity_guesstimate = 10;
|
||||||
|
|
||||||
|
let mut stack = vec![(Vec::with_capacity(capacity_guesstimate), self)];
|
||||||
|
|
||||||
std::iter::from_fn(move || {
|
std::iter::from_fn(move || {
|
||||||
while let Some((prefix, node)) = stack.pop() {
|
while let Some((prefix, node)) = stack.pop() {
|
||||||
|
@ -107,9 +111,10 @@ impl<'a, K: Eq + Hash + 'a, V> TreeCacheNode<K, V> {
|
||||||
TreeCacheNode::Leaf(value) => return Some((prefix, value)),
|
TreeCacheNode::Leaf(value) => return Some((prefix, value)),
|
||||||
TreeCacheNode::Branch(_, map) => {
|
TreeCacheNode::Branch(_, map) => {
|
||||||
stack.extend(map.iter().map(|(k, v)| {
|
stack.extend(map.iter().map(|(k, v)| {
|
||||||
let mut prefix = prefix.clone();
|
let mut new_prefix = Vec::with_capacity(capacity_guesstimate);
|
||||||
prefix.push(k);
|
new_prefix.extend_from_slice(&prefix);
|
||||||
(prefix, v)
|
new_prefix.push(k);
|
||||||
|
(new_prefix, v)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,6 +123,23 @@ impl<'a, K: Eq + Hash + 'a, V> TreeCacheNode<K, V> {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn values(&'a self) -> impl Iterator<Item = &V> {
|
||||||
|
let mut stack = vec![self];
|
||||||
|
|
||||||
|
std::iter::from_fn(move || {
|
||||||
|
while let Some(node) = stack.pop() {
|
||||||
|
match node {
|
||||||
|
TreeCacheNode::Leaf(value) => return Some(value),
|
||||||
|
TreeCacheNode::Branch(_, map) => {
|
||||||
|
stack.extend(map.iter().map(|(_k, v)| v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: Clone + Eq + Hash + 'a, V> TreeCacheNode<K, V> {
|
impl<'a, K: Clone + Eq + Hash + 'a, V> TreeCacheNode<K, V> {
|
||||||
|
|
Loading…
Reference in a new issue