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

Prioritise and reduce the amount of events decrypted on application startup #1684

Merged
merged 16 commits into from
May 12, 2021

Conversation

germain-gg
Copy link
Contributor

@germain-gg germain-gg commented May 6, 2021

Related to element-hq/element-web#13266
To review alongside matrix-org/matrix-react-sdk#5980

When loading element, it will either read from cache the entire /sync blob that has been persisted or will query that information from Synapse.
The idea is to implement a priority queue that will cap the maximum of events that will be decrypted on application startup. Everything else will be lazily decrypted when a user views a room

At the moment, from clicking on a tile to having all messages displayed on screen it takes roughly a second. With lazy decryption it will approximately take 2 seconds but the most recent messages will start to appear on screen from the first second.

Critical events

This pull request brings the concept of critical events. These are the events that a client requires to have decrypted to ensure it can run and provide all the required features to the user

At the moment a critical event is an event that falls under the following category

  • Last event of a room (to ensure previews are working)
  • All events that occured after the read marker (to ensure correct-ness in notification count)

Let me know if you can think about anything else that would benefit from being decrypted ahead of time

In addition to that, I am using breadcrumb rooms to detect which rooms a user might be likely to interact with again in the future and proactively decrypt the live timeline.
This could be scrapped if we wanted to further improve app start performance at the cost of a few layout shift as highlighted in this video

Benchmark

# of e2ee rooms load time (before) load time (after) % difference
250 4mn50 31s 89%
43 37s 12s 67%

These recordings are taken on an existing session by looking at the time between a hard refresh until the console stops spamming "decrypted events" messages by the thousands.

Please share with me your recording and I will add them to the table above, the code is up and running on a netlify preview
If you want to know the number of encrypted rooms in your account you can run the following snippet in your terminal

Object.values(mxMatrixClientPeg.get().store.rooms).filter(room => {
  return room._client.isRoomEncrypted(room.roomId)
}).length;

src/sync.js Outdated Show resolved Hide resolved
@dbkr
Copy link
Member

dbkr commented May 6, 2021

in general this looks like a good approach I think

Copy link
Collaborator

@jryans jryans left a comment

Choose a reason for hiding this comment

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

Overall, this is looking reasonable so far. Do you have any data on how much performance improves with this change in isolation?

src/sync.js Outdated Show resolved Hide resolved
@germain-gg
Copy link
Contributor Author

Do you have any data on how much performance improves with this change in isolation?

I have just done a benchmark using the same account. It takes down the load time from 4 minutes 50 seconds down to 30 seconds. During which the application is still usable, but with a degraded experience (sometimes the room spinner is missing, ...)

And below is a screen recording of the experience when we load a room where we need to lazily decrypt events

lazyloadevents.mov

src/models/room.js Outdated Show resolved Hide resolved
@turt2live turt2live self-requested a review May 6, 2021 15:42
@germain-gg germain-gg requested review from jryans and dbkr May 7, 2021 14:29
Copy link
Collaborator

@jryans jryans left a comment

Choose a reason for hiding this comment

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

Looking good overall to me! 😄

src/models/room.js Show resolved Hide resolved
@jryans
Copy link
Collaborator

jryans commented May 7, 2021

Ah yeah, as mentioned before, it would be good to have some kind of automated to verify rooms still decrypt as expected, either here or in the React layer (whichever makes more sense).

Copy link
Member

@turt2live turt2live left a comment

Choose a reason for hiding this comment

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

ftr, this seems to give about a 15x improvement for decrypting the important rooms on my account. Clicking on a room and having it pop in at that second is amazing 😍

I have profiles I can share privately of before and after if people are interested (though you need a ton of CPU to load them).

@germain-gg germain-gg changed the title Lazily decrypt event on room view Prioritise and reduce the amount of events decrypted on application startup May 10, 2021
@germain-gg germain-gg marked this pull request as ready for review May 10, 2021 17:18
@germain-gg germain-gg requested a review from a team May 10, 2021 17:18
Copy link
Member

@turt2live turt2live left a comment

Choose a reason for hiding this comment

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

Overall looks fine. Like the react-sdk, I mostly have lint concerns. The flow seems to be valid though. Thank you!

spec/test-utils.js Show resolved Hide resolved
src/models/event.js Show resolved Hide resolved
src/models/room.js Outdated Show resolved Hide resolved
src/models/room.js Outdated Show resolved Hide resolved
src/models/room.js Outdated Show resolved Hide resolved
Co-authored-by: Travis Ralston <travpc@gmail.com>
@germain-gg germain-gg requested a review from turt2live May 11, 2021 09:06
Copy link
Member

@turt2live turt2live left a comment

Choose a reason for hiding this comment

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

thanks :)

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.

4 participants