Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): Fix performance of AllRemoteEvents::(in|de)crement_all_timeline_item_index_after #4608

Merged

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Feb 3, 2025

This patch fixes the performance of
AllRemoteEvents::increment_all_timeline_item_index_after and
decrment_all_timeline_item_index_after.

It appears that the code was previously iterating over all items. This
is a waste of time. This patch updates the code to iterate over all
items in reverse order:

  • if new_timeline_item_index is 0, we need to shift all items anyways,
    so all items must be traversed, the iterator direction doesn't matter,
  • otherwise, it's unlikely we want to traverse all items: the item has
    been either inserted or pushed back, so there is no need to traverse
    the first items; we can also break the iteration as soon as all
    timeline item index after new_timeline_item_index has been updated.

I'm testing this patch with the test_lazy_back_pagination test in
#4594. With 10_000
events in the sync, the ObservableItems::push_back method (that uses
AllRemoteEvents::increment_all_timeline_item_index_after) was taking
7% of the whole execution time. With this patch, it takes 0.1%.

The call tree of TimelineEventHandler::add_item and its flamegraph
before this patch…

Screenshot 2025-02-03 at 10 01 13 Screenshot 2025-02-03 at 10 02 38

… and after this patch:

Screenshot 2025-02-03 at 10 01 34 Screenshot 2025-02-03 at 10 02 25

@Hywan Hywan requested a review from a team as a code owner February 3, 2025 09:07
@Hywan Hywan requested review from poljar and removed request for a team February 3, 2025 09:07
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.75%. Comparing base (33a2cc3) to head (06d56b6).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4608   +/-   ##
=======================================
  Coverage   85.74%   85.75%           
=======================================
  Files         291      291           
  Lines       33292    33292           
=======================================
+ Hits        28545    28548    +3     
+ Misses       4747     4744    -3     

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

…line_item_index_after`.

This patch fixes the performance of
`AllRemoteEvents::increment_all_timeline_item_index_after` and
`decrment_all_timeline_item_index_after`.

It appears that the code was previously iterating over all items. This
is a waste of time. This patch updates the code to iterate over all
items in reverse order:

- if `new_timeline_item_index` is 0, we need to shift all items anyways,
  so all items must be traversed, the iterator direction doesn't matter,
- otherwise, it's unlikely we want to traverse all items: the item has
  been either inserted or pushed back, so there is no need to traverse
  the first items; we can also break the iteration as soon as all
  timeline item index after `new_timeline_item_index` has been updated.

I'm testing this patch with the `test_lazy_back_pagination` test in
matrix-org#4594. With 10_000
events in the sync, the `ObservableItems::push_back` method (that uses
`AllRemoteEvents::increment_all_timeline_item_index_after`) was taking
7% of the whole execution time. With this patch, it takes 0.7%.
@Hywan Hywan force-pushed the fix-ui-performance-observable-items-push-back branch from afd29fb to 06d56b6 Compare February 3, 2025 09:28
@Hywan Hywan merged commit 9ab5547 into matrix-org:main Feb 3, 2025
40 checks passed
Hywan added a commit to Hywan/matrix-rust-sdk that referenced this pull request Feb 4, 2025
Hywan added a commit to Hywan/matrix-rust-sdk that referenced this pull request Feb 4, 2025
Hywan added a commit that referenced this pull request Feb 4, 2025
This patch adds #4601, #4608, #4612 and #4616 in their respective
`CHANGELOG.md`s.
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