Skip to content

Commit

Permalink
chore(ui): Remove the RoomListService::rooms cache.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hywan committed Jul 1, 2024
1 parent 6132841 commit 7bd0427
Showing 1 changed file with 4 additions and 36 deletions.
40 changes: 4 additions & 36 deletions crates/matrix-sdk-ui/src/room_list_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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::{
Expand Down Expand Up @@ -103,20 +98,9 @@ pub struct RoomListService {
///
/// `RoomListService` is a simple state-machine.
state: SharedObservable<State>,

/// Room cache, to avoid recreating `Room`s every time users fetch them.
rooms: Arc<Mutex<RingBuffer<Room>>>,
}

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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -403,21 +382,10 @@ impl RoomListService {

/// Get a [`Room`] if it exists.
pub fn room(&self, room_id: &RoomId) -> Result<Room, Error> {
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)]
Expand Down

0 comments on commit 7bd0427

Please sign in to comment.