Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 0feaa2a

Browse files
author
Scott Goodson
committed
Improvements to the efficiency of recursivelySetInterfaceState: and the beta range controller.
1 parent 03d13b1 commit 0feaa2a

9 files changed

Lines changed: 103 additions & 47 deletions

File tree

AsyncDisplayKit.xcodeproj/project.pbxproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@
495495
remoteGlobalIDString = 058D09AB195D04C000B7D73C;
496496
remoteInfo = AsyncDisplayKit;
497497
};
498+
DEACA2B11C425DC400FA9DDF /* PBXContainerItemProxy */ = {
499+
isa = PBXContainerItemProxy;
500+
containerPortal = 058D09A4195D04C000B7D73C /* Project object */;
501+
proxyType = 1;
502+
remoteGlobalIDString = 058D09AB195D04C000B7D73C;
503+
remoteInfo = AsyncDisplayKit;
504+
};
498505
/* End PBXContainerItemProxy section */
499506

500507
/* Begin PBXCopyFilesBuildPhase section */
@@ -1524,6 +1531,7 @@
15241531
buildRules = (
15251532
);
15261533
dependencies = (
1534+
DEACA2B21C425DC400FA9DDF /* PBXTargetDependency */,
15271535
);
15281536
name = AsyncDisplayKitTestHost;
15291537
productName = AsyncDisplayKitTestHost;
@@ -1939,6 +1947,11 @@
19391947
target = 058D09AB195D04C000B7D73C /* AsyncDisplayKit */;
19401948
targetProxy = 058D09C2195D04C000B7D73C /* PBXContainerItemProxy */;
19411949
};
1950+
DEACA2B21C425DC400FA9DDF /* PBXTargetDependency */ = {
1951+
isa = PBXTargetDependency;
1952+
target = 058D09AB195D04C000B7D73C /* AsyncDisplayKit */;
1953+
targetProxy = DEACA2B11C425DC400FA9DDF /* PBXContainerItemProxy */;
1954+
};
19421955
/* End PBXTargetDependency section */
19431956

19441957
/* Begin PBXVariantGroup section */

AsyncDisplayKit.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings

Lines changed: 0 additions & 8 deletions
This file was deleted.

AsyncDisplayKit/ASDisplayNode.mm

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,22 @@ + (Class)layerClass
198198
return [_ASDisplayLayer class];
199199
}
200200

201-
+ (void)scheduleNodeForDisplay:(ASDisplayNode *)node
201+
+ (void)scheduleNodeForRecursiveDisplay:(ASDisplayNode *)node
202202
{
203203
ASDisplayNodeAssertMainThread();
204+
ASDisplayNodeAssert([ASDisplayNode shouldUseNewRenderingRange], @"+scheduleNodeForRecursiveDisplay: should never be called without the new rendering range enabled");
204205
static NSMutableSet *nodesToDisplay = nil;
205206
static BOOL displayScheduled = NO;
206207
static ASDN::RecursiveMutex displaySchedulerLock;
208+
207209
{
208210
ASDN::MutexLocker l(displaySchedulerLock);
209211
if (!nodesToDisplay) {
210212
nodesToDisplay = [[NSMutableSet alloc] init];
211213
}
212214
[nodesToDisplay addObject:node];
213215
}
216+
214217
if (!displayScheduled) {
215218
displayScheduled = YES;
216219
// It's essenital that any layout pass that is scheduled during the current
@@ -1557,13 +1560,11 @@ - (void)recursivelyEnsureDisplaySynchronously:(BOOL)synchronously
15571560

15581561
- (void)setShouldBypassEnsureDisplay:(BOOL)shouldBypassEnsureDisplay
15591562
{
1560-
ASDN::MutexLocker l(_propertyLock);
15611563
_flags.shouldBypassEnsureDisplay = shouldBypassEnsureDisplay;
15621564
}
15631565

15641566
- (BOOL)shouldBypassEnsureDisplay
15651567
{
1566-
ASDN::MutexLocker l(_propertyLock);
15671568
return _flags.shouldBypassEnsureDisplay;
15681569
}
15691570

@@ -1612,7 +1613,7 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
16121613
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
16131614
{
16141615
ASDisplayNodeAssertThreadAffinity(self);
1615-
return [ASLayoutSpec new];
1616+
return nil;
16161617
}
16171618

16181619
- (ASLayout *)calculatedLayout
@@ -1772,7 +1773,7 @@ - (void)setInterfaceState:(ASInterfaceState)newState
17721773
oldState = _interfaceState;
17731774
_interfaceState = newState;
17741775
}
1775-
1776+
17761777
if ((newState & ASInterfaceStateMeasureLayout) != (oldState & ASInterfaceStateMeasureLayout)) {
17771778
// Trigger asynchronous measurement if it is not already cached or being calculated.
17781779
}
@@ -1782,8 +1783,11 @@ - (void)setInterfaceState:(ASInterfaceState)newState
17821783
// Still, the interfaceState should be updated to the current state of the node; just don't act on the transition.
17831784

17841785
// Entered or exited data loading state.
1785-
if ((newState & ASInterfaceStateFetchData) != (oldState & ASInterfaceStateFetchData)) {
1786-
if (newState & ASInterfaceStateFetchData) {
1786+
BOOL nowFetchData = ASInterfaceStateIncludesFetchData(newState);
1787+
BOOL wasFetchData = ASInterfaceStateIncludesFetchData(oldState);
1788+
1789+
if (nowFetchData != wasFetchData) {
1790+
if (nowFetchData) {
17871791
[self fetchData];
17881792
} else {
17891793
if ([self supportsRangeManagedInterfaceState]) {
@@ -1793,21 +1797,42 @@ - (void)setInterfaceState:(ASInterfaceState)newState
17931797
}
17941798

17951799
// Entered or exited contents rendering state.
1796-
if ((newState & ASInterfaceStateDisplay) != (oldState & ASInterfaceStateDisplay)) {
1800+
BOOL nowDisplay = ASInterfaceStateIncludesDisplay(newState);
1801+
BOOL wasDisplay = ASInterfaceStateIncludesDisplay(oldState);
1802+
1803+
if (nowDisplay != wasDisplay) {
17971804
if ([self supportsRangeManagedInterfaceState]) {
1798-
if (newState & ASInterfaceStateDisplay) {
1805+
if (nowDisplay) {
17991806
// Once the working window is eliminated (ASRangeHandlerRender), trigger display directly here.
18001807
[self setDisplaySuspended:NO];
18011808
} else {
18021809
[self setDisplaySuspended:YES];
18031810
[self clearContents];
18041811
}
1812+
} else {
1813+
// NOTE: This case isn't currently supported as setInterfaceState: isn't exposed externally, and all
1814+
// internal use cases are range-managed. When a node is visible, don't mess with display - CA will start it.
1815+
if ([ASDisplayNode shouldUseNewRenderingRange] && !ASInterfaceStateIncludesVisible(newState)) {
1816+
// Check __implementsDisplay purely for efficiency - it's faster even than calling -asyncLayer.
1817+
if ([self __implementsDisplay]) {
1818+
if (nowDisplay) {
1819+
[ASDisplayNode scheduleNodeForRecursiveDisplay:self];
1820+
} else {
1821+
[[self asyncLayer] cancelAsyncDisplay];
1822+
[self clearContents];
1823+
}
1824+
}
1825+
}
18051826
}
18061827
}
18071828

1808-
// Entered or exited data loading state.
1809-
if ((newState & ASInterfaceStateVisible) != (oldState & ASInterfaceStateVisible)) {
1810-
if (newState & ASInterfaceStateVisible) {
1829+
// Became visible or invisible. When range-managed, this represents literal visibility - at least one pixel
1830+
// is onscreen. If not range-managed, we can't guarantee more than the node being present in an onscreen window.
1831+
BOOL nowVisible = ASInterfaceStateIncludesVisible(newState);
1832+
BOOL wasVisible = ASInterfaceStateIncludesVisible(oldState);
1833+
1834+
if (nowVisible != wasVisible) {
1835+
if (nowVisible) {
18111836
[self visibilityDidChange:YES];
18121837
} else {
18131838
[self visibilityDidChange:NO];
@@ -1843,11 +1868,23 @@ - (void)exitInterfaceState:(ASInterfaceState)interfaceState
18431868

18441869
- (void)recursivelySetInterfaceState:(ASInterfaceState)interfaceState
18451870
{
1871+
ASInterfaceState oldState = self.interfaceState;
1872+
ASInterfaceState newState = interfaceState;
18461873
ASDisplayNodePerformBlockOnEveryNode(nil, self, ^(ASDisplayNode *node) {
18471874
node.interfaceState = interfaceState;
18481875
});
1849-
// FIXME: This should also be called in setInterfaceState: if it isn't being applied recursively.
1850-
[ASDisplayNode scheduleNodeForDisplay:self];
1876+
1877+
if ([self supportsRangeManagedInterfaceState]) {
1878+
// Instead of each node in the recursion assuming it needs to schedule itself for display,
1879+
// setInterfaceState: skips this when handling range-managed nodes (our whole subtree has this set).
1880+
// If our range manager intends for us to be displayed right now, and didn't before, get started!
1881+
1882+
BOOL nowDisplay = ASInterfaceStateIncludesDisplay(newState);
1883+
BOOL wasDisplay = ASInterfaceStateIncludesDisplay(oldState);
1884+
if (nowDisplay && (nowDisplay != wasDisplay)) {
1885+
[ASDisplayNode scheduleNodeForRecursiveDisplay:self];
1886+
}
1887+
}
18511888
}
18521889

18531890
- (ASHierarchyState)hierarchyState

AsyncDisplayKit/ASDisplayNodeExtras.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
#import <AsyncDisplayKit/ASBaseDefines.h>
1313
#import <AsyncDisplayKit/ASDisplayNode.h>
1414

15+
// Because inline methods can't be extern'd and need to be part of the translation unit of code
16+
// that compiles with them to actually inline, we both declare and define these in the header.
17+
inline BOOL ASInterfaceStateIncludesVisible(ASInterfaceState interfaceState)
18+
{
19+
return ((interfaceState & ASInterfaceStateVisible) == ASInterfaceStateVisible);
20+
}
21+
22+
inline BOOL ASInterfaceStateIncludesDisplay(ASInterfaceState interfaceState)
23+
{
24+
return ((interfaceState & ASInterfaceStateDisplay) == ASInterfaceStateDisplay);
25+
}
26+
27+
inline BOOL ASInterfaceStateIncludesFetchData(ASInterfaceState interfaceState)
28+
{
29+
return ((interfaceState & ASInterfaceStateFetchData) == ASInterfaceStateFetchData);
30+
}
31+
1532
NS_ASSUME_NONNULL_BEGIN
1633

1734
ASDISPLAYNODE_EXTERN_C_BEGIN

AsyncDisplayKit/Details/ASRangeControllerBeta.mm

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@
1717
#import "ASInternalHelpers.h"
1818
#import "ASDisplayNode+FrameworkPrivate.h"
1919

20-
extern BOOL ASInterfaceStateIncludesVisible(ASInterfaceState interfaceState)
21-
{
22-
return ((interfaceState & ASInterfaceStateVisible) == ASInterfaceStateVisible);
23-
}
24-
25-
extern BOOL ASInterfaceStateIncludesDisplay(ASInterfaceState interfaceState)
26-
{
27-
return ((interfaceState & ASInterfaceStateDisplay) == ASInterfaceStateDisplay);
28-
}
29-
30-
extern BOOL ASInterfaceStateIncludesFetchData(ASInterfaceState interfaceState)
31-
{
32-
return ((interfaceState & ASInterfaceStateFetchData) == ASInterfaceStateFetchData);
33-
}
34-
3520
@interface ASRangeControllerBeta ()
3621
{
3722
BOOL _rangeIsValid;

AsyncDisplayKit/Details/_ASDisplayLayer.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ - (void)setNeedsDisplay
110110
ASDisplayNodeAssertMainThread();
111111

112112
ASDN::MutexLocker l(_displaySuspendedLock);
113+
// FIXME: Reconsider whether we should cancel a display in progress.
114+
// We should definitely cancel a display that is scheduled, but unstarted display.
113115
[self cancelAsyncDisplay];
114116

115117
// Short circuit if display is suspended. When resumed, we will setNeedsDisplay at that time.

AsyncDisplayKit/Layout/ASLayoutSpec.mm

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ - (instancetype)init
3939
if (!(self = [super init])) {
4040
return nil;
4141
}
42-
_layoutChildren = [NSMutableDictionary dictionary];
4342
_isMutable = YES;
4443
return self;
4544
}
@@ -56,11 +55,6 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
5655
return self;
5756
}
5857

59-
- (void)setChild:(id<ASLayoutable>)child;
60-
{
61-
[self setChild:child forIdentifier:kDefaultChildKey];
62-
}
63-
6458
- (id<ASLayoutable>)layoutableToAddFromLayoutable:(id<ASLayoutable>)child
6559
{
6660
if (self.isFinalLayoutable == NO) {
@@ -88,6 +82,19 @@ - (void)setChild:(id<ASLayoutable>)child;
8882
return child;
8983
}
9084

85+
- (NSMutableDictionary *)layoutChildren
86+
{
87+
if (!_layoutChildren) {
88+
_layoutChildren = [NSMutableDictionary dictionary];
89+
}
90+
return _layoutChildren;
91+
}
92+
93+
- (void)setChild:(id<ASLayoutable>)child;
94+
{
95+
[self setChild:child forIdentifier:kDefaultChildKey];
96+
}
97+
9198
- (void)setChild:(id<ASLayoutable>)child forIdentifier:(NSString *)identifier
9299
{
93100
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");

AsyncDisplayKit/Private/ASDisplayNode+UIViewBridge.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "ASInternalHelpers.h"
1212
#import "ASAssert.h"
1313
#import "ASDisplayNodeInternal.h"
14+
#import "ASDisplayNodeExtras.h"
1415
#import "ASDisplayNode+Subclasses.h"
1516
#import "ASDisplayNode+FrameworkPrivate.h"
1617
#import "ASDisplayNode+Beta.h"
@@ -250,9 +251,11 @@ - (void)setNeedsDisplay
250251
_messageToViewOrLayer(setNeedsDisplay);
251252

252253
if ([ASDisplayNode shouldUseNewRenderingRange]) {
253-
BOOL shouldDisplay = ((_interfaceState & ASInterfaceStateDisplay) == ASInterfaceStateDisplay);
254-
if (_layer && !_flags.synchronous && shouldDisplay) {
255-
[ASDisplayNode scheduleNodeForDisplay:self];
254+
BOOL nowDisplay = ASInterfaceStateIncludesDisplay(_interfaceState);
255+
// FIXME: This should not need to recursively display, so create a non-recursive variant.
256+
// The semantics of setNeedsDisplay (as defined by CALayer behavior) are not recursive.
257+
if (_layer && !_flags.synchronous && nowDisplay && [self __implementsDisplay]) {
258+
[ASDisplayNode scheduleNodeForRecursiveDisplay:self];
256259
}
257260
}
258261
}

AsyncDisplayKit/Private/ASDisplayNodeInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ typedef NS_OPTIONS(NSUInteger, ASDisplayNodeMethodOverrides)
109109
#endif
110110
}
111111

112-
+ (void)scheduleNodeForDisplay:(ASDisplayNode *)node;
112+
+ (void)scheduleNodeForRecursiveDisplay:(ASDisplayNode *)node;
113113

114114
// The _ASDisplayLayer backing the node, if any.
115115
@property (nonatomic, readonly, retain) _ASDisplayLayer *asyncLayer;

0 commit comments

Comments
 (0)