Skip to content

Commit 339d6b2

Browse files
committed
base: Box large RoomInfo fields
RoomInfo is often passed around by value, including in futures where holding types with a large stack size is especially problematic¹. It might make sense to move the actual data of (Base)RoomInfo into an inner struct that is always held inside a Box or Arc, but this change should have most of the benefits of that while being a bit simpler. ¹ rust-lang/rust#69826
1 parent ab4c524 commit 339d6b2

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

crates/matrix-sdk-base/src/client.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,10 @@ impl BaseClient {
620620
/// decrypted event if we found one, along with its index in the
621621
/// latest_encrypted_events list, or None if we didn't find one.
622622
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
623-
async fn decrypt_latest_suitable_event(&self, room: &Room) -> Option<(LatestEvent, usize)> {
623+
async fn decrypt_latest_suitable_event(
624+
&self,
625+
room: &Room,
626+
) -> Option<(Box<LatestEvent>, usize)> {
624627
let enc_events = room.latest_encrypted_events();
625628

626629
// Walk backwards through the encrypted events, looking for one we can decrypt
@@ -633,7 +636,7 @@ impl BaseClient {
633636
is_suitable_for_latest_event(&any_sync_event)
634637
{
635638
// The event is the right type for us to use as latest_event
636-
return Some((LatestEvent::new(decrypted), i));
639+
return Some((Box::new(LatestEvent::new(decrypted)), i));
637640
}
638641
}
639642
}

crates/matrix-sdk-base/src/rooms/normal.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ impl Room {
403403
/// sliding sync.
404404
#[cfg(feature = "experimental-sliding-sync")]
405405
pub fn latest_event(&self) -> Option<LatestEvent> {
406-
self.inner.read().latest_event.clone()
406+
self.inner.read().latest_event.as_deref().cloned()
407407
}
408408

409409
/// Update the last event in the room
410410
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
411-
pub(crate) fn set_latest_event(&self, latest_event: Option<LatestEvent>) {
411+
pub(crate) fn set_latest_event(&self, latest_event: Option<Box<LatestEvent>>) {
412412
self.inner.update(|info| info.latest_event = latest_event);
413413
}
414414

@@ -429,7 +429,7 @@ impl Room {
429429
/// Panics if index is not a valid index in the latest_encrypted_events
430430
/// list.
431431
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
432-
pub(crate) fn on_latest_event_decrypted(&self, latest_event: LatestEvent, index: usize) {
432+
pub(crate) fn on_latest_event_decrypted(&self, latest_event: Box<LatestEvent>, index: usize) {
433433
self.set_latest_event(Some(latest_event));
434434
self.latest_encrypted_events.write().unwrap().drain(0..=index);
435435
}
@@ -732,10 +732,10 @@ pub struct RoomInfo {
732732
pub(crate) encryption_state_synced: bool,
733733
/// The last event send by sliding sync
734734
#[cfg(feature = "experimental-sliding-sync")]
735-
pub(crate) latest_event: Option<LatestEvent>,
735+
pub(crate) latest_event: Option<Box<LatestEvent>>,
736736
/// Base room info which holds some basic event contents important for the
737737
/// room state.
738-
pub(crate) base_info: BaseRoomInfo,
738+
pub(crate) base_info: Box<BaseRoomInfo>,
739739
}
740740

741741
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
@@ -769,7 +769,7 @@ impl RoomInfo {
769769
encryption_state_synced: false,
770770
#[cfg(feature = "experimental-sliding-sync")]
771771
latest_event: None,
772-
base_info: BaseRoomInfo::new(),
772+
base_info: Box::new(BaseRoomInfo::new()),
773773
}
774774
}
775775

@@ -1246,10 +1246,10 @@ mod tests {
12461246
last_prev_batch: Some("pb".to_owned()),
12471247
sync_info: SyncInfo::FullySynced,
12481248
encryption_state_synced: true,
1249-
latest_event: Some(LatestEvent::new(
1249+
latest_event: Some(Box::new(LatestEvent::new(
12501250
Raw::from_json_string(json!({"sender": "@u:i.uk"}).to_string()).unwrap().into(),
1251-
)),
1252-
base_info: BaseRoomInfo::new(),
1251+
))),
1252+
base_info: Box::new(BaseRoomInfo::new()),
12531253
};
12541254

12551255
let info_json = json!({
@@ -1698,10 +1698,10 @@ mod tests {
16981698
}
16991699

17001700
#[cfg(feature = "experimental-sliding-sync")]
1701-
fn make_latest_event(event_id: &str) -> LatestEvent {
1702-
LatestEvent::new(SyncTimelineEvent::new(
1701+
fn make_latest_event(event_id: &str) -> Box<LatestEvent> {
1702+
Box::new(LatestEvent::new(SyncTimelineEvent::new(
17031703
Raw::from_json_string(json!({ "event_id": event_id }).to_string()).unwrap(),
1704-
))
1704+
)))
17051705
}
17061706

17071707
fn timestamp(minutes_ago: u32) -> MilliSecondsSinceUnixEpoch {

crates/matrix-sdk-base/src/sliding_sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ async fn cache_latest_events(
524524
}
525525
}
526526

527-
let latest_event = LatestEvent::new_with_sender_details(
527+
let latest_event = Box::new(LatestEvent::new_with_sender_details(
528528
event.clone(),
529529
sender_profile,
530530
sender_name_is_ambiguous,
531-
);
531+
));
532532

533533
// Store it in the return RoomInfo, and in the Room, to make sure they are
534534
// consistent

crates/matrix-sdk-base/src/store/migration_helpers.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl RoomInfoV1 {
117117
sync_info,
118118
encryption_state_synced,
119119
#[cfg(feature = "experimental-sliding-sync")]
120-
latest_event: latest_event.map(LatestEvent::new),
120+
latest_event: latest_event.map(|ev| Box::new(LatestEvent::new(ev))),
121121
base_info: base_info.migrate(create),
122122
}
123123
}
@@ -157,7 +157,10 @@ struct BaseRoomInfoV1 {
157157

158158
impl BaseRoomInfoV1 {
159159
/// Migrate this to a [`BaseRoomInfo`].
160-
fn migrate(self, create: Option<&SyncOrStrippedState<RoomCreateEventContent>>) -> BaseRoomInfo {
160+
fn migrate(
161+
self,
162+
create: Option<&SyncOrStrippedState<RoomCreateEventContent>>,
163+
) -> Box<BaseRoomInfo> {
161164
let BaseRoomInfoV1 {
162165
avatar,
163166
canonical_alias,
@@ -186,7 +189,7 @@ impl BaseRoomInfoV1 {
186189
MinimalStateEvent::Redacted(ev) => MinimalStateEvent::Redacted(ev),
187190
});
188191

189-
BaseRoomInfo {
192+
Box::new(BaseRoomInfo {
190193
avatar,
191194
canonical_alias,
192195
create,
@@ -200,7 +203,7 @@ impl BaseRoomInfoV1 {
200203
tombstone,
201204
topic,
202205
rtc_member: BTreeMap::new(),
203-
}
206+
})
204207
}
205208
}
206209

0 commit comments

Comments
 (0)