Skip to content

Commit 3f71d9a

Browse files
committed
fix(sdk): Improve performance of RoomEvents::maybe_apply_new_redaction.
This patch improves the performance of `RoomEvents::maybe_apply_new_redaction`. This method deserialises all the events it receives, entirely. If the event is not an `m.room.redaction`, then the method returns early. Most of the time, the event is deserialised for nothing because most events aren't of kind `m.room.redaction`! This patch first uses `Raw::get_field("type")` to detect the type of the event. If it's a `m.room.redaction`, then the event is entirely deserialized, otherwise the method returns. When running the `test_lazy_back_pagination` from #4594 with 10'000 events, prior to this patch, this method takes 11% of the execution time. With this patch, this method takes 2.5%.
1 parent b077f45 commit 3f71d9a

File tree

1 file changed

+13
-1
lines changed
  • crates/matrix-sdk/src/event_cache/room

1 file changed

+13
-1
lines changed

crates/matrix-sdk/src/event_cache/room/events.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use matrix_sdk_common::linked_chunk::{
2222
ObservableUpdates, Position,
2323
};
2424
use ruma::{
25-
events::{room::redaction::SyncRoomRedactionEvent, AnySyncTimelineEvent},
25+
events::{room::redaction::SyncRoomRedactionEvent, AnySyncTimelineEvent, MessageLikeEventType},
2626
OwnedEventId, RoomVersionId,
2727
};
2828
use tracing::{debug, error, instrument, trace, warn};
@@ -97,6 +97,18 @@ impl RoomEvents {
9797
/// event in the chunk, and replace it by the redacted form.
9898
#[instrument(skip_all)]
9999
fn maybe_apply_new_redaction(&mut self, room_version: &RoomVersionId, event: &Event) {
100+
let raw_event = event.raw();
101+
102+
// Do not deserialise the entire event if we aren't certain it's a
103+
// `m.room.redaction`. It saves a non-negligible amount of computations.
104+
let Ok(Some(MessageLikeEventType::RoomRedaction)) =
105+
raw_event.get_field::<MessageLikeEventType>("type")
106+
else {
107+
return;
108+
};
109+
110+
// It is a `m.room.redaction`! We can deserialize it entirely.
111+
100112
let Ok(AnySyncTimelineEvent::MessageLike(
101113
ruma::events::AnySyncMessageLikeEvent::RoomRedaction(redaction),
102114
)) = event.raw().deserialize()

0 commit comments

Comments
 (0)