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 typing performance by avoiding expensive UI operations #5913

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

Anderas
Copy link
Contributor

@Anderas Anderas commented Mar 24, 2022

Relates to #5906

Root cause

There are a few things going on here, and all related to an insane amount of spaghetti layout code in RoomViewController. Often time a simple sounding method will internally call other less innocent code, escalating the performance cost This is what I observed:

  1. On each viewDidLayoutSubview we will try to scroll back to the bottom, even if the size of the view has not really changed. The scrolling itself would be fine, but it is one of those methods that trigger a number of other heavy updates (e.g. updateViewControllerAppearanceOnRoomDataSourceState). Note that viewDidLayoutSubview is called every time you type a character, which means that when typing fast we are essentially doing all of this expensive work many times per second.
  2. I think that a recent change of calling refreshRoomTitle more often (I eat my words about no performance impact) has made this even worse, because refreshRoomTitle is one of those very long methods with ton of side effects that are not easy to see. This is I believe also why we started seeing this issue worse in the latest release.

Solution

The solution to this is patchwork, because the code is too messy for a simple fix:

Before my changes typing rapidly would consume around 33% of CPU on iPhone 13 Pro. After the changes it dropped to around 17%. We can certainly do more to bring this down.

I am not able to confirm yet whether this also solves the battery drain issue, but using CPU must be at least partly responsible for it.

@Anderas Anderas requested review from a team, ismailgulek and gileluard and removed request for a team and ismailgulek March 24, 2022 12:58
Copy link
Contributor

@gileluard gileluard left a comment

Choose a reason for hiding this comment

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

LGTM 👍 I would recommend to use UIView.bounds though.

}

- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
BOOL didViewChangeFrame = !CGRectEqualToRect(lastViewFrame, self.view.frame);
lastViewFrame = self.view.frame;
Copy link
Contributor

Choose a reason for hiding this comment

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

I do agree that viewDidLayoutSubviews can be called for many reasons and very often indeed. Storing the last view frame is fine but I usually store the last bounds of the view as we're interested by the dimension of the view more than its position.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea thats valid, changed.

@github-actions
Copy link

github-actions bot commented Mar 24, 2022

📱 Scan the QR code below to install the build for this PR.
🔒 This build is for internal testing purpose. Only devices listed in the ad-hoc provisioning profile can install Element Alpha.

QR code

If you can't scan the QR code you can install the build via this link: https://i.diawi.com/CY26t6

@Anderas Anderas requested a review from gileluard March 24, 2022 14:24
Copy link
Contributor

@gileluard gileluard left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@Anderas Anderas merged commit a0b7029 into develop Mar 24, 2022
@Anderas Anderas deleted the andy/5906_typing_perf branch March 24, 2022 15:14
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