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

Commit 735b4eb

Browse files
committed
Revert "[ASLayoutSpec] Use childrenMap directly to prevent creating an NSArray within ASDK (#1937)"
Something interesting going on here with ARC / Objective-C++ that we are investigating and will re-land. This reverts commit c90ed08.
1 parent c797fdf commit 735b4eb

8 files changed

Lines changed: 43 additions & 133 deletions

File tree

AsyncDisplayKit.xcodeproj/project.pbxproj

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@
214214
69E1006F1CA89CB600D88C1B /* ASEnvironmentInternal.mm in Sources */ = {isa = PBXBuildFile; fileRef = 69E1006A1CA89CB600D88C1B /* ASEnvironmentInternal.mm */; };
215215
69E100701CA89CB600D88C1B /* ASEnvironmentInternal.mm in Sources */ = {isa = PBXBuildFile; fileRef = 69E1006A1CA89CB600D88C1B /* ASEnvironmentInternal.mm */; };
216216
69F10C871C84C35D0026140C /* ASRangeControllerUpdateRangeProtocol+Beta.h in Headers */ = {isa = PBXBuildFile; fileRef = 69F10C851C84C35D0026140C /* ASRangeControllerUpdateRangeProtocol+Beta.h */; settings = {ATTRIBUTES = (Public, ); }; };
217-
69F6058D1D3DA27E00C8CA38 /* ASLayoutSpec+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 69F6058C1D3DA27E00C8CA38 /* ASLayoutSpec+Private.h */; };
218217
7630FFA81C9E267E007A7C0E /* ASVideoNode.h in Headers */ = {isa = PBXBuildFile; fileRef = AEEC47DF1C20C2DD00EC1693 /* ASVideoNode.h */; settings = {ATTRIBUTES = (Public, ); }; };
219218
764D83D51C8EA515009B4FB8 /* AsyncDisplayKit+Debug.h in Headers */ = {isa = PBXBuildFile; fileRef = 764D83D21C8EA515009B4FB8 /* AsyncDisplayKit+Debug.h */; settings = {ATTRIBUTES = (Public, ); }; };
220219
764D83D61C8EA515009B4FB8 /* AsyncDisplayKit+Debug.m in Sources */ = {isa = PBXBuildFile; fileRef = 764D83D31C8EA515009B4FB8 /* AsyncDisplayKit+Debug.m */; };
@@ -966,7 +965,6 @@
966965
69E100691CA89CB600D88C1B /* ASEnvironmentInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASEnvironmentInternal.h; sourceTree = "<group>"; };
967966
69E1006A1CA89CB600D88C1B /* ASEnvironmentInternal.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASEnvironmentInternal.mm; sourceTree = "<group>"; };
968967
69F10C851C84C35D0026140C /* ASRangeControllerUpdateRangeProtocol+Beta.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASRangeControllerUpdateRangeProtocol+Beta.h"; sourceTree = "<group>"; };
969-
69F6058C1D3DA27E00C8CA38 /* ASLayoutSpec+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASLayoutSpec+Private.h"; sourceTree = "<group>"; };
970968
6BDC61F51978FEA400E50D21 /* AsyncDisplayKit.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; path = AsyncDisplayKit.h; sourceTree = "<group>"; };
971969
764D83D21C8EA515009B4FB8 /* AsyncDisplayKit+Debug.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AsyncDisplayKit+Debug.h"; sourceTree = "<group>"; };
972970
764D83D31C8EA515009B4FB8 /* AsyncDisplayKit+Debug.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AsyncDisplayKit+Debug.m"; sourceTree = "<group>"; };
@@ -1472,8 +1470,6 @@
14721470
044285051BAA63FE00D16268 /* ASBatchFetching.h */,
14731471
044285061BAA63FE00D16268 /* ASBatchFetching.m */,
14741472
251B8EF61BBB3D690087C538 /* ASDataController+Subclasses.h */,
1475-
8B0768B11CE752EC002E1453 /* ASDefaultPlaybackButton.h */,
1476-
8B0768B21CE752EC002E1453 /* ASDefaultPlaybackButton.m */,
14771473
AEB7B0181C5962EA00662EF4 /* ASDefaultPlayButton.h */,
14781474
AEB7B0191C5962EA00662EF4 /* ASDefaultPlayButton.m */,
14791475
058D0A08195D050800B7D73C /* ASDisplayNode+AsyncDisplay.mm */,
@@ -1482,17 +1478,16 @@
14821478
DE6EA3211C14000600183B10 /* ASDisplayNode+FrameworkPrivate.h */,
14831479
058D0A0B195D050800B7D73C /* ASDisplayNode+UIViewBridge.mm */,
14841480
058D0A0C195D050800B7D73C /* ASDisplayNodeInternal.h */,
1481+
E52405B41C8FEF16004DC8E7 /* ASLayoutTransition.h */,
1482+
E52405B21C8FEF03004DC8E7 /* ASLayoutTransition.mm */,
14851483
69E100691CA89CB600D88C1B /* ASEnvironmentInternal.h */,
14861484
69E1006A1CA89CB600D88C1B /* ASEnvironmentInternal.mm */,
1487-
68B8A4DB1CBD911D007E4543 /* ASImageNode+AnimatedImagePrivate.h */,
14881485
058D0A0D195D050800B7D73C /* ASImageNode+CGExtras.h */,
14891486
058D0A0E195D050800B7D73C /* ASImageNode+CGExtras.m */,
1487+
68B8A4DB1CBD911D007E4543 /* ASImageNode+AnimatedImagePrivate.h */,
14901488
ACF6ED431B17847A00DA7C62 /* ASInternalHelpers.h */,
14911489
ACF6ED441B17847A00DA7C62 /* ASInternalHelpers.m */,
1492-
69F6058C1D3DA27E00C8CA38 /* ASLayoutSpec+Private.h */,
14931490
ACF6ED451B17847A00DA7C62 /* ASLayoutSpecUtilities.h */,
1494-
E52405B41C8FEF16004DC8E7 /* ASLayoutTransition.h */,
1495-
E52405B21C8FEF03004DC8E7 /* ASLayoutTransition.mm */,
14961491
0442850B1BAA64EC00D16268 /* ASMultidimensionalArrayUtils.h */,
14971492
0442850C1BAA64EC00D16268 /* ASMultidimensionalArrayUtils.mm */,
14981493
CC3B20811C3F76D600798563 /* ASPendingStateController.h */,
@@ -1510,6 +1505,8 @@
15101505
CC3B20881C3F7A5400798563 /* ASWeakSet.m */,
15111506
DBC452D91C5BF64600B16017 /* NSArray+Diffing.h */,
15121507
DBC452DA1C5BF64600B16017 /* NSArray+Diffing.m */,
1508+
8B0768B11CE752EC002E1453 /* ASDefaultPlaybackButton.h */,
1509+
8B0768B21CE752EC002E1453 /* ASDefaultPlaybackButton.m */,
15131510
);
15141511
path = Private;
15151512
sourceTree = "<group>";
@@ -1743,7 +1740,6 @@
17431740
B350625B1B010F070018CF92 /* ASEqualityHelpers.h in Headers */,
17441741
680346941CE4052A0009FEB4 /* ASNavigationController.h in Headers */,
17451742
B350621B1B010EFD0018CF92 /* ASFlowLayoutController.h in Headers */,
1746-
69F6058D1D3DA27E00C8CA38 /* ASLayoutSpec+Private.h in Headers */,
17471743
B350621D1B010EFD0018CF92 /* ASHighlightOverlayLayer.h in Headers */,
17481744
C78F7E2B1BF7809800CDEAFC /* ASTableNode.h in Headers */,
17491745
AC7A2C181BDE11DF0093FE1A /* ASTableViewInternal.h in Headers */,

AsyncDisplayKit/ASDisplayNode.mm

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,15 +2958,6 @@ - (void)setEnvironmentTraitCollection:(ASEnvironmentTraitCollection)environmentT
29582958
}
29592959
}
29602960

2961-
#pragma mark - NSFastEnumeration
2962-
2963-
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
2964-
objects:(id __unsafe_unretained [])stackbuf
2965-
count:(NSUInteger)stackbufLength
2966-
{
2967-
return [self.children countByEnumeratingWithState:state objects:stackbuf count:stackbufLength];
2968-
}
2969-
29702961
ASEnvironmentLayoutOptionsForwarding
29712962
ASEnvironmentLayoutExtensibilityForwarding
29722963

AsyncDisplayKit/ASViewController.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ - (void)progagateNewEnvironmentTraitCollection:(ASEnvironmentTraitCollection)env
289289
self.node.environmentState = environmentState;
290290
[self.node setNeedsLayout];
291291

292-
for (id<ASEnvironment> child in self.node) {
292+
NSArray<id<ASEnvironment>> *children = [self.node children];
293+
for (id<ASEnvironment> child in children) {
293294
ASEnvironmentStatePropagateDown(child, environmentState.environmentTraitCollection);
294295
}
295296
}

AsyncDisplayKit/Details/ASEnvironment.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ ASDISPLAYNODE_EXTERN_C_END
9999
* defined in an ASEnvironmentState up and down the ASEnvironment tree. To be able to define how merges of
100100
* States should happen, specific merge functions can be provided
101101
*/
102-
@protocol ASEnvironment <NSObject, NSFastEnumeration>
102+
@protocol ASEnvironment <NSObject>
103103

104104
/// The environment collection of an object which class conforms to the ASEnvironment protocol
105105
- (ASEnvironmentState)environmentState;
@@ -126,7 +126,6 @@ ASDISPLAYNODE_EXTERN_C_END
126126

127127
/// sets a trait collection on this environment state.
128128
- (void)setEnvironmentTraitCollection:(ASEnvironmentTraitCollection)environmentTraitCollection;
129-
130129
@end
131130

132131
// ASCollection/TableNodes don't actually have ASCellNodes as subnodes. Because of this we can't rely on display trait

AsyncDisplayKit/Layout/ASLayoutSpec.mm

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// of patent rights can be found in the PATENTS file in the same directory.
99
//
1010

11-
#import "ASLayoutSpec+Private.h"
11+
#import "ASLayoutSpec.h"
1212

1313
#import "ASAssert.h"
1414
#import "ASEnvironmentInternal.h"
@@ -17,12 +17,16 @@
1717
#import "ASThread.h"
1818
#import "ASTraitCollection.h"
1919

20+
#import <objc/runtime.h>
21+
#import <map>
2022
#import <vector>
2123

24+
typedef std::map<unsigned long, id<ASLayoutable>, std::less<unsigned long>> ASChildMap;
25+
2226
@interface ASLayoutSpec() {
2327
ASEnvironmentState _environmentState;
2428
ASDN::RecursiveMutex __instanceLock__;
25-
ASChildrenMap _childrenMap;
29+
ASChildMap _children;
2630
}
2731
@end
2832

@@ -31,7 +35,6 @@ @implementation ASLayoutSpec
3135
// these dynamic properties all defined in ASLayoutOptionsPrivate.m
3236
@dynamic spacingAfter, spacingBefore, flexGrow, flexShrink, flexBasis,
3337
alignSelf, ascender, descender, sizeRange, layoutPosition, layoutableType;
34-
@synthesize parent = _parent;
3538
@synthesize isFinalLayoutable = _isFinalLayoutable;
3639

3740
- (instancetype)init
@@ -102,35 +105,27 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
102105
return child;
103106
}
104107

105-
#pragma mark - Parent
106-
107108
- (void)setParent:(id<ASLayoutable>)parent
108109
{
110+
// FIXME: Locking should be evaluated here. _parent is not widely used yet, though.
109111
_parent = parent;
110112

111113
if ([parent supportsUpwardPropagation]) {
112114
ASEnvironmentStatePropagateUp(parent, self.environmentState.layoutOptionsState);
113115
}
114116
}
115117

116-
- (id<ASLayoutable>)parent
117-
{
118-
return _parent;
119-
}
120-
121-
#pragma mark - Children
122-
123118
- (void)setChild:(id<ASLayoutable>)child
124119
{
125120
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
126121
if (child) {
127122
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
128123
if (finalLayoutable) {
129-
_childrenMap[0] = finalLayoutable;
124+
_children[0] = finalLayoutable;
130125
[self propagateUpLayoutable:finalLayoutable];
131126
}
132127
} else {
133-
_childrenMap.erase(0);
128+
_children.erase(0);
134129
}
135130
}
136131

@@ -139,9 +134,9 @@ - (void)setChild:(id<ASLayoutable>)child forIndex:(NSUInteger)index
139134
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
140135
if (child) {
141136
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
142-
_childrenMap[index] = finalLayoutable;
137+
_children[index] = finalLayoutable;
143138
} else {
144-
_childrenMap.erase(index);
139+
_children.erase(index);
145140
}
146141
// TODO: Should we propagate up the layoutable at it could happen that multiple children will propagated up their
147142
// layout options and one child will overwrite values from another child
@@ -152,67 +147,35 @@ - (void)setChildren:(NSArray<id<ASLayoutable>> *)children
152147
{
153148
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
154149

155-
_childrenMap.clear();
150+
_children.clear();
156151
NSUInteger i = 0;
157152
for (id<ASLayoutable> child in children) {
158-
_childrenMap[i] = [self layoutableToAddFromLayoutable:child];
153+
_children[i] = [self layoutableToAddFromLayoutable:child];
159154
i += 1;
160155
}
161156
}
162157

163158
- (id<ASLayoutable>)childForIndex:(NSUInteger)index
164159
{
165-
if (index < _childrenMap.size()) {
166-
return _childrenMap[index];
160+
if (index < _children.size()) {
161+
return _children[index];
167162
}
168163
return nil;
169164
}
170165

171166
- (id<ASLayoutable>)child
172167
{
173-
return _childrenMap[0];
168+
return _children[0];
174169
}
175170

176171
- (NSArray *)children
177172
{
178-
// If used inside ASDK, the childrenMap property should be preferred over the children array to prevent unecessary
179-
// boxing
180173
std::vector<ASLayout *> children;
181-
for (auto const &entry : _childrenMap) {
182-
children.push_back(entry.second);
183-
}
184-
185-
return [NSArray arrayWithObjects:&children[0] count:children.size()];
186-
}
187-
188-
#pragma mark - NSFastEnumeration
189-
190-
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
191-
objects:(id __unsafe_unretained [])stackbuf
192-
count:(NSUInteger)stackbufLength
193-
{
194-
NSUInteger count = 0;
195-
unsigned long countOfItemsAlreadyEnumerated = state->state;
196-
197-
if (countOfItemsAlreadyEnumerated == 0) {
198-
state->mutationsPtr = &state->extra[0];
199-
}
200-
201-
if (countOfItemsAlreadyEnumerated < _childrenMap.size()) {
202-
state->itemsPtr = stackbuf;
203-
204-
while((countOfItemsAlreadyEnumerated < _childrenMap.size()) && (count < stackbufLength)) {
205-
stackbuf[count] = _childrenMap[countOfItemsAlreadyEnumerated];
206-
countOfItemsAlreadyEnumerated++;
207-
count++;
208-
}
209-
} else {
210-
count = 0;
174+
for (ASChildMap::iterator it = _children.begin(); it != _children.end(); ++it ) {
175+
children.push_back(it->second);
211176
}
212177

213-
state->state = countOfItemsAlreadyEnumerated;
214-
215-
return count;
178+
return [NSArray arrayWithObjects:&children[0] count:children.size()];
216179
}
217180

218181
#pragma mark - ASEnvironment
@@ -270,15 +233,6 @@ - (ASTraitCollection *)asyncTraitCollection
270233

271234
@end
272235

273-
@implementation ASLayoutSpec (Private)
274-
275-
- (ASChildrenMap)childrenMap
276-
{
277-
return _childrenMap;
278-
}
279-
280-
@end
281-
282236
@implementation ASLayoutSpec (Debugging)
283237

284238
#pragma mark - ASLayoutableAsciiArtProtocol

AsyncDisplayKit/Layout/ASStackLayoutSpec.mm

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#import "ASInternalHelpers.h"
1515

16-
#import "ASLayoutSpec+Private.h"
1716
#import "ASLayoutSpecUtilities.h"
1817
#import "ASStackBaselinePositionedLayout.h"
1918
#import "ASThread.h"
@@ -59,7 +58,7 @@ - (instancetype)initWithDirection:(ASStackLayoutDirection)direction spacing:(CGF
5958
_alignItems = alignItems;
6059
_justifyContent = justifyContent;
6160

62-
self.children = children;
61+
[self setChildren:children];
6362
return self;
6463
}
6564

@@ -121,9 +120,7 @@ - (void)setBaselineRelativeArrangement:(BOOL)baselineRelativeArrangement
121120

122121
- (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
123122
{
124-
auto const &children = self.childrenMap;
125-
126-
if (children.size() == 0) {
123+
if (self.children.count == 0) {
127124
return [ASLayout layoutWithLayoutableObject:self
128125
constrainedSizeRange:constrainedSize
129126
size:constrainedSize.min];
@@ -132,9 +129,9 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
132129
ASStackLayoutSpecStyle style = {.direction = _direction, .spacing = _spacing, .justifyContent = _justifyContent, .alignItems = _alignItems, .baselineRelativeArrangement = _baselineRelativeArrangement};
133130
BOOL needsBaselinePass = _baselineRelativeArrangement || _alignItems == ASStackLayoutAlignItemsBaselineFirst || _alignItems == ASStackLayoutAlignItemsBaselineLast;
134131

135-
std::vector<id<ASLayoutable>> stackChildren;
136-
for (auto const &entry : children) {
137-
stackChildren.push_back(entry.second);
132+
std::vector<id<ASLayoutable>> stackChildren = std::vector<id<ASLayoutable>>();
133+
for (id<ASLayoutable> child in self.children) {
134+
stackChildren.push_back(child);
138135
}
139136

140137
const auto unpositionedLayout = ASStackUnpositionedLayout::compute(stackChildren, style, constrainedSize);
@@ -146,18 +143,14 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
146143
// regardless of whether or not this stack aligns to baseline, we should let ASStackBaselinePositionedLayout::compute find the max ascender
147144
// and min descender in case this spec is a child in another spec that wants to align to a baseline.
148145
const auto baselinePositionedLayout = ASStackBaselinePositionedLayout::compute(positionedLayout, style, constrainedSize);
149-
{
146+
if (self.direction == ASStackLayoutDirectionVertical) {
147+
ASDN::MutexLocker l(__instanceLock__);
148+
self.ascender = [[self.children firstObject] ascender];
149+
self.descender = [[self.children lastObject] descender];
150+
} else {
150151
ASDN::MutexLocker l(__instanceLock__);
151-
if (self.direction == ASStackLayoutDirectionVertical) {
152-
if (stackChildren.size() > 0) {
153-
self.ascender = stackChildren.front().ascender;
154-
self.descender = stackChildren.back().descender;
155-
}
156-
157-
} else {
158-
self.ascender = baselinePositionedLayout.ascender;
159-
self.descender = baselinePositionedLayout.descender;
160-
}
152+
self.ascender = baselinePositionedLayout.ascender;
153+
self.descender = baselinePositionedLayout.descender;
161154
}
162155

163156
if (needsBaselinePass) {

AsyncDisplayKit/Layout/ASStaticLayoutSpec.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#import "ASStaticLayoutSpec.h"
1212

13-
#import "ASLayoutSpec+Private.h"
1413
#import "ASLayoutSpecUtilities.h"
1514
#import "ASLayout.h"
1615

@@ -39,8 +38,10 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
3938
{
4039
CGSize maxConstrainedSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.height);
4140

42-
NSMutableArray *sublayouts = [NSMutableArray arrayWithCapacity:self.childrenMap.size()];
43-
for (id<ASLayoutable> child in self) {
41+
NSArray *children = self.children;
42+
NSMutableArray *sublayouts = [NSMutableArray arrayWithCapacity:children.count];
43+
44+
for (id<ASLayoutable> child in children) {
4445
CGPoint layoutPosition = child.layoutPosition;
4546
CGSize autoMaxSize = CGSizeMake(maxConstrainedSize.width - layoutPosition.x,
4647
maxConstrainedSize.height - layoutPosition.y);

AsyncDisplayKit/Private/ASLayoutSpec+Private.h

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

0 commit comments

Comments
 (0)