From 757d38e8912278b21c0f8cedac3bb2dcad6e8565 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 28 Aug 2025 05:58:06 +0000 Subject: [PATCH] Optimize expected auth types checking. Signed-off-by: Jason Volk --- src/core/matrix/state_res/event_auth.rs | 35 ++++++++++++------------- src/service/rooms/timeline/append.rs | 3 +++ src/service/rooms/timeline/build.rs | 1 - 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/core/matrix/state_res/event_auth.rs b/src/core/matrix/state_res/event_auth.rs index 949887e2..60c7849c 100644 --- a/src/core/matrix/state_res/event_auth.rs +++ b/src/core/matrix/state_res/event_auth.rs @@ -3,10 +3,8 @@ mod room_member; #[cfg(test)] mod tests; -use std::collections::HashSet; - use futures::{ - TryFutureExt, TryStreamExt, + FutureExt, TryFutureExt, TryStreamExt, future::{join3, try_join}, }; use ruma::{ @@ -107,20 +105,17 @@ where return check_room_create(&room_create_event, &rules.authorization); } - let expected_auth_types: HashSet<_> = auth_types_for_event( + let expected_auth_types = auth_types_for_event( incoming_event.event_type(), incoming_event.sender(), incoming_event.state_key(), incoming_event.content(), &rules.authorization, false, - )? - .into_iter() - .collect(); - - let room_id = incoming_event.room_id(); + )?; // Since v1, considering auth_events: + let seen_auth_types = Vec::with_capacity(expected_auth_types.len()); let seen_auth_types = incoming_event .auth_events() .try_stream() @@ -128,11 +123,11 @@ where fetch_event(event_id.to_owned()) .map_err(|_| err!(Request(NotFound("failed to find auth event")))) }) - .ready_try_fold_default(|mut seen_auth_types: HashSet, auth_event| { + .ready_try_fold(seen_auth_types, |mut seen_auth_types, auth_event| { let event_id = auth_event.event_id(); // The auth event must be in the same room as the incoming event. - if auth_event.room_id() != room_id { + if auth_event.room_id() != incoming_event.room_id() { return Err!("auth event {event_id} not in the same room"); } @@ -141,7 +136,7 @@ where .ok_or_else(|| err!("auth event {event_id} has no `state_key`"))?; let event_type = auth_event.event_type(); - let key = (event_type.to_cow_str().into(), state_key.into()); + let key: TypeStateKey = (event_type.to_cow_str().into(), state_key.into()); // Since v1, if there are duplicate entries for a given type and state_key pair, // reject. @@ -166,7 +161,7 @@ where return Err!("rejected auth event {event_id}"); } - seen_auth_types.insert(key); + seen_auth_types.push(key); Ok(seen_auth_types) }) .await?; @@ -188,11 +183,14 @@ where .authorization .room_create_event_id_as_room_id { - let room_create_event_id = room_id.as_event_id().map_err(|e| { - err!(Request(InvalidParam( - "could not construct `m.room.create` event ID from room ID: {e}" - ))) - })?; + let room_create_event_id = incoming_event + .room_id() + .as_event_id() + .map_err(|e| { + err!(Request(InvalidParam( + "could not construct `m.room.create` event ID from room ID: {e}" + ))) + })?; let Ok(room_create_event) = fetch_event(room_create_event_id.clone()).await else { return Err!(Request(NotFound( @@ -307,6 +305,7 @@ where &room_create_event, fetch_state, ) + .boxed() .await; } diff --git a/src/service/rooms/timeline/append.rs b/src/service/rooms/timeline/append.rs index f0f3a8bf..43001018 100644 --- a/src/service/rooms/timeline/append.rs +++ b/src/service/rooms/timeline/append.rs @@ -293,6 +293,7 @@ where .state .get_room_version(pdu.room_id()) .await?; + match room_version_id { | V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 => { if let Some(redact_id) = pdu.redacts() { @@ -434,6 +435,7 @@ where self.services .sending .send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?; + continue; } @@ -450,6 +452,7 @@ where self.services .sending .send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?; + continue; } } diff --git a/src/service/rooms/timeline/build.rs b/src/service/rooms/timeline/build.rs index 37fa1043..62226afc 100644 --- a/src/service/rooms/timeline/build.rs +++ b/src/service/rooms/timeline/build.rs @@ -132,7 +132,6 @@ pub async fn build_and_append_pdu( once(pdu.event_id()), state_lock, ) - .boxed() .await?; // We set the room state after inserting the pdu, so that we never have a moment