Skip to content

Commit d803071

Browse files
Adlai-Hollermikezucc
authored andcommitted
Remove reliance on shared_ptr for ASDisplayNodeLayouts (TextureGroup#1131)
* Remove reliance on shared_ptr for ASDisplayNodeLayouts * Fix up * Fix in yoga * Back to let * Returns inner pointer * Trivial change to kick the CI
1 parent 443f437 commit d803071

File tree

10 files changed

+99
-124
lines changed

10 files changed

+99
-124
lines changed

AsyncDisplayKit.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@
148148
6947B0C01E36B4E30007C478 /* ASStackUnpositionedLayout.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6947B0BD1E36B4E30007C478 /* ASStackUnpositionedLayout.mm */; };
149149
6947B0C31E36B5040007C478 /* ASStackPositionedLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 6947B0C11E36B5040007C478 /* ASStackPositionedLayout.h */; settings = {ATTRIBUTES = (Private, ); }; };
150150
6947B0C51E36B5040007C478 /* ASStackPositionedLayout.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6947B0C21E36B5040007C478 /* ASStackPositionedLayout.mm */; };
151-
6959433F1D70815300B0EE1F /* ASDisplayNodeLayout.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6959433C1D70815300B0EE1F /* ASDisplayNodeLayout.mm */; };
152151
695943401D70815300B0EE1F /* ASDisplayNodeLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 6959433D1D70815300B0EE1F /* ASDisplayNodeLayout.h */; settings = {ATTRIBUTES = (Private, ); }; };
153152
695BE2551DC1245C008E6EA5 /* ASWrapperSpecSnapshotTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 695BE2541DC1245C008E6EA5 /* ASWrapperSpecSnapshotTests.mm */; };
154153
696F01EC1DD2AF450049FBD5 /* ASEventLog.h in Headers */ = {isa = PBXBuildFile; fileRef = 696F01EA1DD2AF450049FBD5 /* ASEventLog.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -705,7 +704,6 @@
705704
6947B0BD1E36B4E30007C478 /* ASStackUnpositionedLayout.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASStackUnpositionedLayout.mm; sourceTree = "<group>"; };
706705
6947B0C11E36B5040007C478 /* ASStackPositionedLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASStackPositionedLayout.h; sourceTree = "<group>"; };
707706
6947B0C21E36B5040007C478 /* ASStackPositionedLayout.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASStackPositionedLayout.mm; sourceTree = "<group>"; };
708-
6959433C1D70815300B0EE1F /* ASDisplayNodeLayout.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASDisplayNodeLayout.mm; sourceTree = "<group>"; };
709707
6959433D1D70815300B0EE1F /* ASDisplayNodeLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASDisplayNodeLayout.h; sourceTree = "<group>"; };
710708
695BE2541DC1245C008E6EA5 /* ASWrapperSpecSnapshotTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASWrapperSpecSnapshotTests.mm; sourceTree = "<group>"; };
711709
696F01EA1DD2AF450049FBD5 /* ASEventLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASEventLog.h; sourceTree = "<group>"; };
@@ -1490,7 +1488,6 @@
14901488
690BC8C020F6D3490052A434 /* ASDisplayNodeCornerLayerDelegate.m */,
14911489
058D0A0C195D050800B7D73C /* ASDisplayNodeInternal.h */,
14921490
6959433D1D70815300B0EE1F /* ASDisplayNodeLayout.h */,
1493-
6959433C1D70815300B0EE1F /* ASDisplayNodeLayout.mm */,
14941491
CCA282C61E9EB64B0037E8B7 /* ASDisplayNodeTipState.h */,
14951492
CCA282C71E9EB64B0037E8B7 /* ASDisplayNodeTipState.m */,
14961493
68B8A4DB1CBD911D007E4543 /* ASImageNode+AnimatedImagePrivate.h */,
@@ -2497,7 +2494,6 @@
24972494
DB78412E1C6BCE1600A9E2B4 /* _ASTransitionContext.m in Sources */,
24982495
B350620B1B010EFD0018CF92 /* ASTableView.mm in Sources */,
24992496
B350620E1B010EFD0018CF92 /* ASTextNode.mm in Sources */,
2500-
6959433F1D70815300B0EE1F /* ASDisplayNodeLayout.mm in Sources */,
25012497
68355B3E1CB57A60001D4E68 /* ASPINRemoteImageDownloader.m in Sources */,
25022498
CC034A141E649F1300626263 /* AsyncDisplayKit+IGListKitMethods.m in Sources */,
25032499
509E68661B3AEDD7009B9150 /* CoreGraphics+ASConvenience.m in Sources */,

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- Small optimization to the layout spec & yoga layout systems by eliminating array copies. [Adlai Holler](https://github.com/Adlai-Holler)
5050
- Optimize layout process by removing `ASRectMap`. [Adlai Holler](https://github.com/Adlai-Holler)
5151
- Remove necessity to use view to access rangeController in ASTableNode, ASCollectionNode. [Michael Schneider](https://github.com/maicki)
52+
- Remove display node's reliance on shared_ptr. [Adlai Holler](https://github.com/Adlai-Holler)
5253

5354
## 2.7
5455
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)

Source/ASDisplayNode+Layout.mm

Lines changed: 62 additions & 64 deletions
Large diffs are not rendered by default.

Source/ASDisplayNode+Yoga.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ - (void)setupYogaCalculatedLayout
209209
// For the root node in a Yoga tree, make sure to preserve the constrainedSize originally provided.
210210
// This will be used for all relayouts triggered by children, since they escalate to root.
211211
ASSizeRange range = parentNode ? ASSizeRangeUnconstrained : self.constrainedSizeForCalculatedLayout;
212-
_pendingDisplayNodeLayout = std::make_shared<ASDisplayNodeLayout>(layout, range, parentSize, _layoutVersion);
212+
_pendingDisplayNodeLayout = ASDisplayNodeLayout(layout, range, parentSize, _layoutVersion);
213213
}
214214
}
215215

Source/ASDisplayNode.mm

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ - (void)_initializeInstance
292292

293293
_primitiveTraitCollection = ASPrimitiveTraitCollectionMakeDefault();
294294

295-
_calculatedDisplayNodeLayout = std::make_shared<ASDisplayNodeLayout>();
296-
_pendingDisplayNodeLayout = nullptr;
297295
_layoutVersion = 1;
298296

299297
_defaultLayoutTransitionDuration = 0.2;
@@ -3812,21 +3810,19 @@ - (NSString *)detailedLayoutDescription
38123810
[props addObject:@{ @"layoutVersion": @(_layoutVersion.load()) }];
38133811
[props addObject:@{ @"bounds": [NSValue valueWithCGRect:self.bounds] }];
38143812

3815-
if (_calculatedDisplayNodeLayout != nullptr) {
3816-
ASDisplayNodeLayout c = *_calculatedDisplayNodeLayout;
3817-
[props addObject:@{ @"calculatedLayout": c.layout }];
3818-
[props addObject:@{ @"calculatedVersion": @(c.version) }];
3819-
[props addObject:@{ @"calculatedConstrainedSize" : NSStringFromASSizeRange(c.constrainedSize) }];
3820-
if (c.requestedLayoutFromAbove) {
3813+
if (_calculatedDisplayNodeLayout.layout) {
3814+
[props addObject:@{ @"calculatedLayout": _calculatedDisplayNodeLayout.layout }];
3815+
[props addObject:@{ @"calculatedVersion": @(_calculatedDisplayNodeLayout.version) }];
3816+
[props addObject:@{ @"calculatedConstrainedSize" : NSStringFromASSizeRange(_calculatedDisplayNodeLayout.constrainedSize) }];
3817+
if (_calculatedDisplayNodeLayout.requestedLayoutFromAbove) {
38213818
[props addObject:@{ @"calculatedRequestedLayoutFromAbove": @"YES" }];
38223819
}
38233820
}
3824-
if (_pendingDisplayNodeLayout != nullptr) {
3825-
ASDisplayNodeLayout p = *_pendingDisplayNodeLayout;
3826-
[props addObject:@{ @"pendingLayout": p.layout }];
3827-
[props addObject:@{ @"pendingVersion": @(p.version) }];
3828-
[props addObject:@{ @"pendingConstrainedSize" : NSStringFromASSizeRange(p.constrainedSize) }];
3829-
if (p.requestedLayoutFromAbove) {
3821+
if (_pendingDisplayNodeLayout.layout) {
3822+
[props addObject:@{ @"pendingLayout": _pendingDisplayNodeLayout.layout }];
3823+
[props addObject:@{ @"pendingVersion": @(_pendingDisplayNodeLayout.version) }];
3824+
[props addObject:@{ @"pendingConstrainedSize" : NSStringFromASSizeRange(_pendingDisplayNodeLayout.constrainedSize) }];
3825+
if (_pendingDisplayNodeLayout.requestedLayoutFromAbove) {
38303826
[props addObject:@{ @"pendingRequestedLayoutFromAbove": (id)kCFNull }];
38313827
}
38323828
}

Source/Private/ASDisplayNodeInternal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ AS_EXTERN NSString * const ASRenderingEngineDidDisplayNodesScheduledBeforeTimest
164164

165165
std::atomic<int32_t> _pendingTransitionID;
166166
ASLayoutTransition *_pendingLayoutTransition;
167-
std::shared_ptr<ASDisplayNodeLayout> _calculatedDisplayNodeLayout;
168-
std::shared_ptr<ASDisplayNodeLayout> _pendingDisplayNodeLayout;
167+
ASDisplayNodeLayout _calculatedDisplayNodeLayout;
168+
ASDisplayNodeLayout _pendingDisplayNodeLayout;
169169

170170
/// Sentinel for layout data. Incremented when we get -setNeedsLayout / -invalidateCalculatedLayout.
171171
/// Starts at 1.

Source/Private/ASDisplayNodeLayout.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ struct ASDisplayNodeLayout {
4343
/**
4444
* Returns whether this is valid for a given version
4545
*/
46-
BOOL isValid(NSUInteger version);
46+
BOOL isValid(NSUInteger versionArg) {
47+
return layout != nil && version >= versionArg;
48+
}
4749

4850
/**
4951
* Returns whether this is valid for a given constrained size, parent size, and version
5052
*/
51-
BOOL isValid(ASSizeRange constrainedSize, CGSize parentSize, NSUInteger version);
53+
BOOL isValid(ASSizeRange theConstrainedSize, CGSize theParentSize, NSUInteger versionArg) {
54+
return isValid(versionArg)
55+
&& CGSizeEqualToSize(parentSize, theParentSize)
56+
&& ASSizeRangeEqualToSizeRange(constrainedSize, theConstrainedSize);
57+
}
5258
};

Source/Private/ASDisplayNodeLayout.mm

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

Source/Private/ASLayoutTransition.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#import <AsyncDisplayKit/ASDisplayNode.h>
1616
#import <AsyncDisplayKit/ASLayoutSpec.h>
1717

18-
#import <memory>
19-
2018
NS_ASSUME_NONNULL_BEGIN
2119

2220
#pragma mark - ASLayoutElementTransition
@@ -52,12 +50,12 @@ AS_SUBCLASSING_RESTRICTED
5250
/**
5351
* Previous layout to transition from
5452
*/
55-
@property (nonatomic, readonly) std::shared_ptr<ASDisplayNodeLayout> previousLayout;
53+
@property (nonatomic, readonly) const ASDisplayNodeLayout &previousLayout NS_RETURNS_INNER_POINTER;
5654

5755
/**
5856
* Pending layout to transition to
5957
*/
60-
@property (nonatomic, readonly) std::shared_ptr<ASDisplayNodeLayout> pendingLayout;
58+
@property (nonatomic, readonly) const ASDisplayNodeLayout &pendingLayout NS_RETURNS_INNER_POINTER;
6159

6260
/**
6361
* Returns if the layout transition needs to happen synchronously
@@ -68,8 +66,8 @@ AS_SUBCLASSING_RESTRICTED
6866
* Returns a newly initialized layout transition
6967
*/
7068
- (instancetype)initWithNode:(ASDisplayNode *)node
71-
pendingLayout:(std::shared_ptr<ASDisplayNodeLayout>)pendingLayout
72-
previousLayout:(std::shared_ptr<ASDisplayNodeLayout>)previousLayout NS_DESIGNATED_INITIALIZER;
69+
pendingLayout:(const ASDisplayNodeLayout &)pendingLayout
70+
previousLayout:(const ASDisplayNodeLayout &)previousLayout NS_DESIGNATED_INITIALIZER;
7371

7472
/**
7573
* Insert and remove subnodes that were added or removed between the previousLayout and the pendingLayout

Source/Private/ASLayoutTransition.mm

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ @implementation ASLayoutTransition {
6060
NSArray<ASDisplayNode *> *_removedSubnodes;
6161
std::vector<NSUInteger> _insertedSubnodePositions;
6262
std::vector<std::pair<ASDisplayNode *, NSUInteger>> _subnodeMoves;
63+
ASDisplayNodeLayout _pendingLayout;
64+
ASDisplayNodeLayout _previousLayout;
6365
}
6466

6567
- (instancetype)initWithNode:(ASDisplayNode *)node
66-
pendingLayout:(std::shared_ptr<ASDisplayNodeLayout>)pendingLayout
67-
previousLayout:(std::shared_ptr<ASDisplayNodeLayout>)previousLayout
68+
pendingLayout:(const ASDisplayNodeLayout &)pendingLayout
69+
previousLayout:(const ASDisplayNodeLayout &)previousLayout
6870
{
6971
self = [super init];
7072
if (self) {
@@ -80,7 +82,7 @@ - (instancetype)initWithNode:(ASDisplayNode *)node
8082
- (BOOL)isSynchronous
8183
{
8284
ASDN::MutexSharedLocker l(__instanceLock__);
83-
return !ASLayoutCanTransitionAsynchronous(_pendingLayout->layout);
85+
return !ASLayoutCanTransitionAsynchronous(_pendingLayout.layout);
8486
}
8587

8688
- (void)commitTransition
@@ -156,8 +158,8 @@ - (void)calculateSubnodeOperationsIfNeeded
156158

157159
// Create an activity even if no subnodes affected.
158160
as_activity_create_for_scope("Calculate subnode operations");
159-
ASLayout *previousLayout = _previousLayout->layout;
160-
ASLayout *pendingLayout = _pendingLayout->layout;
161+
ASLayout *previousLayout = _previousLayout.layout;
162+
ASLayout *pendingLayout = _pendingLayout.layout;
161163

162164
if (previousLayout) {
163165
#if AS_IG_LIST_KIT
@@ -226,9 +228,9 @@ - (ASLayout *)transitionContext:(_ASTransitionContext *)context layoutForKey:(NS
226228
{
227229
ASDN::MutexSharedLocker l(__instanceLock__);
228230
if ([key isEqualToString:ASTransitionContextFromLayoutKey]) {
229-
return _previousLayout->layout;
231+
return _previousLayout.layout;
230232
} else if ([key isEqualToString:ASTransitionContextToLayoutKey]) {
231-
return _pendingLayout->layout;
233+
return _pendingLayout.layout;
232234
} else {
233235
return nil;
234236
}
@@ -238,9 +240,9 @@ - (ASSizeRange)transitionContext:(_ASTransitionContext *)context constrainedSize
238240
{
239241
ASDN::MutexSharedLocker l(__instanceLock__);
240242
if ([key isEqualToString:ASTransitionContextFromLayoutKey]) {
241-
return _previousLayout->constrainedSize;
243+
return _previousLayout.constrainedSize;
242244
} else if ([key isEqualToString:ASTransitionContextToLayoutKey]) {
243-
return _pendingLayout->constrainedSize;
245+
return _pendingLayout.constrainedSize;
244246
} else {
245247
return ASSizeRangeMake(CGSizeZero, CGSizeZero);
246248
}

0 commit comments

Comments
 (0)