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] Implement a std::atomic-based flag system for superb performance #89

Merged
merged 2 commits into from
Apr 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions Source/ASDisplayNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ extern NSInteger const ASDefaultDrawingPriority;
*
* @return NO if the node wraps a _ASDisplayView, YES otherwise.
*/
@property (nonatomic, readonly, assign, getter=isSynchronous) BOOL synchronous;

@property (atomic, readonly, assign, getter=isSynchronous) BOOL synchronous;

/** @name Getting view and layer */

Expand Down
37 changes: 18 additions & 19 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ BOOL ASDisplayNodeSubclassOverridesSelector(Class subclass, SEL selector)

// For classes like ASTableNode, ASCollectionNode, ASScrollNode and similar - we have to be sure to set certain properties
// like setFrame: and setBackgroundColor: directly to the UIView and not apply it to the layer only.
BOOL ASDisplayNodeNeedsSpecialPropertiesHandlingForFlags(ASDisplayNodeFlags flags)
BOOL ASDisplayNodeNeedsSpecialPropertiesHandling(BOOL isSynchronous, BOOL isLayerBacked)
{
return flags.synchronous && !flags.layerBacked;
return isSynchronous && !isLayerBacked;
}

_ASPendingState *ASDisplayNodeGetPendingState(ASDisplayNode *node)
Expand Down Expand Up @@ -314,7 +314,7 @@ - (instancetype)initWithViewClass:(Class)viewClass
ASDisplayNodeAssert([viewClass isSubclassOfClass:[UIView class]], @"should initialize with a subclass of UIView");

_viewClass = viewClass;
_flags.synchronous = ![viewClass isSubclassOfClass:[_ASDisplayView class]];
setFlag(Synchronous, ![viewClass isSubclassOfClass:[_ASDisplayView class]]);

return self;
}
Expand All @@ -328,8 +328,8 @@ - (instancetype)initWithLayerClass:(Class)layerClass
ASDisplayNodeAssert([layerClass isSubclassOfClass:[CALayer class]], @"should initialize with a subclass of CALayer");

_layerClass = layerClass;
_flags.synchronous = ![layerClass isSubclassOfClass:[_ASDisplayLayer class]];
_flags.layerBacked = YES;
setFlag(Synchronous, ![layerClass isSubclassOfClass:[_ASDisplayLayer class]]);

return self;
}
Expand Down Expand Up @@ -378,7 +378,7 @@ - (void)setViewBlock:(ASDisplayNodeViewBlock)viewBlock
ASDisplayNodeAssertNotNil(viewBlock, @"should initialize with a valid block that returns a UIView");

_viewBlock = viewBlock;
_flags.synchronous = YES;
setFlag(Synchronous, YES);
}

- (void)setLayerBlock:(ASDisplayNodeLayerBlock)layerBlock
Expand All @@ -387,8 +387,8 @@ - (void)setLayerBlock:(ASDisplayNodeLayerBlock)layerBlock
ASDisplayNodeAssertNotNil(layerBlock, @"should initialize with a valid block that returns a CALayer");

_layerBlock = layerBlock;
_flags.synchronous = YES;
_flags.layerBacked = YES;
setFlag(Synchronous, YES);
}

- (void)onDidLoad:(ASDisplayNodeDidLoadBlock)body
Expand All @@ -411,7 +411,7 @@ - (void)dealloc
_flags.isDeallocating = YES;

// Synchronous nodes may not be able to call the hierarchy notifications, so only enforce for regular nodes.
ASDisplayNodeAssert(_flags.synchronous || !ASInterfaceStateIncludesVisible(_interfaceState), @"Node should always be marked invisible before deallocating. Node: %@", self);
ASDisplayNodeAssert(checkFlag(Synchronous) || !ASInterfaceStateIncludesVisible(_interfaceState), @"Node should always be marked invisible before deallocating. Node: %@", self);

self.asyncLayer.asyncDelegate = nil;
_view.asyncdisplaykit_node = nil;
Expand Down Expand Up @@ -547,7 +547,7 @@ - (void)__unloadNode
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssert([self isNodeLoaded], @"Implementation shouldn't call __unloadNode if not loaded: %@", self);
ASDisplayNodeAssert(_flags.synchronous == NO, @"Node created using -initWithViewBlock:/-initWithLayerBlock: cannot be unloaded. Node: %@", self);
ASDisplayNodeAssert(checkFlag(Synchronous) == NO, @"Node created using -initWithViewBlock:/-initWithLayerBlock: cannot be unloaded. Node: %@", self);
ASDN::MutexLocker l(__instanceLock__);

if (_flags.layerBacked) {
Expand Down Expand Up @@ -583,7 +583,7 @@ - (UIView *)_locked_viewToLoad
}

// Special handling of wrapping UIKit components
if (_flags.synchronous) {
if (checkFlag(Synchronous)) {
// UIImageView layers. More details on the flags
if ([_viewClass isSubclassOfClass:[UIImageView class]]) {
_flags.canClearContentsOfLayer = NO;
Expand Down Expand Up @@ -805,8 +805,7 @@ - (_ASDisplayLayer *)_locked_asyncLayer

- (BOOL)isSynchronous
{
ASDN::MutexLocker l(__instanceLock__);
return _flags.synchronous;
return checkFlag(Synchronous);
}

- (void)setLayerBacked:(BOOL)isLayerBacked
Expand Down Expand Up @@ -836,7 +835,7 @@ - (BOOL)isLayerBacked
- (BOOL)supportsLayerBacking
{
ASDN::MutexLocker l(__instanceLock__);
return !_flags.synchronous && !_flags.viewEverHadAGestureRecognizerAttached && _viewClass == [_ASDisplayView class] && _layerClass == [_ASDisplayLayer class];
return !checkFlag(Synchronous) && !_flags.viewEverHadAGestureRecognizerAttached && _viewClass == [_ASDisplayView class] && _layerClass == [_ASDisplayLayer class];
}

- (BOOL)shouldAnimateSizeChanges
Expand Down Expand Up @@ -1948,7 +1947,7 @@ - (BOOL)displaysAsynchronously
*/
- (BOOL)_locked_displaysAsynchronously
{
return _flags.synchronous == NO && _flags.displaysAsynchronously;
return checkFlag(Synchronous) == NO && _flags.displaysAsynchronously;
}

- (void)setDisplaysAsynchronously:(BOOL)displaysAsynchronously
Expand All @@ -1958,7 +1957,7 @@ - (void)setDisplaysAsynchronously:(BOOL)displaysAsynchronously
ASDN::MutexLocker l(__instanceLock__);

// Can't do this for synchronous nodes (using layers that are not _ASDisplayLayer and so we can't control display prevention/cancel)
if (_flags.synchronous) {
if (checkFlag(Synchronous)) {
return;
}

Expand Down Expand Up @@ -2052,7 +2051,7 @@ - (void)setContentsScaleForDisplay:(CGFloat)contentsScaleForDisplay
- (void)displayImmediately
{
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssert(!_flags.synchronous, @"this method is designed for asynchronous mode only");
ASDisplayNodeAssert(!checkFlag(Synchronous), @"this method is designed for asynchronous mode only");

[self.asyncLayer displayImmediately];
}
Expand All @@ -2073,7 +2072,7 @@ - (void)__setNeedsDisplay
BOOL nowDisplay = ASInterfaceStateIncludesDisplay(_interfaceState);
// FIXME: This should not need to recursively display, so create a non-recursive variant.
// The semantics of setNeedsDisplay (as defined by CALayer behavior) are not recursive.
if (_layer != nil && !_flags.synchronous && nowDisplay && [self _implementsDisplay]) {
if (_layer != nil && !checkFlag(Synchronous) && nowDisplay && [self _implementsDisplay]) {
shouldScheduleForDisplay = YES;
}
}
Expand Down Expand Up @@ -2317,7 +2316,7 @@ - (void)setDisplaySuspended:(BOOL)flag
__instanceLock__.lock();

// Can't do this for synchronous nodes (using layers that are not _ASDisplayLayer and so we can't control display prevention/cancel)
if (_flags.synchronous || _flags.displaySuspended == flag) {
if (checkFlag(Synchronous) || _flags.displaySuspended == flag) {
__instanceLock__.unlock();
return;
}
Expand Down Expand Up @@ -3406,7 +3405,7 @@ - (void)setHierarchyState:(ASHierarchyState)newState

// Entered rasterization state.
if (newState & ASHierarchyStateRasterized) {
ASDisplayNodeAssert(_flags.synchronous == NO, @"Node created using -initWithViewBlock:/-initWithLayerBlock: cannot be added to subtree of node with shouldRasterizeDescendants=YES. Node: %@", self);
ASDisplayNodeAssert(checkFlag(Synchronous) == NO, @"Node created using -initWithViewBlock:/-initWithLayerBlock: cannot be added to subtree of node with shouldRasterizeDescendants=YES. Node: %@", self);
}

// Entered or exited range managed state.
Expand Down Expand Up @@ -3907,7 +3906,7 @@ - (void)_locked_applyPendingViewState
if (_flags.layerBacked) {
[_pendingViewState applyToLayer:self.layer];
} else {
BOOL specialPropertiesHandling = ASDisplayNodeNeedsSpecialPropertiesHandlingForFlags(_flags);
BOOL specialPropertiesHandling = ASDisplayNodeNeedsSpecialPropertiesHandling(checkFlag(Synchronous), _flags.layerBacked);
[_pendingViewState applyToView:self.view withSpecialPropertiesHandling:specialPropertiesHandling];
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Private/ASDisplayNode+UIViewBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ - (void)setFrame:(CGRect)rect

// For classes like ASTableNode, ASCollectionNode, ASScrollNode and similar - make sure UIView gets setFrame:
struct ASDisplayNodeFlags flags = _flags;
BOOL specialPropertiesHandling = ASDisplayNodeNeedsSpecialPropertiesHandlingForFlags(flags);
BOOL specialPropertiesHandling = ASDisplayNodeNeedsSpecialPropertiesHandling(checkFlag(Synchronous), flags.layerBacked);

BOOL nodeLoaded = __loaded(self);
BOOL isMainThread = ASDisplayNodeThreadIsMain();
Expand Down Expand Up @@ -635,7 +635,7 @@ - (void)setBackgroundColor:(UIColor *)newBackgroundColor
if (shouldApply) {
CGColorRef oldBackgroundCGColor = _layer.backgroundColor;

BOOL specialPropertiesHandling = ASDisplayNodeNeedsSpecialPropertiesHandlingForFlags(_flags);
BOOL specialPropertiesHandling = ASDisplayNodeNeedsSpecialPropertiesHandling(checkFlag(Synchronous), _flags.layerBacked);
if (specialPropertiesHandling) {
_view.backgroundColor = newBackgroundColor;
} else {
Expand Down
15 changes: 13 additions & 2 deletions Source/Private/ASDisplayNodeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
struct ASDisplayNodeFlags;

BOOL ASDisplayNodeSubclassOverridesSelector(Class subclass, SEL selector);
BOOL ASDisplayNodeNeedsSpecialPropertiesHandlingForFlags(ASDisplayNodeFlags flags);
BOOL ASDisplayNodeNeedsSpecialPropertiesHandling(BOOL isSynchronous, BOOL isLayerBacked);

/// Get the pending view state for the node, creating one if needed.
_ASPendingState * ASDisplayNodeGetPendingState(ASDisplayNode * node);
Expand All @@ -55,6 +55,16 @@ typedef NS_OPTIONS(NSUInteger, ASDisplayNodeMethodOverrides)
ASDisplayNodeMethodOverrideClearFetchedData = 1 << 6
};

typedef NS_OPTIONS(uint_least32_t, ASDisplayNodeAtomicFlags)
{
Synchronous = 1 << 0,
};

#define checkFlag(flag) ((_atomicFlags.load() & flag) != 0)
// Returns the old value of the flag as a BOOL.
#define setFlag(flag, x) (((x ? _atomicFlags.fetch_or(flag) \
: _atomicFlags.fetch_and(~flag)) & flag) != 0)

FOUNDATION_EXPORT NSString * const ASRenderingEngineDidDisplayScheduledNodesNotification;
FOUNDATION_EXPORT NSString * const ASRenderingEngineDidDisplayNodesScheduledBeforeTimestamp;

Expand All @@ -71,9 +81,10 @@ FOUNDATION_EXPORT NSString * const ASRenderingEngineDidDisplayNodesScheduledBefo
UIView *_view;
CALayer *_layer;

std::atomic<ASDisplayNodeAtomicFlags> _atomicFlags;

struct ASDisplayNodeFlags {
// public properties
unsigned synchronous:1;
unsigned viewEverHadAGestureRecognizerAttached:1;
unsigned layerBacked:1;
unsigned displaysAsynchronously:1;
Expand Down