perf: Change signature of build_local_dag to remove refmap hacks

This commit is contained in:
timedout committed 2026-07-11 17:27:42 +01:00
1 parent 70a3905761
commit 64a08262ca
5 files changed
+9 -26

No files matched your search

+1 -5
View File
@@ -306,11 +306,7 @@ async fn handle_room(
// failure (e.g., cycles). This is best-effort; proper ordering is the sender's // failure (e.g., cycles). This is best-effort; proper ordering is the sender's
// responsibility. // responsibility.
let sorted_event_ids = if pdu_map.len() >= 2 { let sorted_event_ids = if pdu_map.len() >= 2 {
let refmap = pdu_map build_local_dag(&pdu_map, DagBuilderTree::PrevEvents)
.iter()
.map(|(event_id, obj)| (event_id.clone(), obj))
.collect();
build_local_dag(&refmap, DagBuilderTree::PrevEvents)
.await .await
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
debug_warn!("Failed to build local DAG for room {room_id}: {e}"); debug_warn!("Failed to build local DAG for room {room_id}: {e}");
@@ -42,7 +42,7 @@ pub enum DagBuilderTree {
/// not account for power levels or other tie breaks. /// not account for power levels or other tie breaks.
#[allow(clippy::implicit_hasher)] #[allow(clippy::implicit_hasher)]
pub async fn build_local_dag( pub async fn build_local_dag(
pdu_map: &HashMap<OwnedEventId, &CanonicalJsonObject>, pdu_map: &HashMap<OwnedEventId, CanonicalJsonObject>,
tree: DagBuilderTree, tree: DagBuilderTree,
) -> Result<Vec<OwnedEventId>> { ) -> Result<Vec<OwnedEventId>> {
debug_assert!(pdu_map.len() >= 2, "needless call to build_local_dag with less than 2 PDUs"); debug_assert!(pdu_map.len() >= 2, "needless call to build_local_dag with less than 2 PDUs");
@@ -538,11 +538,7 @@ impl super::Service {
} }
} }
let refmap: HashMap<OwnedEventId, &CanonicalJsonObject> = discovered_events let seeded_ordered = build_local_dag(&discovered_events, DagBuilderTree::AuthEvents)
.iter()
.map(|(id, data)| (id.clone(), data))
.collect();
let seeded_ordered = build_local_dag(&refmap, DagBuilderTree::AuthEvents)
.await .await
.expect("failed to build local DAG"); .expect("failed to build local DAG");
let mut pdus = HashMap::with_capacity(seeded_ordered.len()); let mut pdus = HashMap::with_capacity(seeded_ordered.len());
+3 -10
View File
@@ -2,8 +2,7 @@ use std::collections::{HashMap, hash_map};
use conduwuit::{Err, Event, EventTypeExt, PduEvent, Result, err, warn}; use conduwuit::{Err, Event, EventTypeExt, PduEvent, Result, err, warn};
use ruma::{ use ruma::{
CanonicalJsonObject, OwnedEventId, ServerName, OwnedEventId, ServerName, api::federation::authorization::get_event_authorization,
api::federation::authorization::get_event_authorization,
room_version_rules::RoomVersionRules, room_version_rules::RoomVersionRules,
}; };
use tokio::join; use tokio::join;
@@ -126,17 +125,11 @@ impl super::Service {
where where
Pdu: Event + Send + Sync, Pdu: Event + Send + Sync,
{ {
// TODO(nex): build_local_dag's signature needs changing because all callsites
// seem to do this refmap hack.
let pdu_objects = auth_chain_map let pdu_objects = auth_chain_map
.iter() .iter()
.map(|(event_id, pdu)| (event_id, pdu.to_canonical_object())) .map(|(event_id, pdu)| (event_id.clone(), pdu.to_canonical_object()))
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
let refmap: HashMap<OwnedEventId, &CanonicalJsonObject> = pdu_objects let auth_chain_topo = build_local_dag(&pdu_objects, DagBuilderTree::AuthEvents)
.iter()
.map(|(id, data)| ((*id).to_owned(), data))
.collect();
let auth_chain_topo = build_local_dag(&refmap, DagBuilderTree::AuthEvents)
.await? .await?
.into_iter() .into_iter()
.map(|event_id| (event_id.clone(), auth_chain_map.get(&event_id).unwrap().to_owned())) .map(|event_id| (event_id.clone(), auth_chain_map.get(&event_id).unwrap().to_owned()))
@@ -5,7 +5,7 @@ use conduwuit::{
utils::{BoolExt, IterStream, stream::BroadbandExt}, utils::{BoolExt, IterStream, stream::BroadbandExt},
}; };
use futures::StreamExt; use futures::StreamExt;
use ruma::{CanonicalJsonObject, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, ServerName}; use ruma::{MilliSecondsSinceUnixEpoch, RoomId, ServerName};
use crate::rooms::event_handler::{build_local_dag, fetch_and_handle_outliers::DagBuilderTree}; use crate::rooms::event_handler::{build_local_dag, fetch_and_handle_outliers::DagBuilderTree};
@@ -87,9 +87,7 @@ impl super::Service {
let to_persist = if mapped.len() <= 1 { let to_persist = if mapped.len() <= 1 {
mapped.keys().map(ToOwned::to_owned).collect() mapped.keys().map(ToOwned::to_owned).collect()
} else { } else {
let refmap: HashMap<OwnedEventId, &CanonicalJsonObject> = build_local_dag(&mapped, DagBuilderTree::PrevEvents).await?
mapped.iter().map(|(id, data)| (id.clone(), data)).collect();
build_local_dag(&refmap, DagBuilderTree::PrevEvents).await?
}; };
let job_start = Instant::now(); let job_start = Instant::now();
+1 -1
View File
@@ -20,7 +20,7 @@ pub use fetch_and_handle_outliers::{
DagBuilderTree, GET_MISSING_EVENTS_MAX_BATCH_SIZE, build_local_dag, DagBuilderTree, GET_MISSING_EVENTS_MAX_BATCH_SIZE, build_local_dag,
}; };
use ruma::{ use ruma::{
OwnedEventId, OwnedRoomId, RoomId, events::room::create::RoomCreateEventContent, OwnedEventId, OwnedRoomId, events::room::create::RoomCreateEventContent,
room_version_rules::RoomVersionRules, room_version_rules::RoomVersionRules,
}; };
use tokio::sync::{Notify, mpsc}; use tokio::sync::{Notify, mpsc};