Skip to content

feat(sdk): All caches have an auto-shrink mechanism#6690

Draft
Hywan wants to merge 7 commits into
matrix-org:mainfrom
Hywan:feat-sdk-event-cache-generalised-auto-shrinker
Draft

feat(sdk): All caches have an auto-shrink mechanism#6690
Hywan wants to merge 7 commits into
matrix-org:mainfrom
Hywan:feat-sdk-event-cache-generalised-auto-shrinker

Conversation

@Hywan

@Hywan Hywan commented Jun 25, 2026

Copy link
Copy Markdown
Member

During the big refactoring of the Event Cache, I noticed that the auto-shrink mechanism needs to be implement on all caches now. It was implemented on RoomEventCache before, but we want to extend it to ThreadEventCache now its supports persistent storage (see #6317), and maybe on PinnedEventsCache too since it has its own storage too (note that this is less critical as it seems unlikely we will have that many pinned-events living in memory).

Tasks

  • Move event_cache/caches/room/subscriber.rs to event_cache/caches/subscriber.rs
  • Transform RoomEventCacheSubscriber to Subscriber<RoomEventCacheUpdate>, i.e. make it generic over any updates,
  • Add tests for this Subscriber type only,
  • Update AutoSkrinkPayload to be an enum instead of OwnedRoomId, something like:
    enum AutoShrinkPayload {
        Room { room_id: OwnedRoomId },
        Thread { room_id: OwnedRoomId, thread_id: OwnedEventId },
         // etc.
    }
  • Update the auto_shrink_linked_chunk_task,
  • Update ThreadEventCache::subscribe to return a Subscriber<TimelineVectorDiffs instead of a Receiver<TimelinevectorDiffs>,
  • Update PinnedEventsCache::subscribe to return a Subscriber<TimelineVectorDiffs instead of a Receiver<TimelinevectorDiffs>.


  • 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 added 2 commits June 25, 2026 17:34
…rs` to `subscriber.rs`.

This patch moves the `room/subscriber.rs` module to `subscriber.rs`.
It's the first step to generalise it to more caches.
…ntCacheUpdate>`.

This patch changes `RoomEventCacheSubscriber` to be
`Subscriber<RoomEventCacheUpdate>`: it is now generic over the kind
of update.
@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.06557% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.91%. Comparing base (567463d) to head (ffb8479).
⚠️ Report is 5 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...es/matrix-sdk/src/event_cache/caches/thread/mod.rs 97.36% 0 Missing and 3 partials ⚠️
crates/matrix-sdk/src/event_cache/tasks.rs 72.72% 3 Missing ⚠️
...rates/matrix-sdk-ui/src/timeline/controller/mod.rs 60.00% 0 Missing and 2 partials ⚠️
...es/matrix-sdk/src/event_cache/caches/subscriber.rs 98.65% 2 Missing ⚠️
.../matrix-sdk/src/event_cache/caches/thread/state.rs 85.71% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6690      +/-   ##
==========================================
- Coverage   89.91%   89.91%   -0.01%     
==========================================
  Files         396      396              
  Lines      110475   110688     +213     
  Branches   110475   110688     +213     
==========================================
+ Hits        99338    99523     +185     
- Misses       7371     7382      +11     
- Partials     3766     3783      +17     

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

@codspeed-hq

codspeed-hq Bot commented Jun 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing Hywan:feat-sdk-event-cache-generalised-auto-shrinker (ffb8479) with main (5a0b002)

Open in CodSpeed

Hywan added 4 commits June 26, 2026 10:49
This patch adds a small, but exhaustive, unit test suite for the
`Subscriber` type.
…ent_cache::caches::subscriber`.

This patch reorganises the code a little bit. The
`AutoShrinkChannelPayload` type from `event_cache` to
`event_cache::caches::subscriber`.
This patch renames the `AutoShrinkChannelPayload` type to
`AutoShrinkMessage`:

- it's shorter,
- the “Channel” part isn't really important here,
- across the SDK, we usually refer to this kind of value as _message_
  instead of _payload_.
This patch changes `AutoShrinkMessage` from being a type alias to
`OwnedRoomId` to an enum with a single variant for the moment: `Room {
room_id: OwnedRoomId }`. It's preparation work for supporting more cache
(such as thread).
@Hywan Hywan force-pushed the feat-sdk-event-cache-generalised-auto-shrinker branch from 9aabb02 to dbbfd9a Compare June 26, 2026 13:06
…ith auto-shrink.

This patch updates `ThreadEventCache::subscribe` to return a
`Subscriber` instead of a `tokio::sync::mpsc::Receiver`. The API is
strictly the same: only the type is different.

Returning a `Subscriber` ensures the `ThreadEventCache` is automatically
shrunk when all subscribers are gone away. This is matching the
behaviour of `RoomEventCache::subscribe`. This is possible since
`ThreadEventCache` now supports persistent storage: we can reload the
cache to its last chunk.

To achieve that, the `auto_shrink_sender` is now stored in
`CachesInternals`. It is also part of the `ThreadEventCache`
constructor.

Also, the new `AutoShrinkMessage::Thread` variant has been added, and
supported in `auto_shrink_linked_chunk_task`.

Finally, a new `test_auto_shrink_after_all_subscribers_are_gone` has
been added too.
@Hywan Hywan force-pushed the feat-sdk-event-cache-generalised-auto-shrinker branch from 2cfb994 to ffb8479 Compare June 26, 2026 14:29
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.

1 participant