Skip to content

Commit

Permalink
fixup!: don't use StateChanges to keep the pinned_events list, us…
Browse files Browse the repository at this point in the history
…e `Vec::extend` and `Vec::extend_from_slice` instead of cloning
  • Loading branch information
jmartinesp committed Jul 22, 2024
1 parent 1750b00 commit 8c43c75
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
36 changes: 23 additions & 13 deletions crates/matrix-sdk-base/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use ruma::{
},
push::{Action, PushConditionRoomCtx, Ruleset},
serde::Raw,
OwnedRoomId, OwnedUserId, RoomId, RoomVersionId, UInt, UserId,
OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, RoomVersionId, UInt, UserId,
};
use tokio::sync::{broadcast, Mutex};
#[cfg(feature = "e2e-encryption")]
Expand Down Expand Up @@ -530,6 +530,7 @@ impl BaseClient {
room_info: &mut RoomInfo,
changes: &mut StateChanges,
ambiguity_cache: &mut AmbiguityCache,
pinned_events: &mut Vec<OwnedEventId>,
) -> StoreResult<BTreeSet<OwnedUserId>> {
let mut state_events = BTreeMap::new();
let mut user_ids = BTreeSet::new();
Expand All @@ -552,10 +553,9 @@ impl BaseClient {
handle_room_member_event_for_profiles(&room_info.room_id, member, changes);
} else if let AnySyncStateEvent::RoomPinnedEvents(
SyncRoomPinnedEventsEvent::Original(ev),
) = &event
) = event
{
let new_pinned_event_ids = ev.content.pinned.clone();
changes.pinned_events.insert(room_info.room_id.to_owned(), new_pinned_event_ids);
pinned_events.extend_from_slice(&ev.content.pinned);
}

state_events
Expand Down Expand Up @@ -921,6 +921,7 @@ impl BaseClient {
let state_events = Self::deserialize_state_events(&new_info.state.events);
let (raw_state_events, state_events): (Vec<_>, Vec<_>) =
state_events.into_iter().unzip();
let mut pinned_events = Vec::new();

let mut user_ids = self
.handle_state(
Expand All @@ -929,9 +930,17 @@ impl BaseClient {
&mut room_info,
&mut changes,
&mut ambiguity_cache,
&mut pinned_events,
)
.await?;

if !pinned_events.is_empty() {
room.pinned_event_ids.update(|ids| {
ids.clear();
ids.extend(pinned_events);
});
}

for raw in &new_info.ephemeral.events {
match raw.deserialize() {
Ok(AnySyncEphemeralRoomEvent::Receipt(event)) => {
Expand Down Expand Up @@ -1037,13 +1046,16 @@ impl BaseClient {
let (raw_state_events, state_events): (Vec<_>, Vec<_>) =
state_events.into_iter().unzip();

let mut pinned_events = Vec::new();

let mut user_ids = self
.handle_state(
&raw_state_events,
&state_events,
&mut room_info,
&mut changes,
&mut ambiguity_cache,
&mut pinned_events,
)
.await?;

Expand All @@ -1062,6 +1074,13 @@ impl BaseClient {
)
.await?;

if !pinned_events.is_empty() {
room.pinned_event_ids.update(|ids| {
ids.clear();
ids.extend(pinned_events);
});
}

// Save the new `RoomInfo`.
changes.add_room(room_info);

Expand Down Expand Up @@ -1180,15 +1199,6 @@ impl BaseClient {
}
}

for (room_id, pinned_event_ids) in changes.pinned_events.clone() {
if let Some(room) = self.store.room(&room_id) {
room.pinned_event_ids.update(|ids| {
ids.clear();
ids.append(&mut pinned_event_ids.clone());
});
}
}

for (room_id, room_info) in &changes.room_infos {
if let Some(room) = self.store.room(room_id) {
let room_info_notable_update_reasons =
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub struct Room {

/// The list of pinned event ids in this room in an observable format so
/// they can be subscribed to.
pub pinned_event_ids: SharedObservable<Vec<OwnedEventId>>,
pub(crate) pinned_event_ids: SharedObservable<Vec<OwnedEventId>>,
}

/// The room summary containing member counts and members that should be used to
Expand Down
9 changes: 9 additions & 0 deletions crates/matrix-sdk-base/src/sliding_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,28 @@ impl BaseClient {

room_info.mark_state_partially_synced();

let mut pinned_event_ids = Vec::new();
let mut user_ids = if !state_events.is_empty() {
self.handle_state(
&raw_state_events,
&state_events,
&mut room_info,
changes,
ambiguity_cache,
&mut pinned_event_ids,
)
.await?
} else {
Default::default()
};

if !pinned_event_ids.is_empty() {
room.pinned_event_ids.update(|ids| {
ids.clear();
ids.extend(pinned_event_ids);
});
}

let push_rules = self.get_push_rules(changes).await?;

if let Some(invite_state) = &room_data.invite_state {
Expand Down
3 changes: 0 additions & 3 deletions crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,6 @@ pub struct StateChanges {
/// A map from room id to a map of a display name and a set of user ids that
/// share that display name in the given room.
pub ambiguity_maps: BTreeMap<OwnedRoomId, BTreeMap<String, BTreeSet<OwnedUserId>>>,

/// A map of `RoomId` to pinned `OwnedEventId`s for each room.
pub pinned_events: BTreeMap<OwnedRoomId, Vec<OwnedEventId>>,
}

impl StateChanges {
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk-sqlite/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,6 @@ impl StateStore for SqliteStateStore {
redactions,
stripped_state,
ambiguity_maps,
pinned_events: _,
} = changes;

if let Some(sync_token) = sync_token {
Expand Down

0 comments on commit 8c43c75

Please sign in to comment.