Skip to content

refactor(event cache): allow deref state lock read/write guards to the state#6314

Merged
bnjbvr merged 1 commit intomainfrom
bnjbvr/refactor-event-cache-lock-guards
Mar 19, 2026
Merged

refactor(event cache): allow deref state lock read/write guards to the state#6314
bnjbvr merged 1 commit intomainfrom
bnjbvr/refactor-event-cache-lock-guards

Conversation

@bnjbvr
Copy link
Member

@bnjbvr bnjbvr commented Mar 18, 2026

This makes it possible to common out the implementations of functions that should be available to both kinds.

Also, moves a bit of code that could easily live in the RoomEventCacheState impl block, instead of being free functions.

Only code motion, no changes in functionality.

  • I've documented the public API Changes in the appropriate CHANGELOG.md files.
  • This PR was made with the help of AI. (is rust-analyzer AI?)

…e state

This makes it possible to common out the implementations of functions
that should be available to both kinds.

Also, moves a bit of code that could easily live in the
`RoomEventCacheState` impl block, instead of being free functions.
@bnjbvr bnjbvr requested a review from a team as a code owner March 18, 2026 15:14
@bnjbvr bnjbvr requested review from poljar and removed request for a team March 18, 2026 15:14
@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 83.13253% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.81%. Comparing base (81bed18) to head (4fd97b4).
⚠️ Report is 9 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/room/state.rs 85.13% 5 Missing and 6 partials ⚠️
crates/matrix-sdk/src/event_cache/caches/lock.rs 66.66% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6314      +/-   ##
==========================================
- Coverage   89.83%   89.81%   -0.03%     
==========================================
  Files         376      376              
  Lines      102469   102455      -14     
  Branches   102469   102455      -14     
==========================================
- Hits        92058    92017      -41     
- Misses       6840     6856      +16     
- Partials     3571     3582      +11     

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

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 18, 2026

Merging this PR will improve performance by 98.51%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 49 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation Restore session [memory store] 222.4 ms 112.1 ms +98.51%

Comparing bnjbvr/refactor-event-cache-lock-guards (4fd97b4) with main (81bed18)

Open in CodSpeed

Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Looks sensible to me. Left a comment about the Deref (extended to DerefMut) implementations, not a blocker.

&self.state
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Since state is pub, is it really useful to have a Deref implementation here? My fear is that it can create a bit of confusion when reading the code (despite being simpler in appearance).

Copy link
Member Author

Choose a reason for hiding this comment

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

"jump to definition" and every other actions seem to work fine with Deref/DerefMut. Not having the Deref impl would mean that every current caller of state_guard.room_linked_chunk() would need to be replaced with state_guard.state.room_linked_chunk().

As a thought experiment, I've tried to remove the Deref impls, and I get around 40 compilation errors, which shows that the Deref is then used in multiple places. Since Deref is rather idiomatic in Rust, especially expected for a lock guard (the stdlib has such a Deref impl for its own lock guards), and there are many uses of this, with the related refactors, I'll keep it for now. We can definitely revisit this later, if needs be.

@bnjbvr bnjbvr merged commit 9c4a47b into main Mar 19, 2026
53 checks passed
@bnjbvr bnjbvr deleted the bnjbvr/refactor-event-cache-lock-guards branch March 19, 2026 13:21
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