Skip to content

Commit 15ab776

Browse files
committed
feat(base): Make Store::get_rooms wayyy faster.
`Store::get_rooms` worked as follows: For each room ID, call * Take the read lock for `rooms`, * For each entry in `rooms`, take the room ID (the key), then call `Store::get_room`, which… * Take the read lock for `rooms`, * Based on the room ID (the key), read the room (the value) from `rooms`. So calling `get_rooms` calls `get_room` for each room, which takes the lock every time. This patch modifies that by fetching the values (the rooms) directly from `rooms`, without calling `get_room`. In my benchmark, it took 1.2ms to read 10'000 rooms. Now it takes 542µs. Performance time has improved by -55.8%.
1 parent 868e821 commit 15ab776

File tree

1 file changed

+2
-1
lines changed
  • crates/matrix-sdk-base/src/store

1 file changed

+2
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub(crate) struct Store {
142142
session_meta: Arc<OnceCell<SessionMeta>>,
143143
/// The current sync token that should be used for the next sync call.
144144
pub(super) sync_token: Arc<RwLock<Option<String>>>,
145+
/// All rooms the store knows about.
145146
rooms: Arc<StdRwLock<BTreeMap<OwnedRoomId, Room>>>,
146147
/// A lock to synchronize access to the store, such that data by the sync is
147148
/// never overwritten.
@@ -203,7 +204,7 @@ impl Store {
203204

204205
/// Get all the rooms this store knows about.
205206
pub fn get_rooms(&self) -> Vec<Room> {
206-
self.rooms.read().unwrap().keys().filter_map(|id| self.get_room(id)).collect()
207+
self.rooms.read().unwrap().values().cloned().collect()
207208
}
208209

209210
/// Get all the rooms this store knows about, filtered by state.

0 commit comments

Comments
 (0)