Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk-base: split handle_account_data and process_direct_rooms #4010

Merged

Conversation

jmartinesp
Copy link
Contributor

Follow up for #3990.

This removes a couple of TODOs in the codebase where we were running handle_account_data twice to be able to update the direct rooms after the rooms had been created in the store. Now these responsibilities are split in two separate functions.

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@jmartinesp jmartinesp requested a review from a team as a code owner September 17, 2024 08:58
@jmartinesp jmartinesp requested review from Hywan and removed request for a team September 17, 2024 08:58
Copy link

codecov bot commented Sep 17, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 3 lines in your changes missing coverage. Please review.

Project coverage is 84.26%. Comparing base (f576c72) to head (c2015f5).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk-base/src/client.rs 95.23% 2 Missing ⚠️
crates/matrix-sdk-base/src/sliding_sync/mod.rs 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4010      +/-   ##
==========================================
- Coverage   84.27%   84.26%   -0.02%     
==========================================
  Files         266      266              
  Lines       28336    28341       +5     
==========================================
+ Hits        23880    23881       +1     
- Misses       4456     4460       +4     

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

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.

I believe this is an improvement and nothing jumps to my eyes. Thanks and well-done!

I left a couple of comments, but nothing blocking.

#[instrument(skip_all)]
pub(crate) async fn process_direct_rooms(
&self,
events: Vec<AnyGlobalAccountDataEvent>,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
events: Vec<AnyGlobalAccountDataEvent>,
events: &[AnyGlobalAccountDataEvent],

A Vec demands an allocation, but in this code, we may not need one. So requiring a slice is better here. Read my other comments to see how it translates.

if has_new_direct_room_data {
self.handle_account_data(&response.account_data.events, &mut changes).await;
self.process_direct_rooms(global_account_data_events, &mut changes).await;
Copy link
Member

Choose a reason for hiding this comment

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

Don't pass the Vec but a reference to it, so basically a slice (because a Vec derefs to a slice)

Suggested change
self.process_direct_rooms(global_account_data_events, &mut changes).await;
self.process_direct_rooms(&global_account_data_events, &mut changes).await;

} else if let Ok(Some(direct_account_data)) =
self.store.get_account_data_event(GlobalAccountDataEventType::Direct).await
{
debug!("Found direct room data in the Store, applying it");
self.handle_account_data(&[direct_account_data], &mut changes).await;
if let Ok(direct_account_data) = direct_account_data.deserialize() {
self.process_direct_rooms(vec![direct_account_data], &mut changes).await;
Copy link
Member

Choose a reason for hiding this comment

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

And finally here, don't allocate a vector (which does an allocation on the heap, this is why it's costly), but simply pass a slice of one element.

Suggested change
self.process_direct_rooms(vec![direct_account_data], &mut changes).await;
self.process_direct_rooms(&[direct_account_data], &mut changes).await;

let global_account_data_events = if !account_data.is_empty() {
self.handle_account_data(&account_data.global, &mut changes).await
} else {
Vec::new()
Copy link
Member

Choose a reason for hiding this comment

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

Note: Here this is fine to get a Vec::new(), it does zero allocation.

I know it's different from what we've talked in the above comments, but I'm sharing some Rust knowledge.

if has_new_direct_room_data {
self.handle_account_data(&account_data.global, &mut changes).await;
self.process_direct_rooms(global_account_data_events, &mut changes).await;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.process_direct_rooms(global_account_data_events, &mut changes).await;
self.process_direct_rooms(&global_account_data_events, &mut changes).await;

} else if let Ok(Some(direct_account_data)) =
self.store.get_account_data_event(GlobalAccountDataEventType::Direct).await
{
debug!("Found direct room data in the Store, applying it");
self.handle_account_data(&[direct_account_data], &mut changes).await;
if let Ok(direct_account_data) = direct_account_data.deserialize() {
self.process_direct_rooms(vec![direct_account_data], &mut changes).await;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.process_direct_rooms(vec![direct_account_data], &mut changes).await;
self.process_direct_rooms(&[direct_account_data], &mut changes).await;

This removes a couple of TODOs in the codebase.
@jmartinesp jmartinesp force-pushed the misc/split-handle-account-data-and-process-direct-rooms branch from 6ab5172 to c2015f5 Compare September 18, 2024 06:00
@jmartinesp
Copy link
Contributor Author

Thanks for the comments, I think all those are fixed, so I'll merge it now.

@jmartinesp jmartinesp enabled auto-merge (rebase) September 18, 2024 06:01
@jmartinesp jmartinesp merged commit 7d1bbfa into main Sep 18, 2024
40 checks passed
@jmartinesp jmartinesp deleted the misc/split-handle-account-data-and-process-direct-rooms branch September 18, 2024 06:16
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