Skip to content

feat: compute and cache RoomInfo::active_service_members#6537

Merged
jmartinesp merged 17 commits into
mainfrom
feat/cache-active-service-member-count-in-room-info
May 7, 2026
Merged

feat: compute and cache RoomInfo::active_service_members#6537
jmartinesp merged 17 commits into
mainfrom
feat/cache-active-service-member-count-in-room-info

Conversation

@jmartinesp
Copy link
Copy Markdown
Contributor

@jmartinesp jmartinesp commented May 5, 2026

Changes

  • Add a new RoomSummary::active_room_members field that will be used as a cache.
  • Rename Room::active_service_members to Room::update_active_service_members, which will compute new values for the cache.
  • Also reset this cached value when the member counts change in sync responses.
  • Rename async fn Room::is_dm to Room::compute_is_dm and also trigger Room::update_active_service_members to re-calculate the cache.
  • Add a new synchronous Room::is_dm function that will use the cached active_service_members value or calculate an approximate value as a fallback.
  • Then expose this Room::compute_is_dm and Room::is_dm in RoomInfo, NotificationRoomInfo and SpaceRoom.

Motivation

For both the room list filtering and the SpaceRoom instances we can't rely on an accurate value computed in place, since those APIs are synchronous. The approximate value previously calculated could be wrong in several cases, so if we can compute a better one instead that should make the checks more accurate.

  • I've documented the public API Changes in the appropriate CHANGELOG.md files.
  • This PR was made with the help of AI.

Signed-off-by:

@jmartinesp jmartinesp force-pushed the feat/cache-active-service-member-count-in-room-info branch from 45f313e to 88a8cc5 Compare May 5, 2026 10:17
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

❌ Patch coverage is 88.94737% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.92%. Comparing base (99cfce8) to head (26c8b59).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk-base/src/room/mod.rs 66.66% 5 Missing and 3 partials ⚠️
crates/matrix-sdk-base/src/room/room_info.rs 96.62% 3 Missing ⚠️
crates/matrix-sdk/src/room/mod.rs 84.21% 0 Missing and 3 partials ⚠️
crates/matrix-sdk-ui/src/notification_client.rs 0.00% 0 Missing and 2 partials ⚠️
crates/matrix-sdk-ui/src/spaces/room.rs 33.33% 0 Missing and 2 partials ⚠️
crates/matrix-sdk/src/message_search.rs 0.00% 0 Missing and 2 partials ⚠️
...x-sdk-ui/src/room_list_service/filters/category.rs 98.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6537      +/-   ##
==========================================
- Coverage   89.93%   89.92%   -0.01%     
==========================================
  Files         381      381              
  Lines      106780   106867      +87     
  Branches   106780   106867      +87     
==========================================
+ Hits        96036    96105      +69     
- Misses       7096     7105       +9     
- Partials     3648     3657       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 5, 2026

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing feat/cache-active-service-member-count-in-room-info (26c8b59) with main (99cfce8)

Open in CodSpeed

@jmartinesp jmartinesp force-pushed the feat/cache-active-service-member-count-in-room-info branch 2 times, most recently from 51c245a to 1a6b534 Compare May 5, 2026 11:53
@jmartinesp jmartinesp changed the title WIP: compute and cache RoomInfo::active_service_members feat: compute and cache RoomInfo::active_service_members May 5, 2026
@jmartinesp jmartinesp marked this pull request as ready for review May 5, 2026 12:01
@jmartinesp jmartinesp requested a review from a team as a code owner May 5, 2026 12:01
@jmartinesp jmartinesp requested review from poljar and removed request for a team May 5, 2026 12:01
This will be used as a cache for the computed value.
@jmartinesp jmartinesp force-pushed the feat/cache-active-service-member-count-in-room-info branch from 20a48b4 to 53e84fa Compare May 6, 2026 17:16
@jmartinesp
Copy link
Copy Markdown
Contributor Author

I rebased the PR to solve the conflicts, since I think you still hadn't started reviewing it. The changes should be minimal anyway, just using the new Room::update_and_save_room_info methods instead of manually cloning and setting the new room info.

Copy link
Copy Markdown
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some commits seem like they won't compile, please fix this.

I left some other minor nits, but generally this looks fine.

Don't forget some changelog entries.

Comment on lines +544 to 545
pub async fn update_active_service_members(&self) -> StoreResult<Option<Vec<RoomMember>>> {
if let Some(service_members) = self.service_members() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document this new behavior in the docs of this method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread crates/matrix-sdk-base/src/room/mod.rs Outdated
}

trace!(
"Updating active service members ({}) in room {:?}",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Debug for the room ID instead of Display might not yield the result you'd want, i.e.:

println!("{room_id}, {room_id:?}");

Will yield:

!a:b.c, "!a:b.c"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread crates/matrix-sdk/src/room/mod.rs Outdated
Comment on lines +2038 to +2040
// Re-calculate active service members
self.update_active_service_members().await?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this done here as well? We didn't receive a remote echo from the /sync nor did we sync the members yet.

This will contain the incorrect value and it will be recomputed when we sync the members.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if new_active_service_member_count != current_active_service_member_count {
self.update_and_save_room_info(|mut info| {
info.update_active_service_member_count(Some(new_active_service_member_count));
(info, RoomInfoNotableUpdateReasons::ACTIVE_SERVICE_MEMBERS)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new notable update reason isn't available yet in commit 05e8811, as such the commit won't compile.

It's available in the next, the 61f1e57, commit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agh, wrong rebase of a fixup. Thanks.

/// Checks if the room belongs in the expected category based on whether it's a
/// DM or not.
fn matches(room: &RoomListItem, expected_kind: RoomCategory) -> bool {
let kind = if room.is_dm() { RoomCategory::People } else { RoomCategory::Group };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat refactor.

jmartinesp added 16 commits May 7, 2026 11:56
…active_service_members`

The function will now store the computed value if it changed into the `RoomSummary` cache
…ing the room members of the room or when inviting someone
…the joined/invited user count or the heroes in a room change
This function will now also compute and store the active service members under the hood
This function will check the cached value and use it if available, falling back to using an approximate value that will be calculated synchronously if not.
Based on the cached and synchronous `Room::is_dm`
…when you receive a member hints event that doesn't match what you previously had
@jmartinesp jmartinesp force-pushed the feat/cache-active-service-member-count-in-room-info branch from 53e84fa to 26c8b59 Compare May 7, 2026 10:08
@jmartinesp jmartinesp enabled auto-merge (rebase) May 7, 2026 10:08
@jmartinesp jmartinesp merged commit 777ce05 into main May 7, 2026
53 checks passed
@jmartinesp jmartinesp deleted the feat/cache-active-service-member-count-in-room-info branch May 7, 2026 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants