performance improvements for AuthorLabel #1458 #1590
+41
−21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issues covered
#1458
Description
This is a small step in improving the scroll performance of the app and is meant to be a pattern check for other devs. If these changes are acceptable, then we could implement similar changes widely.
My goal here was to identify and address one small performance issue. Here is the profiling scenario to repeat between changes:
How to test
The "View body" instrument gives you the count and duration of SwiftUI view body methods for all the views in the app. If you sort the results by "Total Duration", you'll see the "worst performance offenders". The "Average Duration" is also very important to look at, but it must be considered in combination with the total number of redraws.
Here's how the ranking looks before the changes in this PR:
Here you see that
AuthorLabel
was drawn 29 times for a total of 33.32ms. This is miserable performance. At 60Hz, a single frame must take less than 16.67ms overall, so we want individual views' times to be MUCH less than that.After only removing the xcstringstool usage in
AuthorLabel
let's run the same test again:This time you see around the same number of redraws, but each one takes 1/10th the time! This is a lot better, but we're still adding up to milliseconds here, and we'd prefer to get it lower.
The next big performance improvement is to cache the result of the
attributedAuthor
method, which is taking too long, in a view-scoped cache. I also made another change to pass only the relevant properties ofAuthor
to this view, rather than the entire object. This is good practice regardless of performance.Now we see the performance we want to get from this view. Even with 22 redraws, the total time is on the order of hundreds of microseconds.