House keeping, remove &mut self in all methods
Improved perf -7.8220%
This commit is contained in:
parent
77cbcc7ee2
commit
8a9f15e01f
10
Cargo.toml
10
Cargo.toml
@ -3,6 +3,13 @@ name = "state-res"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Devin R <devin.ragotzy@gmail.com>"]
|
authors = ["Devin R <devin.ragotzy@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
categories = ["api-bindings", "web-programming"]
|
||||||
|
description = "An abstraction for Matrix state resolution."
|
||||||
|
homepage = "https://www.ruma.io/"
|
||||||
|
keywords = ["matrix", "chat", "state resolution", "ruma"]
|
||||||
|
license = "MIT"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "https://github.com/ruma/state-res"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -19,7 +26,6 @@ tracing-subscriber = "0.2.8"
|
|||||||
[dependencies.ruma]
|
[dependencies.ruma]
|
||||||
git = "https://github.com/DevinR528/ruma"
|
git = "https://github.com/DevinR528/ruma"
|
||||||
branch = "pdu-deserialize"
|
branch = "pdu-deserialize"
|
||||||
# path = "../__forks__/ruma/ruma"
|
|
||||||
features = ["client-api", "federation-api", "appservice-api"]
|
features = ["client-api", "federation-api", "appservice-api"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
@ -27,5 +33,5 @@ lazy_static = "1.4.0"
|
|||||||
criterion = "0.3.3"
|
criterion = "0.3.3"
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "state_bench"
|
name = "state_res_bench"
|
||||||
harness = false
|
harness = false
|
0
benches/event_auth_bench.rs
Normal file
0
benches/event_auth_bench.rs
Normal file
@ -37,7 +37,7 @@ fn lexico_topo_sort(c: &mut Criterion) {
|
|||||||
event_id("p") => vec![event_id("o")],
|
event_id("p") => vec![event_id("o")],
|
||||||
};
|
};
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let _ = resolver
|
let _ = resolver
|
||||||
.lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, Some(id.clone())));
|
.lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, Some(id.clone())));
|
||||||
@ -47,7 +47,7 @@ fn lexico_topo_sort(c: &mut Criterion) {
|
|||||||
|
|
||||||
fn resolution_shallow_auth_chain(c: &mut Criterion) {
|
fn resolution_shallow_auth_chain(c: &mut Criterion) {
|
||||||
c.bench_function("resolve state of 5 events one fork", |b| {
|
c.bench_function("resolve state of 5 events one fork", |b| {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let store = TestStore(RefCell::new(btreemap! {}));
|
let store = TestStore(RefCell::new(btreemap! {}));
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ fn resolution_shallow_auth_chain(c: &mut Criterion) {
|
|||||||
|
|
||||||
fn resolve_deeper_event_set(c: &mut Criterion) {
|
fn resolve_deeper_event_set(c: &mut Criterion) {
|
||||||
c.bench_function("resolve state of 10 events 3 conflicting", |b| {
|
c.bench_function("resolve state of 10 events 3 conflicting", |b| {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let init = INITIAL_EVENTS();
|
let init = INITIAL_EVENTS();
|
||||||
let ban = BAN_STATE_SET();
|
let ban = BAN_STATE_SET();
|
42
src/lib.rs
42
src/lib.rs
@ -11,7 +11,6 @@ use ruma::{
|
|||||||
events::EventType,
|
events::EventType,
|
||||||
identifiers::{EventId, RoomId, RoomVersionId},
|
identifiers::{EventId, RoomId, RoomVersionId},
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod event_auth;
|
mod event_auth;
|
||||||
@ -40,29 +39,14 @@ pub type StateMap<T> = BTreeMap<(EventType, String), T>;
|
|||||||
/// A mapping of `EventId` to `T`, usually a `StateEvent`.
|
/// A mapping of `EventId` to `T`, usually a `StateEvent`.
|
||||||
pub type EventMap<T> = BTreeMap<EventId, T>;
|
pub type EventMap<T> = BTreeMap<EventId, T>;
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize, Serialize)] // TODO make the ser/de impls useful
|
#[derive(Default)]
|
||||||
pub struct StateResolution {
|
pub struct StateResolution;
|
||||||
// TODO remove pub after initial testing
|
|
||||||
/// The set of resolved events over time.
|
|
||||||
pub resolved_events: Vec<StateEvent>,
|
|
||||||
/// The resolved state, kept to have easy access to the last resolved
|
|
||||||
/// layer of state.
|
|
||||||
pub state: BTreeMap<EventType, BTreeMap<String, StateEvent>>,
|
|
||||||
/// The graph of authenticated events, kept to find the most recent auth event
|
|
||||||
/// in a chain for incoming state sets.
|
|
||||||
pub auth_graph: BTreeMap<EventId, Vec<StateMap<EventId>>>,
|
|
||||||
/// The last known point in the state graph.
|
|
||||||
pub most_recent_resolved: Option<(EventType, String)>,
|
|
||||||
|
|
||||||
// fields for temp storage during resolution
|
|
||||||
pub conflicting_events: Vec<StateEvent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StateResolution {
|
impl StateResolution {
|
||||||
/// Resolve sets of state events as they come in. Internally `StateResolution` builds a graph
|
/// Resolve sets of state events as they come in. Internally `StateResolution` builds a graph
|
||||||
/// and an auth chain to allow for state conflict resolution.
|
/// and an auth chain to allow for state conflict resolution.
|
||||||
pub fn resolve(
|
pub fn resolve(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
room_version: &RoomVersionId,
|
room_version: &RoomVersionId,
|
||||||
state_sets: &[StateMap<EventId>],
|
state_sets: &[StateMap<EventId>],
|
||||||
@ -247,7 +231,7 @@ impl StateResolution {
|
|||||||
///
|
///
|
||||||
/// The tuple looks like `(unconflicted, conflicted)`.
|
/// The tuple looks like `(unconflicted, conflicted)`.
|
||||||
pub fn separate(
|
pub fn separate(
|
||||||
&mut self,
|
&self,
|
||||||
state_sets: &[StateMap<EventId>],
|
state_sets: &[StateMap<EventId>],
|
||||||
) -> (StateMap<EventId>, StateMap<Vec<EventId>>) {
|
) -> (StateMap<EventId>, StateMap<Vec<EventId>>) {
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -299,7 +283,7 @@ impl StateResolution {
|
|||||||
|
|
||||||
/// Returns a Vec of deduped EventIds that appear in some chains but no others.
|
/// Returns a Vec of deduped EventIds that appear in some chains but no others.
|
||||||
pub fn get_auth_chain_diff(
|
pub fn get_auth_chain_diff(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
state_sets: &[StateMap<EventId>],
|
state_sets: &[StateMap<EventId>],
|
||||||
store: &dyn StateStore,
|
store: &dyn StateStore,
|
||||||
@ -321,7 +305,7 @@ impl StateResolution {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn reverse_topological_power_sort(
|
pub fn reverse_topological_power_sort(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
power_events: &[EventId],
|
power_events: &[EventId],
|
||||||
event_map: &mut EventMap<StateEvent>,
|
event_map: &mut EventMap<StateEvent>,
|
||||||
@ -379,7 +363,7 @@ impl StateResolution {
|
|||||||
/// `key_fn` is used as a tie breaker. The tie breaker happens based on
|
/// `key_fn` is used as a tie breaker. The tie breaker happens based on
|
||||||
/// power level, age, and event_id.
|
/// power level, age, and event_id.
|
||||||
pub fn lexicographical_topological_sort<F>(
|
pub fn lexicographical_topological_sort<F>(
|
||||||
&mut self,
|
&self,
|
||||||
graph: &BTreeMap<EventId, Vec<EventId>>,
|
graph: &BTreeMap<EventId, Vec<EventId>>,
|
||||||
key_fn: F,
|
key_fn: F,
|
||||||
) -> Vec<EventId>
|
) -> Vec<EventId>
|
||||||
@ -446,7 +430,7 @@ impl StateResolution {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_power_level_for_sender(
|
fn get_power_level_for_sender(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
event_id: &EventId,
|
event_id: &EventId,
|
||||||
event_map: &mut EventMap<StateEvent>,
|
event_map: &mut EventMap<StateEvent>,
|
||||||
@ -506,7 +490,7 @@ impl StateResolution {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn iterative_auth_check(
|
fn iterative_auth_check(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
room_version: &RoomVersionId,
|
room_version: &RoomVersionId,
|
||||||
power_events: &[EventId],
|
power_events: &[EventId],
|
||||||
@ -581,7 +565,7 @@ impl StateResolution {
|
|||||||
/// NOTE we rely on the `event_map` beign full at this point.
|
/// NOTE we rely on the `event_map` beign full at this point.
|
||||||
/// TODO is this ok?
|
/// TODO is this ok?
|
||||||
fn mainline_sort(
|
fn mainline_sort(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
to_sort: &[EventId],
|
to_sort: &[EventId],
|
||||||
resolved_power_level: Option<&EventId>,
|
resolved_power_level: Option<&EventId>,
|
||||||
@ -659,7 +643,7 @@ impl StateResolution {
|
|||||||
|
|
||||||
// TODO make `event` not clone every loop
|
// TODO make `event` not clone every loop
|
||||||
fn get_mainline_depth(
|
fn get_mainline_depth(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
mut event: Option<StateEvent>,
|
mut event: Option<StateEvent>,
|
||||||
mainline_map: &EventMap<usize>,
|
mainline_map: &EventMap<usize>,
|
||||||
@ -692,7 +676,7 @@ impl StateResolution {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add_event_and_auth_chain_to_graph(
|
fn add_event_and_auth_chain_to_graph(
|
||||||
&mut self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
graph: &mut BTreeMap<EventId, Vec<EventId>>,
|
graph: &mut BTreeMap<EventId, Vec<EventId>>,
|
||||||
event_id: &EventId,
|
event_id: &EventId,
|
||||||
@ -726,7 +710,7 @@ impl StateResolution {
|
|||||||
|
|
||||||
/// TODO update self if we go that route just as event_map will be updated
|
/// TODO update self if we go that route just as event_map will be updated
|
||||||
fn _get_event(
|
fn _get_event(
|
||||||
&mut self,
|
&self,
|
||||||
_room_id: &RoomId,
|
_room_id: &RoomId,
|
||||||
ev_id: &EventId,
|
ev_id: &EventId,
|
||||||
event_map: &mut EventMap<StateEvent>,
|
event_map: &mut EventMap<StateEvent>,
|
||||||
|
@ -34,7 +34,7 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
|
|||||||
.init()
|
.init()
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let store = TestStore(RefCell::new(
|
let store = TestStore(RefCell::new(
|
||||||
INITIAL_EVENTS()
|
INITIAL_EVENTS()
|
||||||
@ -568,7 +568,7 @@ fn ban_with_auth_chains() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn base_with_auth_chains() {
|
fn base_with_auth_chains() {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let store = TestStore(RefCell::new(INITIAL_EVENTS()));
|
let store = TestStore(RefCell::new(INITIAL_EVENTS()));
|
||||||
|
|
||||||
@ -608,7 +608,7 @@ fn base_with_auth_chains() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ban_with_auth_chains2() {
|
fn ban_with_auth_chains2() {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let init = INITIAL_EVENTS();
|
let init = INITIAL_EVENTS();
|
||||||
let ban = BAN_STATE_SET();
|
let ban = BAN_STATE_SET();
|
||||||
|
@ -278,7 +278,7 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
|
|||||||
.init()
|
.init()
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let store = TestStore(RefCell::new(
|
let store = TestStore(RefCell::new(
|
||||||
INITIAL_EVENTS()
|
INITIAL_EVENTS()
|
||||||
@ -691,7 +691,7 @@ fn topic_setting() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_event_map_none() {
|
fn test_event_map_none() {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let store = TestStore(RefCell::new(btreemap! {}));
|
let store = TestStore(RefCell::new(btreemap! {}));
|
||||||
|
|
||||||
@ -715,7 +715,7 @@ fn test_event_map_none() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lexicographical_sort() {
|
fn test_lexicographical_sort() {
|
||||||
let mut resolver = StateResolution::default();
|
let resolver = StateResolution::default();
|
||||||
|
|
||||||
let graph = btreemap! {
|
let graph = btreemap! {
|
||||||
event_id("l") => vec![event_id("o")],
|
event_id("l") => vec![event_id("o")],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user