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

Stop doing O(n^2) work to find event's home (eventShouldLiveIn) #3227

Merged
merged 2 commits into from
Mar 23, 2023

Conversation

jryans
Copy link
Collaborator

@jryans jryans commented Mar 23, 2023

In certain rooms (e.g. with many state changes hidden via user preferences), the events array presented to eventShouldLiveIn may contain 100s of events. As part of its various checks, eventShouldLiveIn would get an event's associated ID (reply / relation / redaction parent). It would then use events.find to search the entire (possibly large) events array to look for the parent. (This by itself seems sub-optimal and should probably change to use a map.)

For many events in a room, there is no associated ID. Unfortunately, eventShouldLiveIn did not check whether the associated ID actually exists before running off to search all of events, resulting in O(n^2) work.

This changes eventShouldLiveIn to first check that there is an associated ID before proceeding with its (slow) search. For some rooms, this change drastically improves performance from ~100% CPU usage to nearly idle.

Profile before

image

Profile after

image

Checklist

  • Tests written for new code (and old code if feasible)
  • Linter and other CI checks pass
  • Sign-off given on the changes (see CONTRIBUTING.md)

Here's what your changelog entry will look like:

🐛 Bug Fixes

  • Stop doing O(n^2) work to find event's home (eventShouldLiveIn) (#3227). Contributed by @jryans.

In certain rooms (e.g. with many state changes hidden via user preferences), the
events array presented to `eventShouldLiveIn` may contain 100s of events. As
part of its various checks, `eventShouldLiveIn` would get an event's associated
ID (reply / relation / redaction parent). It would then use `events.find` to
search the entire (possibly large) `events` array to look for the parent. (This
by itself seems sub-optimal and should probably change to use a map.)

For many events in a room, there is no associated ID. Unfortunately,
`eventShouldLiveIn` did not check whether the associated ID actually exists
before running off to search all of `events`, resulting in O(n^2) work.

This changes `eventShouldLiveIn` to first check that there is an associated ID
before proceeding with its (slow) search. For some rooms, this change
drastically improves performance from ~100% CPU usage to nearly idle.

Signed-off-by: J. Ryan Stinnett <jryans@gmail.com>
Copy link
Member

@t3chguy t3chguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: LGTM otherwise

Thanks for this!!

src/models/room.ts Outdated Show resolved Hide resolved
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
@t3chguy t3chguy enabled auto-merge March 23, 2023 18:58
@t3chguy t3chguy added this pull request to the merge queue Mar 23, 2023
Merged via the queue into develop with commit 5f3e115 Mar 23, 2023
@t3chguy t3chguy deleted the jryans/event-home-perf branch March 23, 2023 19:22
@MadLittleMods MadLittleMods changed the title Stop doing O(n^2) work to find event's home Stop doing O(n^2) work to find event's home (eventShouldLiveIn) Mar 23, 2023
su-ex added a commit to SchildiChat/matrix-js-sdk that referenced this pull request Apr 21, 2023
* Allow via_servers property in findPredecessor (update to MSC3946) ([\matrix-org#3240](matrix-org#3240)). Contributed by @andybalaam.
* Fire `closed` event when IndexedDB closes unexpectedly ([\matrix-org#3218](matrix-org#3218)).
* Implement MSC3952: intentional mentions ([\matrix-org#3092](matrix-org#3092)). Fixes element-hq/element-web#24376.
* Send one time key count and unused fallback keys for rust-crypto ([\matrix-org#3215](matrix-org#3215)). Fixes element-hq/element-web#24795. Contributed by @florianduros.
* Improve `processBeaconEvents` hotpath ([\matrix-org#3200](matrix-org#3200)).
* Implement MSC3966: a push rule condition to check if an array contains a value ([\matrix-org#3180](matrix-org#3180)).
* indexddb-local-backend - return the current sync to database promise … ([\matrix-org#3222](matrix-org#3222)). Contributed by @texuf.
* Revert "Add the call object to Call events" ([\matrix-org#3236](matrix-org#3236)).
* Handle group call redaction ([\matrix-org#3231](matrix-org#3231)). Fixes vector-im/voip-internal#128.
* Stop doing O(n^2) work to find event's home (`eventShouldLiveIn`) ([\matrix-org#3227](matrix-org#3227)). Contributed by @jryans.
* Fix bug where video would not unmute if it started muted ([\matrix-org#3213](matrix-org#3213)). Fixes element-hq/element-call#925.
* Fixes to event encryption in the Rust Crypto implementation ([\matrix-org#3202](matrix-org#3202)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-Defect Z-Community-PR Issue is solved by a community member's PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants