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

[ASDisplayNode] -didEnterPreloadState does not need to call -layoutIfNeeded #trivial #411

Merged
merged 1 commit into from
Jul 5, 2017

Conversation

appleguy
Copy link
Member

@appleguy appleguy commented Jul 3, 2017

This was originally added for ASCollectionNode and ASTableNode preloading to work
as intended when nested inside of another ASRangeController-powered node. Indeed,
it is still necessary to trigger layout on these UIKit-backed components in order
for their own ASRangeControllers to start preparing content within them.

However, unlike the comment suggests, it is not necessary to do this for image
nodes. ASNetworkImageNode has only one .URL, and does not use the .bounds or
.calculatedLayout at all during loading. Even the ASMultiplexImageNode does not
use the .bounds, and the ASMapNode uses .calculatedLayout instead of .bounds.

This change has important performance benefits. In particular, it avoids
layouts that would occur on the main thread, often including text sizing,
and also can result in redundant layout passes (even during a layout pass that
triggers a node to enter the preload range, it may force its own layout early).

It would be great to test this change with Pinterest to confirm its safety, but
based on a full audit of the framework codebase, the only possibility that I
see for a regression is if app implementations of -didEnterPreloadState make
direct use of .bounds instead of .calculatedLayout (which is easy to fix).

…Needed #trivial

This was originally added for ASCollectionNode and ASTableNode preloading to work
as intended when nested inside of another ASRangeController-powered node. Indeed,
it is still necessary to trigger layout on these UIKit-backed components in order
for their own ASRangeControllers to start preparing content within them.

However, unlike the comment suggests, it is *not* necessary to do this for image
nodes. ASNetworkImageNode has only one .URL, and does not use the .bounds or
.calculatedLayout at all during loading. Even the ASMultiplexImageNode does not
use the .bounds, and the ASMapNode uses .calculatedLayout instead of .bounds.

This change has important performance benefits. In particular, it avoids
layouts that would occur on the main thread, often including text sizing,
and also can result in redundant layout passes (even during a layout pass that
triggers a node to enter the preload range, it may force its own layout early).

It would be great to test this change with Pinterest to confirm its safety, but
based on a full audit of the framework codebase, the only possibility that I
see for a regression is if app implementations of -didEnterPreloadState make
direct use of .bounds instead of .calculatedLayout (which is easy to fix).
@appleguy
Copy link
Member Author

appleguy commented Jul 3, 2017

Note that a layout pass will be forced when reaching the Display range anyway, via recursivelyEnsureDisplaySynchronously:NO which is called on the ASCellNode at that time. If the layout hasn't changed by the time it is Visible, then no additional work should be performed.

Copy link
Contributor

@maicki maicki left a comment

Choose a reason for hiding this comment

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

@appleguy Just had a small question while browsing over the change. But if this question turns out to be not really relevant, it's really awesome you found unnecessary work we can safely remove.

@@ -2915,10 +2915,6 @@ - (void)didEnterPreloadState
ASDisplayNodeAssertLockUnownedByCurrentThread(__instanceLock__);
[_interfaceStateDelegate didEnterPreloadState];

// Trigger a layout pass to ensure all subnodes have the correct size to preload their content.
// This is important for image nodes, as well as collection and table nodes.
[self layoutIfNeeded];
Copy link
Contributor

Choose a reason for hiding this comment

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

If we remove layoutIfNeeded in this case would user with custom ASDisplayNode subclass that relied on the fact that the size was available via bounds / calculatedSize suddenly get an undefined behavior?

They would have to basically do the same as ASTableNode and ASCollectionNode and explicitly allocating the view and trigger a layout if needed if they would like to have the old behavior?

Copy link
Member Author

Choose a reason for hiding this comment

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

@maicki Since this sets it in a downward propagation, it wouldn't necessarily have set the bounds on self, unless the parent had done so first.

Assuming the parent had done so, then yes this could be a behavior change. However, using .calculatedSize actually will not change in behavior.

I think it is quite rare to override this method, and even more rare to use .bounds in it. One potential option could be to throw an assertion if a client DOES access .bounds during this call, and to tell them "this probably won't do what you want, use .calculatedLayout.size instead".

Copy link
Member

@nguyenhuy nguyenhuy left a comment

Choose a reason for hiding this comment

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

Good point on calculatedSize being available and should be used by subclasses.

Approving because this behavior was introduced just 3 months ago (facebookarchive/AsyncDisplayKit#3263). So we're talking about a 3-month window in which developers has been allowed to use the bounds in - (void)didEnterPreloadState, which I reckon very few (if any) did. I did a quick check on Pinterest code base and we don't do it.

@nguyenhuy
Copy link
Member

Would love to have input from @Adlai-Holler before merging.

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.

I think this change is acceptable. Clients who depend on self.bounds to be up-to-date can either trigger the early layout themselves or rely on the size of the calculated layout.

@nguyenhuy nguyenhuy merged commit 03592e0 into master Jul 5, 2017
bernieperez pushed a commit to AtomTickets/Texture that referenced this pull request Apr 25, 2018
…Needed #trivial (TextureGroup#411)

This was originally added for ASCollectionNode and ASTableNode preloading to work
as intended when nested inside of another ASRangeController-powered node. Indeed,
it is still necessary to trigger layout on these UIKit-backed components in order
for their own ASRangeControllers to start preparing content within them.

However, unlike the comment suggests, it is *not* necessary to do this for image
nodes. ASNetworkImageNode has only one .URL, and does not use the .bounds or
.calculatedLayout at all during loading. Even the ASMultiplexImageNode does not
use the .bounds, and the ASMapNode uses .calculatedLayout instead of .bounds.

This change has important performance benefits. In particular, it avoids
layouts that would occur on the main thread, often including text sizing,
and also can result in redundant layout passes (even during a layout pass that
triggers a node to enter the preload range, it may force its own layout early).

It would be great to test this change with Pinterest to confirm its safety, but
based on a full audit of the framework codebase, the only possibility that I
see for a regression is if app implementations of -didEnterPreloadState make
direct use of .bounds instead of .calculatedLayout (which is easy to fix).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants