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

Optimize layout flattening #982

Merged
merged 4 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Clean up unused/unneeded header macros. [Adlai Holler](https://github.com/Adlai-Holler)
- Clean up C-function `extern` decorators. [Adlai Holler](https://github.com/Adlai-Holler)
- Add an experiment to reduce work involved in collection teardown. [Adlai Holler](https://github.com/Adlai-Holler)
- Optimize layout flattening, particularly reducing retain/release operations. [Adlai Holler](https://github.com/Adlai-Holler)

## 2.7
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)
Expand Down
1 change: 1 addition & 0 deletions Source/Base/ASBaseDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import <Foundation/NSObjCRuntime.h>

#define AS_EXTERN FOUNDATION_EXTERN
#define unowned __unsafe_unretained
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we should just be consistent and use __unsafe_unretained in a framework.

Copy link
Member Author

Choose a reason for hiding this comment

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

Pros:

  • Less underscores
  • Shorter, cleaner

Cons:

  • Not standard
  • Could one day be a collision and we'd have to revert


#ifdef __GNUC__
# define ASDISPLAYNODE_GNUC(major, minor) \
Expand Down
68 changes: 35 additions & 33 deletions Source/Layout/ASLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@ ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT BOOL ASLayoutIsDisplayNodeType(ASLayo
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;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Had to make this a method so that it can see the _sublayouts ivar for direct access (remove retain/release pair per sublayout).

@interface ASLayout () <ASDescriptionProvider>
{
ASLayoutElementType _layoutElementType;
Expand Down Expand Up @@ -216,52 +200,70 @@ - (void)setRetainSublayoutLayoutElements:(BOOL)retainSublayoutLayoutElements

#pragma mark - Layout Flattening

- (BOOL)isFlattened
{
// A layout is flattened if its position is null, and all of its sublayouts are of type displaynode with no sublayouts.
if (! ASPointIsNull(_position)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove whitespace?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

return NO;
}

for (ASLayout *sublayout in _sublayouts) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would NSFastEnumeration allow us to cast that as unowned void * and cast down with a bridge to the type we need? In fact does it do a retain for the sub layouts in here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fast enumeration doesn't retain the objects.

if (ASLayoutIsDisplayNodeType(sublayout) == NO || sublayout->_sublayouts.count > 0) {
return NO;
}
}

return YES;
}

- (ASLayout *)filteredNodeLayoutTree NS_RETURNS_RETAINED
{
if (ASLayoutIsFlattened(self)) {
if ([self isFlattened]) {
// All flattened layouts must have this flag enabled
// to ensure sublayout elements are retained until the layouts are applied.
self.retainSublayoutLayoutElements = YES;
return self;
}

struct Context {
ASLayout *layout;
unowned ASLayout *layout;
CGPoint absolutePosition;
};

// Queue used to keep track of sublayouts while traversing this layout in a DFS fashion.
std::deque<Context> queue;
for (ASLayout *sublayout in self.sublayouts) {
for (ASLayout *sublayout in _sublayouts) {
queue.push_back({sublayout, sublayout.position});
}

NSMutableArray *flattenedSublayouts = [[NSMutableArray alloc] init];
auto flattenedSublayouts = [[NSMutableArray<ASLayout *> alloc] init];

while (!queue.empty()) {
const Context context = std::move(queue.front());
queue.pop_front();

ASLayout *layout = context.layout;
const NSArray<ASLayout *> *sublayouts = layout.sublayouts;
const NSUInteger sublayoutsCount = sublayouts.count;
unowned ASLayout *layout = context.layout;
const NSUInteger sublayoutsCount = layout->_sublayouts.count;
Copy link
Member Author

Choose a reason for hiding this comment

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

Direct ivar access isn't so much about avoiding the message-send, it's about avoiding the retain/release pair. _sublayouts is already a +1 so let's use it.

Copy link
Member

Choose a reason for hiding this comment

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

Mind adding this comment in code?

const CGPoint absolutePosition = context.absolutePosition;

if (ASLayoutIsDisplayNodeType(layout)) {
if (sublayoutsCount > 0 || CGPointEqualToPoint(ASCeilPointValues(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
size:layout.size
position:absolutePosition
sublayouts:@[]];
auto newLayout = [ASLayout layoutWithLayoutElement:layout->_layoutElement
size:layout.size
position:absolutePosition
sublayouts:@[]];
[flattenedSublayouts addObject:newLayout];
} else {
[flattenedSublayouts addObject:layout];
}
[flattenedSublayouts addObject:layout];
} else if (sublayoutsCount > 0){
std::vector<Context> sublayoutContexts;
for (ASLayout *sublayout in sublayouts) {
sublayoutContexts.push_back({sublayout, absolutePosition + sublayout.position});
} else if (sublayoutsCount > 0) {
// Fast-reverse-enumerate the sublayouts array by copying it into a C-array and push_front'ing each into the queue.
unowned ASLayout *rawSublayouts[sublayoutsCount];
[layout->_sublayouts getObjects:rawSublayouts range:NSMakeRange(0, sublayoutsCount)];
for (NSInteger i = sublayoutsCount - 1; i >= 0; i--) {
queue.push_front({rawSublayouts[i], absolutePosition + rawSublayouts[i].position});
}
queue.insert(queue.cbegin(), sublayoutContexts.begin(), sublayoutContexts.end());
Copy link
Member Author

Choose a reason for hiding this comment

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

The bottom here (the search expansion) is far and away the biggest win. We no longer create another vector (heap allocation) and we remove 2 retain/release pairs per sublayout. The first is inserting into the vector then destroying the vector, the second is inserting into the queue and then removing from the queue.

}
}

Expand Down