Skip to content

perf: Parallelize creating stores to make session restoration faster#6544

Merged
jmartinesp merged 2 commits intomainfrom
perf/try-creating-stores-concurrently
May 7, 2026
Merged

perf: Parallelize creating stores to make session restoration faster#6544
jmartinesp merged 2 commits intomainfrom
perf/try-creating-stores-concurrently

Conversation

@jmartinesp
Copy link
Copy Markdown
Contributor

@jmartinesp jmartinesp commented May 7, 2026

Changes

  • Parallelize creating stores for Client instances.
  • Also try parallelizing get_or_init for Client::event_cache and Client::thread_subscription_catchup. This may not do much, since the intialization is actually sync unless there was already some value (maybe not worth it?).

Codspeed doesn't seem to return different results, but I could definitely see changes when tested in a real client:

# Without joins

09:53:50.618 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:560 | spans: build
09:53:51.659 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~1.05s

09:54:29.128 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:560 | spans: build
09:54:30.278 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~1.15s

09:55:02.873 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:560 | spans: build
09:55:04.119 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~1.45s

09:58:59.290 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:560 | spans: build
09:59:00.641 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~1.3s

------------------------

# With joins

09:52:46.243 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:562 | spans: build
09:52:46.913 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~650ms


09:56:23.745 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:562 | spans: build
09:56:24.755 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~1s


09:57:14.194 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:562 | spans: build
09:57:14.821 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~630ms


09:57:53.431 org.matrix.rust.sdk     io.element.android.x.debug           D  matrix_sdk::client::builder: Starting to build the Client | crates/matrix-sdk/src/client/builder/mod.rs:562 | spans: build
09:57:54.058 org.matrix.rust.sdk     io.element.android.x.debug           D  elementx: Session restored successfully for sessionId: @jorgem:element.io | RustMatrixAuthenticationService.kt:112
~600ms
  • 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 perf/try-creating-stores-concurrently branch from 40e438b to 6d2db3e Compare May 7, 2026 07:07
@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.92%. Comparing base (99cfce8) to head (3eaa4a3).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk-sqlite/src/crypto_store.rs 57.14% 1 Missing and 2 partials ⚠️
crates/matrix-sdk-sqlite/src/state_store.rs 62.50% 1 Missing and 2 partials ⚠️
crates/matrix-sdk/src/client/builder/mod.rs 78.57% 2 Missing and 1 partial ⚠️
crates/matrix-sdk-sqlite/src/event_cache_store.rs 75.00% 1 Missing ⚠️
crates/matrix-sdk-sqlite/src/media_store.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6544      +/-   ##
==========================================
- Coverage   89.93%   89.92%   -0.02%     
==========================================
  Files         381      381              
  Lines      106780   106773       -7     
  Branches   106780   106773       -7     
==========================================
- Hits        96036    96014      -22     
- Misses       7096     7107      +11     
- Partials     3648     3652       +4     

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

@jmartinesp jmartinesp force-pushed the perf/try-creating-stores-concurrently branch 2 times, most recently from 039ae72 to dd5136d Compare May 7, 2026 07:23
@jmartinesp jmartinesp force-pushed the perf/try-creating-stores-concurrently branch from dd5136d to edeffe8 Compare May 7, 2026 07:24
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 7, 2026

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing perf/try-creating-stores-concurrently (3eaa4a3) with main (99cfce8)

Open in CodSpeed

@jmartinesp jmartinesp marked this pull request as ready for review May 7, 2026 08:20
@jmartinesp jmartinesp requested a review from a team as a code owner May 7, 2026 08:20
@jmartinesp jmartinesp requested review from Hywan and removed request for a team May 7, 2026 08:20
Copy link
Copy Markdown
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.

It looks great, thank you! Would be nice to have a benchmark for that, though it's not particularly “critical”. It might become critical when the client is built in a constrained process, like NSE on iOS for example.

use tokio::sync::Mutex;
use tokio::sync::OnceCell;
#[cfg(feature = "sqlite")]
use tokio::try_join;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess the best way to handle this is by declaring try_join in matrix_sdk_common::executor:

  1. for the case of #[cfg(not(target_family = "wasm"))], re-export tokio::try_join.
  2. for the case of #[cfg(target_family = "wasm")], re-export futures_util::try_join.

Even better, if you don't want to bother with that, you can use futures_util::try_join in all cases, without touching matrix_sdk_common. That way you remove the dependencies to tokio for this case.

if let Some(ref cache_path) = cache_path {
config = config.path(cache_path);
}
let caches_config = if let Some(ref cache_path) = cache_path {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can rename caches_config to cache_with_path. I think it's more self-explanatory, thoughts?

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.

config_with_cache_path?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup, good idea.

Comment thread crates/matrix-sdk/src/client/mod.rs Outdated
use serde::de::DeserializeOwned;
use tokio::sync::{Mutex, OnceCell, RwLock, RwLockReadGuard, broadcast};
use tokio::{
join,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same idea: maybe use futures_util here?

@jmartinesp
Copy link
Copy Markdown
Contributor Author

Would be nice to have a benchmark for that, though it's not particularly “critical”.

I think there is ("Restore session (Client reload)") it's just not displaying any changes.

@jmartinesp jmartinesp requested a review from Hywan May 7, 2026 09:16
@Hywan
Copy link
Copy Markdown
Member

Hywan commented May 7, 2026

Maybe because the stores aren't big enough to take a noticeable time in the benchmark. Okay. Let's keep things as they are for now.

@jmartinesp jmartinesp force-pushed the perf/try-creating-stores-concurrently branch from edfc283 to 3eaa4a3 Compare May 7, 2026 09:35
@jmartinesp jmartinesp merged commit 9ff4a4c into main May 7, 2026
54 checks passed
@jmartinesp jmartinesp deleted the perf/try-creating-stores-concurrently branch May 7, 2026 10:09
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