Skip to content

feat(sdk): Flatten the hierarchy of caches in the Event Cache, part 3: Event-focused#6603

Merged
Hywan merged 14 commits into
matrix-org:features/event-cache-refactoringfrom
Hywan:features/event-cache-refactoring-flatten-event-focused
May 22, 2026
Merged

feat(sdk): Flatten the hierarchy of caches in the Event Cache, part 3: Event-focused#6603
Hywan merged 14 commits into
matrix-org:features/event-cache-refactoringfrom
Hywan:features/event-cache-refactoring-flatten-event-focused

Conversation

@Hywan

@Hywan Hywan commented May 21, 2026

Copy link
Copy Markdown
Member

This PR is the sequel of #6517. This time, we are “flattening” the pinned-events cache, i.e. it extracts the pinned-event cache from the room cache to move it directly in Caches. The tree goes from:

graph TD;
    EventCache-->Caches;
    Caches-->RoomEventCache;
    RoomEventCache-->RoomEventCacheState;
    RoomEventCacheState-->EventFocusedCache;
    EventFocusedCache-->EventFocusedCacheInner;
Loading

to:

graph TD;
    EventCache-->Caches;
    Caches-->RoomEventCache;
    Caches-->EventFocusedCache;
    RoomEventCache-->RoomEventCacheState;
    EventFocusedCache-->EventFocusedCacheState;
Loading

This change is simpler than the previous ones (for thread caches or pinned-event cache) because an event-focused cache doesn't listen to live sync update. Consequently, we don't need any form of aggregator (in the “pipelines”) and so on.

New Features or Breaking Changes

First and foremost, EventFocusedCacheInner is renamed EventFocusedCacheState because it contains its state.

Then, all methods related to event-focused caches on RoomEventCache are removed, for example: RoomEventCache::get_or_create_event_focused_cache, or RoomEventCache::get_event_focused_cache are now removed. EventCache now provides 1 new public method: event_focused to lazily access an EventFocusedCache.

One new feature is that it's now possible to reset an event-focused cache, whilst it was not possible before.

Bug Fixes

Redecryptor (aka R2D2) has no more ABBA locking problem when replacing resolved UTDs in various caches. Redecryptor “extends” EventCache and uses the Caches type. Since all caches are now separated from each other, the initial locking problem no longer exists.



  • I've documented the public API changes in the appropriate changelog files
    (see Writing changelog entries).
  • This PR was made with the help of AI.

Signed-off-by:

@Hywan Hywan force-pushed the features/event-cache-refactoring-flatten-event-focused branch from c749995 to 24aee00 Compare May 21, 2026 16:01
@codecov

codecov Bot commented May 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.54902% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.84%. Comparing base (28e82f4) to head (420c093).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ix-sdk/src/event_cache/caches/event_focused/mod.rs 12.50% 14 Missing ⚠️
crates/matrix-sdk/src/event_cache/caches/mod.rs 82.60% 9 Missing and 3 partials ⚠️
crates/matrix-sdk/src/event_cache/mod.rs 84.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           features/event-cache-refactoring    #6603      +/-   ##
====================================================================
- Coverage                             89.89%   89.84%   -0.05%     
====================================================================
  Files                                   385      385              
  Lines                                109052   109074      +22     
  Branches                             109052   109074      +22     
====================================================================
- Hits                                  98035    98002      -33     
- Misses                                 7310     7348      +38     
- Partials                               3707     3724      +17     

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

@codspeed-hq

codspeed-hq Bot commented May 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing Hywan:features/event-cache-refactoring-flatten-event-focused (420c093) with main (1205c2b)1

Open in CodSpeed

Footnotes

  1. No successful run was found on features/event-cache-refactoring (28e82f4) during the generation of this report, so main (1205c2b) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Hywan Hywan force-pushed the features/event-cache-refactoring-flatten-event-focused branch from 24aee00 to 8ea9363 Compare May 22, 2026 12:25
@Hywan Hywan force-pushed the features/event-cache-refactoring branch from b62812a to 28e82f4 Compare May 22, 2026 12:49
Hywan added 14 commits May 22, 2026 14:53
…sed`.

This patch moves `EventFocusedCacheKey` from the `room` module into the
`event_focused` module because it's about the event-focused cache.
…already exists.

This patch clones an existing `WeakRoom` instead of creating another one
from scratch.
This patch removes the `room` argument of
`EventFocusedCache(Inner)::start_from`. First off, this is not required
as the struct already has the `WeakRoom`. Second, this is dangerous as
the two rooms might differ, or different rooms might be used between
called. This can be really inconsistent.

This patch removes the `room` argument and the code uses the `WeakRoom`
in the struct instead.
This patch stores all the `EventFocusedCache` in `Caches`.
…dCacheKey`.

This patch renames the `EventFocusedCacheKey::focused` field to
`focused_event_id` to clarify what is focused.
…caches.

This patch updates R2D2 to uses `Caches` via
`EventCache::inner::all_caches_for_room` to get a reference to all the
caches. It updates the way the event-focused cache was updated. No more
ABBA lock problem here. The code is simplified.
This patch reorganises the imports. Notably, `RoomPagination` is a
submodule: no need to import it from `crate::event_cache`.
…tate`.

This patch renames `EventFocusedCacheInner` to `EventFocusedCacheState`
because it contains the state of the `EventFocusedCacheState`.
This patch updates `ResetCaches` to handle the event-focused caches.
@Hywan Hywan force-pushed the features/event-cache-refactoring-flatten-event-focused branch from 8ea9363 to 420c093 Compare May 22, 2026 12:53
@Hywan Hywan requested a review from jmartinesp May 22, 2026 12:56
@Hywan Hywan marked this pull request as ready for review May 22, 2026 12:56
@Hywan Hywan requested a review from a team as a code owner May 22, 2026 12:56
@Hywan Hywan requested review from stefanceriu and removed request for a team and stefanceriu May 22, 2026 12:56

@jmartinesp jmartinesp left a comment

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.

LGTM, thanks!

Comment thread crates/matrix-sdk/src/event_cache/caches/mod.rs

OwnedRwLockWriteGuard::downgrade_map(
event_focused_caches,
|event_focused_caches| event_focused_caches.get(&key).unwrap(),

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.

I guess it's safe to unwrap here because we just added it above?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct!

@Hywan Hywan merged commit 93a9d40 into matrix-org:features/event-cache-refactoring May 22, 2026
110 of 116 checks passed
@Hywan Hywan linked an issue May 28, 2026 that may be closed by this pull request
21 tasks
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.

[meta] Event Cache layers refactoring

2 participants