Revert "[ASLayoutSpec] Use childrenMap directly to prevent creating an NSArray within ASDK Part 2 (#2021)"

This reverts commit d5a7c19522.
This commit is contained in:
Michael Schneider
2016-08-02 10:10:36 -07:00
parent d5a7c19522
commit d5cbe33686
11 changed files with 57 additions and 185 deletions

View File

@@ -8,7 +8,7 @@
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "ASLayoutSpec+Private.h"
#import "ASLayoutSpec.h"
#import "ASAssert.h"
#import "ASEnvironmentInternal.h"
@@ -17,13 +17,16 @@
#import "ASThread.h"
#import "ASTraitCollection.h"
#import <objc/runtime.h>
#import <map>
#import <vector>
typedef std::map<unsigned long, id<ASLayoutable>, std::less<unsigned long>> ASChildMap;
@interface ASLayoutSpec() {
ASEnvironmentState _environmentState;
ASDN::RecursiveMutex __instanceLock__;
ASChildrenMap _childrenMap;
unsigned long _mutations;
ASChildMap _children;
}
@end
@@ -32,7 +35,6 @@
// these dynamic properties all defined in ASLayoutOptionsPrivate.m
@dynamic spacingAfter, spacingBefore, flexGrow, flexShrink, flexBasis,
alignSelf, ascender, descender, sizeRange, layoutPosition, layoutableType;
@synthesize parent = _parent;
@synthesize isFinalLayoutable = _isFinalLayoutable;
- (instancetype)init
@@ -42,7 +44,6 @@
}
_isMutable = YES;
_environmentState = ASEnvironmentStateMakeDefault();
_mutations = 0;
return self;
}
@@ -104,8 +105,6 @@
return child;
}
#pragma mark - Parent
- (void)setParent:(id<ASLayoutable>)parent
{
// FIXME: Locking should be evaluated here. _parent is not widely used yet, though.
@@ -116,16 +115,18 @@
}
}
- (id<ASLayoutable>)parent
{
return _parent;
}
#pragma mark - Children
- (void)setChild:(id<ASLayoutable>)child
{
[self setChild:child forIndex:0];
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
if (child) {
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
if (finalLayoutable) {
_children[0] = finalLayoutable;
[self propagateUpLayoutable:finalLayoutable];
}
} else {
_children.erase(0);
}
}
- (void)setChild:(id<ASLayoutable>)child forIndex:(NSUInteger)index
@@ -133,16 +134,11 @@
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
if (child) {
id<ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable:child];
if (finalLayoutable) {
_childrenMap[index] = finalLayoutable;
[self propagateUpLayoutable:finalLayoutable];
}
_children[index] = finalLayoutable;
} else {
_childrenMap.erase(index);
_children.erase(index);
}
_mutations++;
// TODO: Should we propagate up the layoutable as it could happen that multiple children will propagated up their
// TODO: Should we propagate up the layoutable at it could happen that multiple children will propagated up their
// layout options and one child will overwrite values from another child
// [self propagateUpLayoutable:finalLayoutable];
}
@@ -151,73 +147,37 @@
{
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
_childrenMap.clear();
_children.clear();
NSUInteger i = 0;
for (id<ASLayoutable> child in children) {
_childrenMap[i] = [self layoutableToAddFromLayoutable:child];
_children[i] = [self layoutableToAddFromLayoutable:child];
i += 1;
_mutations++;
}
}
- (id<ASLayoutable>)childForIndex:(NSUInteger)index
{
if (index < _childrenMap.size()) {
return _childrenMap[index];
if (index < _children.size()) {
return _children[index];
}
return nil;
}
- (id<ASLayoutable>)child
{
return _childrenMap[0];
return _children[0];
}
- (NSArray *)children
{
// If used inside ASDK, the childrenMap property should be preferred over the children array to prevent
// unecessary boxing
std::vector<ASLayout *> children;
for (auto const &entry : _childrenMap) {
children.push_back(entry.second);
for (ASChildMap::iterator it = _children.begin(); it != _children.end(); ++it ) {
children.push_back(it->second);
}
return [NSArray arrayWithObjects:&children[0] count:children.size()];
}
#pragma mark - NSFastEnumeration
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
objects:(id __unsafe_unretained [])stackbuf
count:(NSUInteger)stackbufLength
{
NSUInteger count = 0;
unsigned long countOfItemsAlreadyEnumerated = state->state;
if (countOfItemsAlreadyEnumerated == 0) {
state->mutationsPtr = &_mutations;
}
if (countOfItemsAlreadyEnumerated < _childrenMap.size()) {
state->itemsPtr = stackbuf;
while((countOfItemsAlreadyEnumerated < _childrenMap.size()) && (count < stackbufLength)) {
// Hold on for the object while enumerating
__autoreleasing id child = _childrenMap[countOfItemsAlreadyEnumerated];
stackbuf[count] = child;
countOfItemsAlreadyEnumerated++;
count++;
}
} else {
count = 0;
}
state->state = countOfItemsAlreadyEnumerated;
return count;
}
#pragma mark - ASEnvironment
- (ASEnvironmentState)environmentState
@@ -273,15 +233,6 @@ ASEnvironmentLayoutExtensibilityForwarding
@end
@implementation ASLayoutSpec (Private)
- (ASChildrenMap)childrenMap
{
return _childrenMap;
}
@end
@implementation ASLayoutSpec (Debugging)
#pragma mark - ASLayoutableAsciiArtProtocol