Skip to content

Commit 0a95063

Browse files
committed
optimize(timeline): find the position of an event by starting from the end
1 parent bb1bc40 commit 0a95063

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/matrix-sdk-ui/src/timeline/controller/observable_items.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,16 @@ impl AllRemoteEvents {
19471947

19481948
/// Get the position of an event in the events array by its ID.
19491949
pub fn position_by_event_id(&self, event_id: &EventId) -> Option<usize> {
1950-
self.0.iter().position(|event_meta| event_meta.event_id == event_id)
1950+
// Reverse the iterator to start looking at the end. Since this will give us the
1951+
// "reverse" position, reverse the index after finding the event.
1952+
//
1953+
// If the reversed index was 0 in a array of size 1, then it should stay 0, so
1954+
// that justifies the `- 1` in the formula below.
1955+
self.0
1956+
.iter()
1957+
.rev()
1958+
.position(|event_meta| event_meta.event_id == event_id)
1959+
.map(|rev_index| self.0.len() - 1 - rev_index)
19511960
}
19521961

19531962
/// Shift to the right all timeline item indexes that are equal to or

0 commit comments

Comments
 (0)