From c4f9ef2e2e6f4467fdfab0b167516d91f189288b Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 27 May 2025 09:04:06 +0200 Subject: [PATCH] refactor(base): Improve `dispatch_and_get_new_users`. This patch renames `dispatch_and_get_new_users` to `dispatch`. This response processor no longer returns the new user IDs, but they are written in a mutable reference argument. This argument is typed by a new private trait: `NewUsers`. This trait is implemented on `BTreeSet` and on `()`. It helps to avoid allocations when the new users are not needed. --- .../response_processors/room/msc4186/mod.rs | 12 +++++- .../src/response_processors/room/sync_v2.rs | 16 +++++--- .../src/response_processors/state_events.rs | 38 +++++++++++++------ 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/crates/matrix-sdk-base/src/response_processors/room/msc4186/mod.rs b/crates/matrix-sdk-base/src/response_processors/room/msc4186/mod.rs index da506d21a..885a87d00 100644 --- a/crates/matrix-sdk-base/src/response_processors/room/msc4186/mod.rs +++ b/crates/matrix-sdk-base/src/response_processors/room/msc4186/mod.rs @@ -15,6 +15,8 @@ pub mod extensions; use std::collections::BTreeMap; +#[cfg(feature = "e2e-encryption")] +use std::collections::BTreeSet; #[cfg(feature = "e2e-encryption")] use matrix_sdk_common::deserialized_responses::TimelineEvent; @@ -104,12 +106,18 @@ pub async fn update_any_room( room_info.mark_state_partially_synced(); room_info.handle_encryption_state(requested_required_states.for_room(room_id)); - #[cfg_attr(not(feature = "e2e-encryption"), allow(unused))] - let new_user_ids = state_events::sync::dispatch_and_get_new_users( + #[cfg(feature = "e2e-encryption")] + let mut new_user_ids = BTreeSet::new(); + + #[cfg(not(feature = "e2e-encryption"))] + let mut new_user_ids = (); + + state_events::sync::dispatch( context, (&raw_state_events, &state_events), &mut room_info, ambiguity_cache, + &mut new_user_ids, ) .await?; diff --git a/crates/matrix-sdk-base/src/response_processors/room/sync_v2.rs b/crates/matrix-sdk-base/src/response_processors/room/sync_v2.rs index c1ad5ff15..b2941558a 100644 --- a/crates/matrix-sdk-base/src/response_processors/room/sync_v2.rs +++ b/crates/matrix-sdk-base/src/response_processors/room/sync_v2.rs @@ -63,11 +63,14 @@ pub async fn update_joined_room( let (raw_state_events, state_events) = state_events::sync::collect(&joined_room.state.events); - let mut new_user_ids = state_events::sync::dispatch_and_get_new_users( + let mut new_user_ids = BTreeSet::new(); + + state_events::sync::dispatch( context, (&raw_state_events, &state_events), &mut room_info, ambiguity_cache, + &mut new_user_ids, ) .await?; @@ -80,14 +83,15 @@ pub async fn update_joined_room( let (raw_state_events_from_timeline, state_events_from_timeline) = state_events::sync::collect_from_timeline(&joined_room.timeline.events); - let mut other_new_user_ids = state_events::sync::dispatch_and_get_new_users( + state_events::sync::dispatch( context, (&raw_state_events_from_timeline, &state_events_from_timeline), &mut room_info, ambiguity_cache, + &mut new_user_ids, ) .await?; - new_user_ids.append(&mut other_new_user_ids); + updated_members_in_room.insert(room_id.to_owned(), new_user_ids.clone()); #[cfg(feature = "e2e-encryption")] @@ -175,22 +179,24 @@ pub async fn update_left_room( let (raw_state_events, state_events) = state_events::sync::collect(&left_room.state.events); - let _ = state_events::sync::dispatch_and_get_new_users( + state_events::sync::dispatch( context, (&raw_state_events, &state_events), &mut room_info, ambiguity_cache, + &mut (), ) .await?; let (raw_state_events_from_timeline, state_events_from_timeline) = state_events::sync::collect_from_timeline(&left_room.timeline.events); - let _ = state_events::sync::dispatch_and_get_new_users( + state_events::sync::dispatch( context, (&raw_state_events_from_timeline, &state_events_from_timeline), &mut room_info, ambiguity_cache, + &mut (), ) .await?; diff --git a/crates/matrix-sdk-base/src/response_processors/state_events.rs b/crates/matrix-sdk-base/src/response_processors/state_events.rs index 5d04a63af..13ff0d70e 100644 --- a/crates/matrix-sdk-base/src/response_processors/state_events.rs +++ b/crates/matrix-sdk-base/src/response_processors/state_events.rs @@ -24,7 +24,7 @@ pub mod sync { use ruma::{ events::{room::member::MembershipState, AnySyncTimelineEvent}, - OwnedUserId, + OwnedUserId, UserId, }; use tracing::instrument; @@ -57,25 +57,23 @@ pub mod sync { })) } - /// Dispatch the sync state events and return the new users for this room. + /// Dispatch the sync state events. /// /// `raw_events` and `events` must be generated from [`collect`]. /// Events must be exactly the same list of events that are in /// `raw_events`, but deserialised. We demand them here to avoid /// deserialising multiple times. + /// + /// The `new_users` mutable reference allows to collect the new users for + /// this room. #[instrument(skip_all, fields(room_id = ?room_info.room_id))] - pub async fn dispatch_and_get_new_users( + pub async fn dispatch( context: &mut Context, (raw_events, events): (&[Raw], &[AnySyncStateEvent]), room_info: &mut RoomInfo, ambiguity_cache: &mut AmbiguityCache, - ) -> StoreResult> { - let mut user_ids = BTreeSet::new(); - - if raw_events.is_empty() { - return Ok(user_ids); - } - + new_users: &mut U, + ) -> StoreResult<()> { for (raw_event, event) in iter::zip(raw_events, events) { room_info.handle_state_event(event); @@ -86,7 +84,7 @@ pub mod sync { match member.membership() { MembershipState::Join | MembershipState::Invite => { - user_ids.insert(member.state_key().to_owned()); + new_users.insert(member.state_key()); } _ => (), } @@ -104,7 +102,23 @@ pub mod sync { .insert(event.state_key().to_owned(), raw_event.clone()); } - Ok(user_ids) + Ok(()) + } + + /// A trait to collect new users in [`dispatch`]. + trait NewUsers { + /// Insert a new user in the collection of new users. + fn insert(&mut self, user_id: &UserId); + } + + impl NewUsers for BTreeSet { + fn insert(&mut self, user_id: &UserId) { + self.insert(user_id.to_owned()); + } + } + + impl NewUsers for () { + fn insert(&mut self, _user_id: &UserId) {} } }