From 7bd042796647b080408d404dfebf09716c3e4d93 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 1 Jul 2024 14:48:26 +0200 Subject: [PATCH] chore(ui): Remove the `RoomListService::rooms` cache. This patch removes the `RoomListService::rooms` cache, since now a `Room` is pretty cheap to build. This cache was also used to keep the `Timeline` alive, but it's now recommended that the consumer of the `Room` keeps its own clone of the `Timeline` somewhere. We may introduce a cache inside `RoomListService` for the `Timeline` later. --- .../src/room_list_service/mod.rs | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/mod.rs b/crates/matrix-sdk-ui/src/room_list_service/mod.rs index b52e902eb2a..0ab0fb4399b 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/mod.rs @@ -59,11 +59,7 @@ mod room_list; pub mod sorters; mod state; -use std::{ - num::NonZeroUsize, - sync::{Arc, Mutex}, - time::Duration, -}; +use std::{sync::Arc, time::Duration}; use async_stream::stream; use eyeball::{SharedObservable, Subscriber}; @@ -72,7 +68,6 @@ use matrix_sdk::{ event_cache::EventCacheError, Client, Error as SlidingSyncError, SlidingSync, SlidingSyncList, SlidingSyncMode, }; -use matrix_sdk_base::ring_buffer::RingBuffer; pub use room::*; pub use room_list::*; use ruma::{ @@ -103,20 +98,9 @@ pub struct RoomListService { /// /// `RoomListService` is a simple state-machine. state: SharedObservable, - - /// Room cache, to avoid recreating `Room`s every time users fetch them. - rooms: Arc>>, } impl RoomListService { - /// Size of the room's ring buffer. - /// - /// This number should be high enough so that navigating to a room - /// previously visited is almost instant, but also not too high so as to - /// avoid exhausting memory. - // SAFETY: `new_unchecked` is safe because 128 is not zero. - const ROOM_OBJECT_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(128) }; - /// Create a new `RoomList`. /// /// A [`matrix_sdk::SlidingSync`] client will be created, with a cached list @@ -200,12 +184,7 @@ impl RoomListService { // Eagerly subscribe the event cache to sync responses. client.event_cache().subscribe()?; - Ok(Self { - client, - sliding_sync, - state: SharedObservable::new(State::Init), - rooms: Arc::new(Mutex::new(RingBuffer::new(Self::ROOM_OBJECT_CACHE_SIZE))), - }) + Ok(Self { client, sliding_sync, state: SharedObservable::new(State::Init) }) } /// Start to sync the room list. @@ -403,21 +382,10 @@ impl RoomListService { /// Get a [`Room`] if it exists. pub fn room(&self, room_id: &RoomId) -> Result { - let mut rooms = self.rooms.lock().unwrap(); - - if let Some(room) = rooms.iter().rfind(|room| room.id() == room_id) { - return Ok(room.clone()); - } - - let room = Room::new( + Ok(Room::new( self.client.get_room(room_id).ok_or_else(|| Error::RoomNotFound(room_id.to_owned()))?, &self.sliding_sync, - ); - - // Save for later. - rooms.push(room.clone()); - - Ok(room) + )) } #[cfg(test)]