-
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
Introduce ASCollectionGalleryLayoutDelegate #76
Introduce ASCollectionGalleryLayoutDelegate #76
Conversation
237dd9f
to
941fb2a
Compare
0f39b76
to
4af0eb0
Compare
@Adlai-Holler @maicki @garrettmoon @appleguy: This PR is ready for review! |
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.
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?
97732ca
to
89a4d2b
Compare
- 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.
89a4d2b
to
a82a0be
Compare
- 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
@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 Because of that, the elements-to-layout-attributes map after 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 |
@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 I'm gonna work on preventing |
@@ -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]; |
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.
Stray line
Source/Private/ASCollectionLayout.mm
Outdated
/** | ||
* 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) |
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.
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.
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.
Yeap, changed to a class method.
Source/Details/ASPageTable.m
Outdated
|
||
for (id key in self) { | ||
[result setObject:[[self objectForKey:key] copy] forKey:key]; | ||
} |
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.
Two performance questions here that deserve some profiling or thought:
initWithKeyOptions:valueOptions:
is optimized compared toinitWithKeyPointerFunctions:valuePointerFunctions:
. I don't know whether the resulting storage also performs differently.self.keyPointerFunctions
andself.valuePointerFunctions
both create newNSPointerFunctions
objects.
Now that measurement and layoutAttributes are totally disconnected, not sure if we even need this. Right?
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.
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.
Source/Details/ASDataController.h
Outdated
*/ | ||
- (void)prepareLayoutWithContext:(id)context; | ||
- (ASCollectionLayoutState *)calculateLayoutWithContext:(ASCollectionLayoutContext *)context; |
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.
What do you think about making this a class method to encourage immutable state?
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 played around with this idea and in the end I actually like it. Thanks for the suggestion!
1115f9d
to
9835fe6
Compare
068fa68
to
e4ffbee
Compare
- 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.
e4ffbee
to
5950a9f
Compare
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.
Wonderful! I love to see the reduction in state and in information passing! Let's get this deployed and battle-tested!
Generated by 🚫 Danger |
Awesome, thanks so much for the reviews! Pushed a 1 line change, gonna merge after BuildKite passes. |
* 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
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:
Layout delegate to take over node allocation responsibility fromASDataController
.ASRangeManager
, thus by pass layout inspector completely.RevisitASCollectionViewLayoutController
and its friends.Ticket: #186