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<OwnedUserId>` and on `()`. It helps to avoid allocations when
the new users are not needed.
This commit is contained in:
Ivan Enderlin
2025-05-27 09:04:06 +02:00
parent acce75a6dc
commit c4f9ef2e2e
3 changed files with 47 additions and 19 deletions

View File

@@ -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?;

View File

@@ -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?;

View File

@@ -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<U: NewUsers>(
context: &mut Context,
(raw_events, events): (&[Raw<AnySyncStateEvent>], &[AnySyncStateEvent]),
room_info: &mut RoomInfo,
ambiguity_cache: &mut AmbiguityCache,
) -> StoreResult<BTreeSet<OwnedUserId>> {
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<OwnedUserId> {
fn insert(&mut self, user_id: &UserId) {
self.insert(user_id.to_owned());
}
}
impl NewUsers for () {
fn insert(&mut self, _user_id: &UserId) {}
}
}