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

Introduce ASCollectionGalleryLayoutDelegate #76

Merged
merged 25 commits into from
Jul 14, 2017

Conversation

nguyenhuy
Copy link
Member

@nguyenhuy nguyenhuy commented Apr 27, 2017

  • ASCollectionGalleryLayoutDelegate arranges items of the same size into a multi-line stack (say photo gallery or pager). It takes advantage of the fact that its items always have a fixed size to measure as few items as possible while still being able to track their positions at all time. This helps reduce startup/reloadData time, as well as memory footprint.
  • It then uses a measure range, which also works as a allocate range, to figure out which items to measure ahead of time. And it guarantees that each item is scheduled to measure only once.
  • Lastly, ASCollectionLayoutDelegate has some new methods that allow delegates to hook up and stay ahead of layout attributes requests from the backing view. ASCollectionGalleryLayoutDelegate for example uses these methods to ensure elements that have their layout attributes requested are always ready for consumption, and to measure more elements in the background.

Follow up tasks:

  • Unit tests (for real!).
  • Layout delegate to take over node allocation responsibility from ASDataController.
  • Let layout delegate report scrollable directions directly to ASRangeManager, thus by pass layout inspector completely.
  • Revisit ASCollectionViewLayoutController and its friends.

Ticket: #186

@nguyenhuy nguyenhuy force-pushed the HNGalleryLayoutDelegate branch 4 times, most recently from 237dd9f to 941fb2a Compare April 28, 2017 10:32
@nguyenhuy nguyenhuy mentioned this pull request Apr 28, 2017
@nguyenhuy nguyenhuy changed the title Introduce ASCollectionGalleryLayoutDelegate [HOLD] Introduce ASCollectionGalleryLayoutDelegate Apr 28, 2017
@nguyenhuy nguyenhuy force-pushed the HNGalleryLayoutDelegate branch 3 times, most recently from 0f39b76 to 4af0eb0 Compare May 4, 2017 12:29
@nguyenhuy nguyenhuy changed the title [HOLD] Introduce ASCollectionGalleryLayoutDelegate Introduce ASCollectionGalleryLayoutDelegate May 4, 2017
@nguyenhuy
Copy link
Member Author

@Adlai-Holler @maicki @garrettmoon @appleguy: This PR is ready for review!

Copy link
Member

@Adlai-Holler Adlai-Holler left a comment

Choose a reason for hiding this comment

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

Rather than add API to the CollectionLayoutDelegate, it seems that this logic isn't specific to the gallery layout. Any collection should probably have this same behavior – ensuring that nodes are measured asynchronously in advance. In the case that the node was already measured during the calculation, we'll hit the cache and it'll go fast.

So why not move this logic up into ASCollectionLayout or ASDataController and remove the new ensure methods from the API?

- It arranges items of the same size into a multi-line stack (say photo gallery or pager). It takes advantage of the fact that its items always have a fixed size to measure as few items as possible while still being able to track their positions at all time. This helps reduce startup/reloadData time, as well as memory footprint.
- It then uses a measure range, which also works as a allocate range, to figure out which items to measure ahead of time. And it guarantees that each item is scheduled to measure only once.
- Lastly, ASCollectionLayoutDelegate has some new methods that allow delegates to hook up and stay ahead of layout attributes requests from the backing view. ASCollectionGalleryLayoutDelegate for example uses these methods to ensure elements that have their layout attributes requested are always ready for consumption, and to measure more elements in the background.
- Collection layout delegates must have a crollable directions property.
- Simplify gallery delegate by not storing unmeasured attributes since calling measure on already measured elements should be cache hits and super fast.
- Abstact some code in gallery delegate to ASCollectionLayoutState+Private and _ASCollectionGalleryLayoutItem.
- Other improvements in gallery delegate
@nguyenhuy
Copy link
Member Author

@Adlai-Holler To answer your question here:

The answer is that, not all layout delegates want to calculate layout attributes for all elements at the beginning, and I don't want to enforce that. The only requirement for them is that by the time -calculateLayoutWithContext returns, there must be a content size calculated. How does a delegate come up with that number is totally up to it. It may measure and position all elements at the beginning (masonry layout), it may position all elements and measure some of them (like the gallery layout here), or it may position and measure only a very few of them (such as a pager layout).

Because of that, the elements-to-layout-attributes map after -calculateLayoutWithContext may not contain enough information to determine which elements fall into a given rect. Calling the "ensure" methods at appropriate time makes sure that the map is updated by the layout delegate. In addition, the delegate may, or may not, take advantage of that moment to measure some more elements. That's entirely up to its implementation because it's the one that knows best about its layout.

I see your point for putting the measure range logic into a place where clients can take advantage of. Later, we may consider putting it into an abstract layout delegate that gallery delegate then subclasses. I just don't want to put it in ASCollectionLayout or ASDataController because then the framework needs to make certain assumptions/restrictions on the API which in turn makes it less extensible.

@TextureGroup TextureGroup deleted a comment Jul 12, 2017
@TextureGroup TextureGroup deleted a comment Jul 12, 2017
@TextureGroup TextureGroup deleted a comment Jul 12, 2017
@nguyenhuy
Copy link
Member Author

@Adlai-Holler Thinking again, I agree with you that it's better to limit mutations and require clients to always return final rects for all items via -calculateLayoutWithContext:. I've updated this PR to move the measure logic to ASCollectionLayout.

I'm gonna work on preventing ASDataController from allocating nodes since either ASCollectionLayout or its delegate can handle that quite effortlessly. This work shouldn't block this PR, so please go ahead with another round of review. Thank you!

@@ -44,7 +44,10 @@ - (void)viewDidLoad
{
[super viewDidLoad];

self.collectionNode = [[ASCollectionNode alloc] initWithLayoutDelegate:[[ASCollectionFlowLayoutDelegate alloc] init] layoutFacilitator:nil];
id<ASCollectionLayoutDelegate> layoutDelegate = [[ASCollectionGalleryLayoutDelegate alloc] initWithScrollableDirections:ASScrollDirectionVerticalDirections itemSize:CGSizeMake(180, 90)];
// id<ASCollectionLayoutDelegate> layoutDelegate = [[ASCollectionFlowLayoutDelegate alloc] init];
Copy link
Member

Choose a reason for hiding this comment

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

Stray line

/**
* Measures all elements in the specified rect and blocks the calling thread while measuring those in the blocking rect.
*/
ASDISPLAYNODE_INLINE void ASCollectionLayoutMeasureElementsInRects(CGRect rect, CGRect blockingRect, ASCollectionLayoutState *layout)
Copy link
Member

Choose a reason for hiding this comment

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

There's too much code in this function IMO for it to be inlined. I'd make it a plain old C function or a class method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeap, changed to a class method.


for (id key in self) {
[result setObject:[[self objectForKey:key] copy] forKey:key];
}
Copy link
Member

Choose a reason for hiding this comment

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

Two performance questions here that deserve some profiling or thought:

  • initWithKeyOptions:valueOptions: is optimized compared to initWithKeyPointerFunctions:valuePointerFunctions:. I don't know whether the resulting storage also performs differently.
  • self.keyPointerFunctions and self.valuePointerFunctions both create new NSPointerFunctions objects.

Now that measurement and layoutAttributes are totally disconnected, not sure if we even need this. Right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Valid questions!

I ended up removing this method after implementing a method that walks through the table and gathers unmeasured layout attributes, which is what I should have done in the first place.

*/
- (void)prepareLayoutWithContext:(id)context;
- (ASCollectionLayoutState *)calculateLayoutWithContext:(ASCollectionLayoutContext *)context;
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about making this a class method to encourage immutable state?

Copy link
Member Author

Choose a reason for hiding this comment

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

I played around with this idea and in the end I actually like it. Thanks for the suggestion!

@TextureGroup TextureGroup deleted a comment Jul 13, 2017
@TextureGroup TextureGroup deleted a comment Jul 13, 2017
@TextureGroup TextureGroup deleted a comment Jul 13, 2017
@nguyenhuy nguyenhuy force-pushed the HNGalleryLayoutDelegate branch 3 times, most recently from 068fa68 to e4ffbee Compare July 13, 2017 18:27
@TextureGroup TextureGroup deleted a comment Jul 13, 2017
- Make -calculateLayoutWithContext: to be class methods in ASDataControllerLayoutDelegate and ASCollectionLayoutDelegate.
- Add layout delegate class and layout cache to ASCollectionLayoutContext+Private, to be use by ASCollectionLayout only.
- ASDataController no longer allocates all nodes but lets ASCollectionLayout determine.
- Add scrollableDirections to the layout context since it's often needed by the layout pass. Otherwise users have to wrap it in an info object.
- Update built-in layout delegates and CustomCollectionView example.
- Publish ASHashing. It might be helpful for clients that implement custom collection info objects.
Copy link
Member

@Adlai-Holler Adlai-Holler left a comment

Choose a reason for hiding this comment

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

Wonderful! I love to see the reduction in state and in information passing! Let's get this deployed and battle-tested!

@ghost
Copy link

ghost commented Jul 14, 2017

1 Warning
⚠️ This is a big PR, please consider splitting it up to ease code review.

Generated by 🚫 Danger

@nguyenhuy
Copy link
Member Author

Awesome, thanks so much for the reviews! Pushed a 1 line change, gonna merge after BuildKite passes.

@nguyenhuy nguyenhuy merged commit 3ccc2f0 into TextureGroup:master Jul 14, 2017
bernieperez pushed a commit to AtomTickets/Texture that referenced this pull request Apr 25, 2018
* Implement ASCollectionGalleryLayoutDelegate
- It arranges items of the same size into a multi-line stack (say photo gallery or pager). It takes advantage of the fact that its items always have a fixed size to measure as few items as possible while still being able to track their positions at all time. This helps reduce startup/reloadData time, as well as memory footprint.
- It then uses a measure range, which also works as a allocate range, to figure out which items to measure ahead of time. And it guarantees that each item is scheduled to measure only once.
- Lastly, ASCollectionLayoutDelegate has some new methods that allow delegates to hook up and stay ahead of layout attributes requests from the backing view. ASCollectionGalleryLayoutDelegate for example uses these methods to ensure elements that have their layout attributes requested are always ready for consumption, and to measure more elements in the background.

* Handle items that span multiple pages and other improvements in gallery delegate

* Minor fixes

* Fix failing tests

* Fix custom collection example

* Implement missing method in gallery layout delegate

* Fix warnings

* Some improvements
- Collection layout delegates must have a crollable directions property.
- Simplify gallery delegate by not storing unmeasured attributes since calling measure on already measured elements should be cache hits and super fast.
- Abstact some code in gallery delegate to ASCollectionLayoutState+Private and _ASCollectionGalleryLayoutItem.
- Other improvements in gallery delegate

* Fix file licenses

* Move measure range logic to ASCollectionLayout

* Track unmeasured elements

* Remove pending layout in ASCollectionLayout

* Get back pending layout because the timing to latch new data is not ideal

* Add ASCollectionLayoutCache

* Fix file licenses

* Fix xcodeproj

* Add async collection layout to examples/ASCollectionView

* Measure method in ASCollectionLayout to be a class method

* Encourage more immutable states
- Make -calculateLayoutWithContext: to be class methods in ASDataControllerLayoutDelegate and ASCollectionLayoutDelegate.
- Add layout delegate class and layout cache to ASCollectionLayoutContext+Private, to be use by ASCollectionLayout only.
- ASDataController no longer allocates all nodes but lets ASCollectionLayout determine.
- Add scrollableDirections to the layout context since it's often needed by the layout pass. Otherwise users have to wrap it in an info object.
- Update built-in layout delegates and CustomCollectionView example.
- Publish ASHashing. It might be helpful for clients that implement custom collection info objects.

* Remove additionalInfo property in ASCollectionLayoutState

* ASCollectionLayoutState to correctly filter unmeasured elements

* Add ASHashing to umbrella header

* Fix file licenses

* Add ASDispatchAsync and use it in ASCollectionLayout

* Improve code comment in ASCollectionLayoutState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants