-
Notifications
You must be signed in to change notification settings - Fork 235
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
Optimize SwiftUIWrapperView #320
Conversation
77e5cee
to
2fce208
Compare
2fce208
to
a8caca1
Compare
public override var isHidden: Bool { | ||
didSet { | ||
if isHidden { | ||
hostingController.rootView = AnyView(EmptyView()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not ideal, but way faster than the previous strategy of giving every view an ID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also add an API in the future to opt-out of this behavior if someone doesn't need onAppear
or onDisappear
callbacks in their views, which would get them back ~20% CPU usage when scrolling fast 🤯
} | ||
|
||
// MARK: Fileprivate | ||
|
||
fileprivate var contentAndID: ContentAndID { | ||
didSet { | ||
hostingController.rootView = .init(content: contentAndID.content, id: contentAndID.id) | ||
hostingController.rootView = AnyView(contentAndID.content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I profiled and there was no perf hit from switching to AnyView
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice findings!
Details
This PR significantly reduces CPU use for people using SwiftUI views for their calendar items by making 2 optimizations:
UIHostingController
as a child view controller, and just work with its hosting view directlyonAppear
andonDisappear
callbacks - instead, set a hidden view's to just display anEmptyView
Together, these changes reduce CPU use by 35-40% when scrolling fast.
Related Issue
#299
Motivation and Context
Performance improvements.
How Has This Been Tested
Example app
Types of changes
Checklist