-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[ASLayout] Revisit the flattening algorithm #395
Merged
nguyenhuy
merged 5 commits into
TextureGroup:master
from
nguyenhuy:HNRemoveFlattenedFlag
Jun 29, 2017
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7b8040
Implement tests for the layout flattening process
nguyenhuy 44202d9
Refactor the flattening algorithm
nguyenhuy 4dd09fc
Update changelog
nguyenhuy 75bbb17
Ceil position values before comparing
nguyenhuy 95707da
Explain why sublayout elements must be retained
nguyenhuy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ extern BOOL ASPointIsNull(CGPoint point) | |
/** | ||
* Creates an defined number of " |" indent blocks for the recursive description. | ||
*/ | ||
static inline NSString * descriptionIndents(NSUInteger indents) | ||
ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT NSString * descriptionIndents(NSUInteger indents) | ||
{ | ||
NSMutableString *description = [NSMutableString string]; | ||
for (NSUInteger i = 0; i < indents; i++) { | ||
|
@@ -49,14 +49,31 @@ extern BOOL ASPointIsNull(CGPoint point) | |
return description; | ||
} | ||
|
||
ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT BOOL ASLayoutIsDisplayNodeType(ASLayout *layout) | ||
{ | ||
return layout.type == ASLayoutElementTypeDisplayNode; | ||
} | ||
|
||
ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT BOOL ASLayoutIsFlattened(ASLayout *layout) | ||
{ | ||
// A layout is flattened if its position is null, and all of its sublayouts are of type displaynode with no sublayouts. | ||
if (! ASPointIsNull(layout.position)) { | ||
return NO; | ||
} | ||
|
||
for (ASLayout *sublayout in layout.sublayouts) { | ||
if (ASLayoutIsDisplayNodeType(sublayout) == NO || sublayout.sublayouts.count > 0) { | ||
return NO; | ||
} | ||
} | ||
|
||
return YES; | ||
} | ||
|
||
@interface ASLayout () <ASDescriptionProvider> | ||
{ | ||
ASLayoutElementType _layoutElementType; | ||
} | ||
/** | ||
* A boolean describing if the current layout has been flattened. | ||
*/ | ||
@property (nonatomic, getter=isFlattened) BOOL flattened; | ||
|
||
/* | ||
* Caches all sublayouts if set to YES or destroys the sublayout cache if set to NO. Defaults to YES | ||
|
@@ -129,7 +146,6 @@ - (instancetype)initWithLayoutElement:(id<ASLayoutElement>)layoutElement | |
[_elementToRectTable setRect:layout.frame forKey:layout.layoutElement]; | ||
} | ||
|
||
_flattened = NO; | ||
self.retainSublayoutLayoutElements = [ASLayout shouldRetainSublayoutLayoutElements]; | ||
} | ||
|
||
|
@@ -173,14 +189,6 @@ + (instancetype)layoutWithLayoutElement:(id<ASLayoutElement>)layoutElement size: | |
sublayouts:nil]; | ||
} | ||
|
||
+ (instancetype)layoutWithLayout:(ASLayout *)layout position:(CGPoint)position | ||
{ | ||
return [self layoutWithLayoutElement:layout.layoutElement | ||
size:layout.size | ||
position:position | ||
sublayouts:layout.sublayouts]; | ||
} | ||
|
||
#pragma mark - Sublayout Elements Caching | ||
|
||
- (void)setRetainSublayoutLayoutElements:(BOOL)retainSublayoutLayoutElements | ||
|
@@ -207,7 +215,10 @@ - (void)setRetainSublayoutLayoutElements:(BOOL)retainSublayoutLayoutElements | |
|
||
- (ASLayout *)filteredNodeLayoutTree | ||
{ | ||
NSMutableArray *flattenedSublayouts = [NSMutableArray array]; | ||
if (ASLayoutIsFlattened(self)) { | ||
self.retainSublayoutLayoutElements = YES; | ||
return self; | ||
} | ||
|
||
struct Context { | ||
ASLayout *layout; | ||
|
@@ -216,28 +227,40 @@ - (ASLayout *)filteredNodeLayoutTree | |
|
||
// Queue used to keep track of sublayouts while traversing this layout in a DFS fashion. | ||
std::deque<Context> queue; | ||
queue.push_front({self, CGPointZero}); | ||
for (ASLayout *sublayout in self.sublayouts) { | ||
queue.push_back({sublayout, sublayout.position}); | ||
} | ||
|
||
NSMutableArray *flattenedSublayouts = [NSMutableArray array]; | ||
|
||
while (!queue.empty()) { | ||
Context context = queue.front(); | ||
const Context context = queue.front(); | ||
queue.pop_front(); | ||
|
||
if (self != context.layout && context.layout.type == ASLayoutElementTypeDisplayNode) { | ||
ASLayout *layout = [ASLayout layoutWithLayout:context.layout position:context.absolutePosition]; | ||
layout.flattened = YES; | ||
[flattenedSublayouts addObject:layout]; | ||
} | ||
|
||
std::vector<Context> sublayoutContexts; | ||
for (ASLayout *sublayout in context.layout.sublayouts) { | ||
if (sublayout.isFlattened == NO) { | ||
ASLayout *layout = context.layout; | ||
const NSArray<ASLayout *> *sublayouts = layout.sublayouts; | ||
const NSUInteger sublayoutsCount = sublayouts.count; | ||
const CGPoint absolutePosition = context.absolutePosition; | ||
|
||
if (ASLayoutIsDisplayNodeType(layout)) { | ||
if (sublayoutsCount > 0 || CGPointEqualToPoint(absolutePosition, layout.position) == NO) { | ||
// Only create a new layout if the existing one can't be reused, which means it has either some sublayouts or an invalid absolute position. | ||
layout = [ASLayout layoutWithLayoutElement:layout.layoutElement | ||
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. Is this the "aggressive reuse"? Is this case skipped often enough in practice to make a difference? Not sure of the performance impact of removing the flag but do we have a good guess as to the consequences of landing this PR? |
||
size:layout.size | ||
position:absolutePosition | ||
sublayouts:@[]]; | ||
} | ||
[flattenedSublayouts addObject:layout]; | ||
} else if (sublayoutsCount > 0){ | ||
std::vector<Context> sublayoutContexts; | ||
for (ASLayout *sublayout in sublayouts) { | ||
sublayoutContexts.push_back({sublayout, context.absolutePosition + sublayout.position}); | ||
} | ||
queue.insert(queue.cbegin(), sublayoutContexts.begin(), sublayoutContexts.end()); | ||
} | ||
queue.insert(queue.cbegin(), sublayoutContexts.begin(), sublayoutContexts.end()); | ||
} | ||
|
||
ASLayout *layout = [ASLayout layoutWithLayoutElement:_layoutElement size:_size position:CGPointZero sublayouts:flattenedSublayouts]; | ||
ASLayout *layout = [ASLayout layoutWithLayoutElement:_layoutElement size:_size sublayouts:flattenedSublayouts]; | ||
layout.retainSublayoutLayoutElements = YES; | ||
return layout; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
This seems like a strange side effect. Should we document why this needs to be the case?