diff --git a/crates/ruma-state-res/benches/state_res_bench.rs b/crates/ruma-state-res/benches/state_res_bench.rs index 57b19667..2673eded 100644 --- a/crates/ruma-state-res/benches/state_res_bench.rs +++ b/crates/ruma-state-res/benches/state_res_bench.rs @@ -8,7 +8,7 @@ #![allow(clippy::exhaustive_structs)] use std::{ - collections::{BTreeMap, BTreeSet}, + collections::{HashMap, HashSet}, convert::{TryFrom, TryInto}, sync::{ atomic::{AtomicU64, Ordering::SeqCst}, @@ -19,7 +19,7 @@ use std::{ use criterion::{criterion_group, criterion_main, Criterion}; use event::StateEvent; use js_int::uint; -use maplit::{btreemap, btreeset}; +use maplit::{btreemap, hashmap, hashset}; use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ pdu::{EventHash, Pdu, RoomV3Pdu}, @@ -37,12 +37,12 @@ static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0); fn lexico_topo_sort(c: &mut Criterion) { c.bench_function("lexicographical topological sort", |b| { - let graph = btreemap! { - event_id("l") => btreeset![event_id("o")], - event_id("m") => btreeset![event_id("n"), event_id("o")], - event_id("n") => btreeset![event_id("o")], - event_id("o") => btreeset![], // "o" has zero outgoing edges but 4 incoming edges - event_id("p") => btreeset![event_id("o")], + let graph = hashmap! { + event_id("l") => hashset![event_id("o")], + event_id("m") => hashset![event_id("n"), event_id("o")], + event_id("n") => hashset![event_id("o")], + event_id("o") => hashset![], // "o" has zero outgoing edges but 4 incoming edges + event_id("p") => hashset![event_id("o")], }; b.iter(|| { let _ = StateResolution::lexicographical_topological_sort(&graph, |id| { @@ -54,7 +54,7 @@ fn lexico_topo_sort(c: &mut Criterion) { fn resolution_shallow_auth_chain(c: &mut Criterion) { c.bench_function("resolve state of 5 events one fork", |b| { - let mut store = TestStore(btreemap! {}); + let mut store = TestStore(hashmap! {}); // build up the DAG let (state_at_bob, state_at_charlie, _) = store.set_up(); @@ -154,7 +154,7 @@ criterion_main!(benches); // IMPLEMENTATION DETAILS AHEAD // /////////////////////////////////////////////////////////////////////*/ -pub struct TestStore(pub BTreeMap>); +pub struct TestStore(pub HashMap>); #[allow(unused)] impl TestStore { @@ -179,8 +179,8 @@ impl TestStore { &self, room_id: &RoomId, event_ids: &[EventId], - ) -> Result> { - let mut result = BTreeSet::new(); + ) -> Result> { + let mut result = HashSet::new(); let mut stack = event_ids.to_vec(); // DFS for auth event chain @@ -206,26 +206,21 @@ impl TestStore { room_id: &RoomId, event_ids: Vec>, ) -> Result> { - let mut chains = vec![]; + let mut auth_chain_sets = vec![]; for ids in event_ids { // TODO state store `auth_event_ids` returns self in the event ids list // when an event returns `auth_event_ids` self is not contained - let chain = self.auth_event_ids(room_id, &ids)?.into_iter().collect::>(); - chains.push(chain); + let chain = self.auth_event_ids(room_id, &ids)?.into_iter().collect::>(); + auth_chain_sets.push(chain); } - if let Some(chain) = chains.first() { - let rest = chains.iter().skip(1).flatten().cloned().collect(); - let common = chain.intersection(&rest).collect::>(); - - Ok(chains + if let Some(first) = auth_chain_sets.first().cloned() { + let common = auth_chain_sets .iter() - .flatten() - .filter(|id| !common.contains(id)) - .cloned() - .collect::>() - .into_iter() - .collect()) + .skip(1) + .fold(first, |a, b| a.intersection(b).cloned().collect::>()); + + Ok(auth_chain_sets.into_iter().flatten().filter(|id| !common.contains(id)).collect()) } else { Ok(vec![]) } @@ -387,7 +382,7 @@ where // all graphs start with these input events #[allow(non_snake_case)] -fn INITIAL_EVENTS() -> BTreeMap> { +fn INITIAL_EVENTS() -> HashMap> { vec![ to_pdu_event::( "CREATE", @@ -469,7 +464,7 @@ fn INITIAL_EVENTS() -> BTreeMap> { // all graphs start with these input events #[allow(non_snake_case)] -fn BAN_STATE_SET() -> BTreeMap> { +fn BAN_STATE_SET() -> HashMap> { vec![ to_pdu_event( "PA", diff --git a/crates/ruma-state-res/src/lib.rs b/crates/ruma-state-res/src/lib.rs index 2c7d01dc..91c49227 100644 --- a/crates/ruma-state-res/src/lib.rs +++ b/crates/ruma-state-res/src/lib.rs @@ -105,7 +105,7 @@ impl StateResolution { let mut auth_diff = StateResolution::get_auth_chain_diff(room_id, auth_chain_sets)?; // Add the auth_diff to conflicting now we have a full set of conflicting events - auth_diff.extend(conflicting.values().cloned().flatten().filter_map(|o| o)); + auth_diff.extend(conflicting.values().cloned().flatten().flatten()); debug!("auth diff: {}", auth_diff.len()); trace!("{:?}", auth_diff); @@ -230,9 +230,9 @@ impl StateResolution { let common = auth_chain_sets .iter() .skip(1) - .fold(first, |a, b| a.intersection(&b).cloned().collect::>()); + .fold(first, |a, b| a.intersection(b).cloned().collect::>()); - Ok(auth_chain_sets.into_iter().flatten().filter(|id| !common.contains(&id)).collect()) + Ok(auth_chain_sets.into_iter().flatten().filter(|id| !common.contains(id)).collect()) } else { Ok(hashset![]) }