Skip to content

Commit

Permalink
refactor: avoid use of async_trait for RoomIdentityProvider
Browse files Browse the repository at this point in the history
This is an 8 seconds (out of 22) decrease of the matrix-sdk compile
times.
  • Loading branch information
bnjbvr committed Dec 19, 2024
1 parent e4712be commit f2942db
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
40 changes: 22 additions & 18 deletions crates/matrix-sdk-crypto/src/identities/room_identity_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::collections::HashMap;

use async_trait::async_trait;
use matrix_sdk_common::BoxFuture;
use ruma::{
events::{
room::member::{MembershipState, SyncRoomMemberEvent},
Expand All @@ -31,18 +31,17 @@ use crate::store::IdentityUpdates;
///
/// This is implemented by `matrix_sdk::Room` and is a trait here so we can
/// supply a mock when needed.
#[async_trait]
pub trait RoomIdentityProvider: core::fmt::Debug {
/// Is the user with the supplied ID a member of this room?
async fn is_member(&self, user_id: &UserId) -> bool;
fn is_member<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, bool>;

/// Return a list of the [`UserIdentity`] of all members of this room
async fn member_identities(&self) -> Vec<UserIdentity>;
fn member_identities(&self) -> BoxFuture<'_, Vec<UserIdentity>>;

/// Return the [`UserIdentity`] of the user with the supplied ID (even if
/// they are not a member of this room) or None if this user does not
/// exist.
async fn user_identity(&self, user_id: &UserId) -> Option<UserIdentity>;
fn user_identity<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, Option<UserIdentity>>;

/// Return the [`IdentityState`] of the supplied user identity.
/// Normally only overridden in tests.
Expand Down Expand Up @@ -352,7 +351,7 @@ mod tests {
sync::{Arc, Mutex},
};

use async_trait::async_trait;
use matrix_sdk_common::BoxFuture;
use matrix_sdk_test::async_test;
use ruma::{
device_id,
Expand Down Expand Up @@ -1030,23 +1029,28 @@ mod tests {
}
}

#[async_trait]
impl RoomIdentityProvider for FakeRoom {
async fn is_member(&self, user_id: &UserId) -> bool {
self.users.lock().unwrap().get(user_id).map(|m| m.is_member).unwrap_or(false)
fn is_member<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, bool> {
Box::pin(async {
self.users.lock().unwrap().get(user_id).map(|m| m.is_member).unwrap_or(false)
})
}

async fn member_identities(&self) -> Vec<UserIdentity> {
self.users
.lock()
.unwrap()
.values()
.filter_map(|m| if m.is_member { Some(m.user_identity.clone()) } else { None })
.collect()
fn member_identities(&self) -> BoxFuture<'_, Vec<UserIdentity>> {
Box::pin(async {
self.users
.lock()
.unwrap()
.values()
.filter_map(|m| if m.is_member { Some(m.user_identity.clone()) } else { None })
.collect()
})
}

async fn user_identity(&self, user_id: &UserId) -> Option<UserIdentity> {
self.users.lock().unwrap().get(user_id).map(|m| m.user_identity.clone())
fn user_identity<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, Option<UserIdentity>> {
Box::pin(async {
self.users.lock().unwrap().get(user_id).map(|m| m.user_identity.clone())
})
}

fn state_of(&self, user_identity: &UserIdentity) -> IdentityState {
Expand Down
49 changes: 26 additions & 23 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use std::{
};

use async_stream::stream;
#[cfg(all(feature = "e2e-encryption", not(target_arch = "wasm32")))]
use async_trait::async_trait;
use eyeball::SharedObservable;
use futures_core::Stream;
use futures_util::{
Expand All @@ -47,6 +45,8 @@ use matrix_sdk_base::{
ComposerDraft, RoomInfoNotableUpdateReasons, RoomMemberships, StateChanges, StateStoreDataKey,
StateStoreDataValue,
};
#[cfg(all(feature = "e2e-encryption", not(target_arch = "wasm32")))]
use matrix_sdk_common::BoxFuture;
use matrix_sdk_common::{
deserialized_responses::SyncTimelineEvent,
executor::{spawn, JoinHandle},
Expand Down Expand Up @@ -3363,34 +3363,37 @@ impl Room {
}

#[cfg(all(feature = "e2e-encryption", not(target_arch = "wasm32")))]
#[async_trait]
impl RoomIdentityProvider for Room {
async fn is_member(&self, user_id: &UserId) -> bool {
self.get_member(user_id).await.unwrap_or(None).is_some()
fn is_member<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, bool> {
Box::pin(async { self.get_member(user_id).await.unwrap_or(None).is_some() })
}

async fn member_identities(&self) -> Vec<UserIdentity> {
let members = self
.members(RoomMemberships::JOIN | RoomMemberships::INVITE)
.await
.unwrap_or_else(|_| Default::default());
fn member_identities(&self) -> BoxFuture<'_, Vec<UserIdentity>> {
Box::pin(async {
let members = self
.members(RoomMemberships::JOIN | RoomMemberships::INVITE)
.await
.unwrap_or_else(|_| Default::default());

let mut ret: Vec<UserIdentity> = Vec::new();
for member in members {
if let Some(i) = self.user_identity(member.user_id()).await {
ret.push(i);
let mut ret: Vec<UserIdentity> = Vec::new();
for member in members {
if let Some(i) = self.user_identity(member.user_id()).await {
ret.push(i);
}
}
}
ret
ret
})
}

async fn user_identity(&self, user_id: &UserId) -> Option<UserIdentity> {
self.client
.encryption()
.get_user_identity(user_id)
.await
.unwrap_or(None)
.map(|u| u.underlying_identity())
fn user_identity<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, Option<UserIdentity>> {
Box::pin(async {
self.client
.encryption()
.get_user_identity(user_id)
.await
.unwrap_or(None)
.map(|u| u.underlying_identity())
})
}
}

Expand Down

0 comments on commit f2942db

Please sign in to comment.