-
Notifications
You must be signed in to change notification settings - Fork 11
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
Reusability hint to improve reuse performance when composite components are present. #146
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public struct ReusabilityHintCombiner { | ||
internal var types: [Any.Type] | ||
|
||
internal init<R: Renderable>(root: R) { | ||
types = [root.componentType] | ||
} | ||
|
||
public mutating func combine<R: Renderable>(_ component: R) { | ||
types.append(component.componentType) | ||
component.makeReusabilityHint(using: &self) | ||
} | ||
|
||
__consuming func generate() -> String { | ||
andersio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return types.map(fullyQualifiedTypeName(of:)).joined(separator: ",") | ||
} | ||
|
||
__consuming func isCompatible(with other: __owned ReusabilityHintCombiner) -> Bool { | ||
return types.elementsEqual(other.types, by: ==) | ||
} | ||
} | ||
|
||
|
||
func fullyQualifiedTypeName(of type: Any.Type) -> String { | ||
/// NOTE: `String.init(reflecting:)` gives the fully qualified type name. | ||
// Tests would catch unexpeced type name printing behavior due to Swift runtime changes. | ||
return String(reflecting: type) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,15 @@ protocol BentoReusableView: AnyObject { | |
|
||
extension BentoReusableView { | ||
func bind(_ component: AnyRenderable?) { | ||
let oldComponent = self.component | ||
self.component = component | ||
|
||
if let component = component { | ||
let renderingView: UIView | ||
|
||
if let view = containedView, type(of: view) == component.viewType { | ||
if let oldComponent = oldComponent, | ||
let view = containedView, | ||
oldComponent.reusabilityHintCombiner.isCompatible(with: component.reusabilityHintCombiner) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not usually happen, should it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All visible items hit this path when the state changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeh |
||
renderingView = view | ||
} else { | ||
renderingView = component.viewType.generate() | ||
|
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.
Does it scan it through the hierarchy? e.g
C(children: [C(children: [A()], B()])
vs C(children: [C(children: [B()], A()])?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.
It doesn’t do so automatically. When you say
combiner.combine(child)
, A’s part of the call it would eventually invokemakeReusabilityHint(using:)
on the child. So as long as you combine every child using the combiner, and that all descendants implement this, the whole hierarchy would be walked over.This can be made less crumblesome if we have a protocol for composite component e.g.
CompositeRenderable
which is also useful for auto propagation of messages like*LifecycleAware
. But this can be done after this PR which lays the cornerstone.