perf: Remove huge clone and tackle TODOs

This commit is contained in:
timedout
2026-06-27 20:21:35 +01:00
committed by Jade Ellis
parent 00a9ef1c06
commit 3b8788d5d2
2 changed files with 12 additions and 14 deletions
@@ -35,7 +35,7 @@
/// them in a processable order, so this is just a best effort attempt. It does
/// not account for power levels or other tie breaks.
pub async fn build_local_dag<S: std::hash::BuildHasher + Send + Sync>(
pdu_map: &HashMap<OwnedEventId, CanonicalJsonObject, S>,
pdu_map: &HashMap<OwnedEventId, &CanonicalJsonObject, S>,
) -> conduwuit::Result<Vec<OwnedEventId>> {
debug_assert!(pdu_map.len() >= 2, "needless call to build_local_dag with less than 2 PDUs");
let mut dag: HashMap<OwnedEventId, HashSet<OwnedEventId>> =
@@ -237,7 +237,6 @@ pub(super) async fn fetch_and_handle_missing_events<'a, Pdu>(
}
// Insert this PDU back at the end of the queue so that it will be resolved once
// all of its auth events have been fetched.
// TODO: This may result in infinite looping, needs a breaker
if have_all_auth {
debug!(elapsed=?start.elapsed(),%next_id, "Have all auth events");
discovered_events.insert(next_id, value);
@@ -251,12 +250,13 @@ pub(super) async fn fetch_and_handle_missing_events<'a, Pdu>(
}
}
let seeded_ordered = build_local_dag(
&discovered_events.clone(), /* TODO: this clones like several megabytes of data,
* owie :( */
)
.await
.expect("failed to build local DAG");
let refmap: HashMap<OwnedEventId, &CanonicalJsonObject> = discovered_events
.iter()
.map(|(id, data)| (id.clone(), data))
.collect();
let seeded_ordered = build_local_dag(&refmap)
.await
.expect("failed to build local DAG");
let mut pdus = HashMap::with_capacity(seeded_ordered.len());
for id in seeded_ordered {
let pdu_json = discovered_events.remove(&id).unwrap();
@@ -279,10 +279,6 @@ pub(super) async fn fetch_and_handle_missing_events<'a, Pdu>(
| Err(e) =>
warn!(elapsed=?start.elapsed(),"Authentication of event {id} failed: {e:?}"),
}
// TODO: should this try to promote to timeline?
// If we got here, we probably weren't able to promote it before
// now.
}
trace!(elapsed=?start.elapsed(),"Finished fetch_and_handle_missing_events: fetched and handled {} missing PDUs", pdus.len());
@@ -5,7 +5,7 @@
utils::{BoolExt, IterStream, stream::BroadbandExt},
};
use futures::StreamExt;
use ruma::{RoomId, ServerName};
use ruma::{CanonicalJsonObject, OwnedEventId, RoomId, ServerName};
use crate::rooms::event_handler::build_local_dag;
@@ -78,7 +78,9 @@ pub(super) async fn fetch_prevs(
let to_persist = if mapped.len() <= 1 {
mapped.keys().map(ToOwned::to_owned).collect()
} else {
build_local_dag(&mapped).await?
let refmap: HashMap<OwnedEventId, &CanonicalJsonObject> =
mapped.iter().map(|(id, data)| (id.clone(), data)).collect();
build_local_dag(&refmap).await?
};
let job_start = Instant::now();