Skip to content

Commit c78639d

Browse files
committed
sdk-ui: fix pinned events benchmark
1 parent af5107f commit c78639d

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

benchmarks/benches/room_bench.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::time::Duration;
1+
use std::{sync::Arc, time::Duration};
22

33
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
44
use matrix_sdk::{
@@ -157,7 +157,6 @@ pub fn load_pinned_events_benchmark(c: &mut Criterion) {
157157
.respond_with(move |r: &Request| {
158158
let segments: Vec<&str> = r.url.path_segments().expect("Invalid path").collect();
159159
let event_id_str = segments[6];
160-
// let f = EventFactory::new().room(&room_id)
161160
let event_id = EventId::parse(event_id_str).expect("Invalid event id in response");
162161
let event = f
163162
.text_msg(format!("Message {event_id_str}"))
@@ -170,9 +169,6 @@ pub fn load_pinned_events_benchmark(c: &mut Criterion) {
170169
})
171170
.mount(&server),
172171
);
173-
// runtime.block_on(server.reset());
174-
175-
client.event_cache().subscribe().unwrap();
176172

177173
let room = client.get_room(&room_id).expect("Room not found");
178174
assert!(!room.pinned_event_ids().is_empty());
@@ -184,6 +180,15 @@ pub fn load_pinned_events_benchmark(c: &mut Criterion) {
184180
group.throughput(Throughput::Elements(count as u64));
185181
group.sample_size(10);
186182

183+
let client = Arc::new(client);
184+
185+
{
186+
let client = client.clone();
187+
runtime.spawn_blocking(move || {
188+
client.event_cache().subscribe().unwrap();
189+
});
190+
}
191+
187192
group.bench_function(BenchmarkId::new("load_pinned_events", name), |b| {
188193
b.to_async(&runtime).iter(|| async {
189194
assert!(!room.pinned_event_ids().is_empty());
@@ -207,7 +212,6 @@ pub fn load_pinned_events_benchmark(c: &mut Criterion) {
207212
{
208213
let _guard = runtime.enter();
209214
runtime.block_on(server.reset());
210-
drop(client);
211215
drop(server);
212216
}
213217

crates/matrix-sdk-ui/src/timeline/pinned_events_loader.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{fmt::Formatter, num::NonZeroUsize, sync::Arc};
1717
use futures_util::{future::join_all, FutureExt as _};
1818
use matrix_sdk::{
1919
config::RequestConfig, event_cache::paginator::PaginatorError, BoxFuture, Room,
20+
SendOutsideWasm, SyncOutsideWasm,
2021
};
2122
use matrix_sdk_base::deserialized_responses::SyncTimelineEvent;
2223
use ruma::{EventId, MilliSecondsSinceUnixEpoch, OwnedEventId};
@@ -112,10 +113,10 @@ impl PinnedEventsLoader {
112113
}
113114
}
114115

115-
pub trait PinnedEventsRoom {
116+
pub trait PinnedEventsRoom: SendOutsideWasm + SyncOutsideWasm {
116117
/// Load a single room event using the cache or network and any events
117118
/// related to it, if they are cached.
118-
async fn load_event_with_relations<'a>(
119+
fn load_event_with_relations<'a>(
119120
&'a self,
120121
event_id: &'a EventId,
121122
request_config: Option<RequestConfig>,
@@ -148,7 +149,7 @@ pub trait PinnedEventsRoom {
148149
}
149150

150151
impl PinnedEventsRoom for Room {
151-
async fn load_event_with_relations<'a>(
152+
fn load_event_with_relations<'a>(
152153
&'a self,
153154
event_id: &'a EventId,
154155
request_config: Option<RequestConfig>,

crates/matrix-sdk-ui/src/timeline/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl PaginableRoom for TestRoomDataProvider {
319319
}
320320

321321
impl PinnedEventsRoom for TestRoomDataProvider {
322-
async fn load_event_with_relations<'a>(
322+
fn load_event_with_relations<'a>(
323323
&'a self,
324324
_event_id: &'a EventId,
325325
_request_config: Option<RequestConfig>,

crates/matrix-sdk-ui/tests/integration/timeline/pinned_event.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use matrix_sdk_ui::{
1515
timeline::{TimelineFocus, TimelineItemContent},
1616
Timeline,
1717
};
18-
use ruma::{event_id, owned_room_id, MilliSecondsSinceUnixEpoch, OwnedRoomId};
18+
use ruma::{
19+
event_id, events::room::message::RoomMessageEventContentWithoutRelation, owned_room_id,
20+
MilliSecondsSinceUnixEpoch, OwnedRoomId,
21+
};
1922
use serde_json::json;
2023
use stream_assert::assert_pending;
2124
use wiremock::MockServer;
@@ -402,7 +405,11 @@ async fn test_edited_events_are_reflected_in_sync() {
402405
test_helper.server.reset().await;
403406

404407
let edited_event = f
405-
.replacement_msg("edited message!", event_id!("$1"))
408+
.text_msg("edited message!")
409+
.edit(
410+
event_id!("$1"),
411+
RoomMessageEventContentWithoutRelation::text_plain("* edited message!"),
412+
)
406413
.event_id(event_id!("$2"))
407414
.server_ts(MilliSecondsSinceUnixEpoch::now())
408415
.into_timeline();
@@ -425,7 +432,7 @@ async fn test_edited_events_are_reflected_in_sync() {
425432
assert_eq!(index, 1);
426433
match value.as_event().unwrap().content() {
427434
TimelineItemContent::Message(m) => {
428-
assert_eq!(m.body(), "edited message!")
435+
assert_eq!(m.body(), "* edited message!")
429436
}
430437
_ => panic!("Should be a message event"),
431438
}
@@ -547,7 +554,11 @@ async fn test_edited_events_survive_pinned_event_ids_change() {
547554
test_helper.server.reset().await;
548555

549556
let edited_pinned_event = f
550-
.replacement_msg("edited message!", event_id!("$1"))
557+
.text_msg("* edited message!")
558+
.edit(
559+
event_id!("$1"),
560+
RoomMessageEventContentWithoutRelation::text_plain("edited message!"),
561+
)
551562
.event_id(event_id!("$2"))
552563
.server_ts(MilliSecondsSinceUnixEpoch::now())
553564
.into_timeline();

crates/matrix-sdk/src/event_cache/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,9 @@ impl RoomEventCache {
570570

571571
/// Save a single event in the event cache, for further retrieval with
572572
/// [`Self::event`].
573-
// This doesn't insert the event into the linked chunk. In the future there'll
574-
// be no distinction between the linked chunk and the separate cache.
573+
// TODO: This doesn't insert the event into the linked chunk. In the future
574+
// there'll be no distinction between the linked chunk and the separate
575+
// cache. There is a discussion in https://github.com/matrix-org/matrix-rust-sdk/issues/3886.
575576
pub(crate) async fn save_event(&self, event: SyncTimelineEvent) {
576577
if let Some(event_id) = event.event_id() {
577578
let mut cache = self.inner.all_events_cache.write().await;
@@ -586,8 +587,9 @@ impl RoomEventCache {
586587
/// Save some events in the event cache, for further retrieval with
587588
/// [`Self::event`]. This function will save them using a single lock,
588589
/// as opposed to [`Self::save_event`].
589-
// This doesn't insert the event into the linked chunk. In the future there'll
590-
// be no distinction between the linked chunk and the separate cache.
590+
// TODO: This doesn't insert the event into the linked chunk. In the future
591+
// there'll be no distinction between the linked chunk and the separate
592+
// cache. There is a discussion in https://github.com/matrix-org/matrix-rust-sdk/issues/3886.
591593
pub(crate) async fn save_events(&self, events: impl IntoIterator<Item = SyncTimelineEvent>) {
592594
let mut cache = self.inner.all_events_cache.write().await;
593595
for event in events {

crates/matrix-sdk/src/event_cache/paginator.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,16 @@ impl PaginableRoom for Room {
471471
match self.event_with_context(event_id, lazy_load_members, num_events, None).await {
472472
Ok(result) => result,
473473

474-
Err(err) => {
475-
// If the error was a 404, then the event wasn't found on the server;
476-
// special case this to make it easy to react to
477-
// such an error.
478-
if let Some(error) = err.as_client_api_error() {
479-
if error.status_code == 404 {
480-
// Event not found
481-
return Err(PaginatorError::EventNotFound(event_id.to_owned()));
474+
Err(err) => {
475+
// If the error was a 404, then the event wasn't found on the server;
476+
// special case this to make it easy to react to
477+
// such an error.
478+
if let Some(error) = err.as_client_api_error() {
479+
if error.status_code == 404 {
480+
// Event not found
481+
return Err(PaginatorError::EventNotFound(event_id.to_owned()));
482+
}
482483
}
483-
}
484484

485485
// Otherwise, just return a wrapped error.
486486
return Err(PaginatorError::SdkError(Box::new(err)));

0 commit comments

Comments
 (0)