Skip to content

Commit 2f1d57e

Browse files
committed
Merge pull request facebookarchive#708 from Adlai-Holler/FasterLayoutNodes
Faster handling of iteration to lay out nodes in ASDataController.
2 parents 67cf6d3 + 99b9f73 commit 2f1d57e

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

AsyncDisplayKit/Details/ASDataController.mm

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,27 @@ - (void)_layoutNodes:(NSArray *)nodes atIndexPaths:(NSArray *)indexPaths withAni
105105
}
106106

107107
dispatch_group_t layoutGroup = dispatch_group_create();
108-
108+
ASSizeRange *nodeBoundSizes = (ASSizeRange *)malloc(sizeof(ASSizeRange) * nodes.count);
109109
for (NSUInteger j = 0; j < nodes.count && j < indexPaths.count; j += kASDataControllerSizingCountPerProcessor) {
110-
NSArray *subIndexPaths = [indexPaths subarrayWithRange:NSMakeRange(j, MIN(kASDataControllerSizingCountPerProcessor, indexPaths.count - j))];
110+
NSInteger batchCount = MIN(kASDataControllerSizingCountPerProcessor, indexPaths.count - j);
111111

112-
//TODO: There should be a fast-path that avoids all of this object creation.
113-
NSMutableArray *nodeBoundSizes = [[NSMutableArray alloc] initWithCapacity:kASDataControllerSizingCountPerProcessor];
114-
[subIndexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
115-
ASSizeRange constrainedSize = [_dataSource dataController:self constrainedSizeForNodeAtIndexPath:indexPath];
116-
[nodeBoundSizes addObject:[NSValue valueWithBytes:&constrainedSize objCType:@encode(ASSizeRange)]];
117-
}];
112+
for (NSUInteger k = j; k < j + batchCount; k++) {
113+
nodeBoundSizes[k] = [_dataSource dataController:self constrainedSizeForNodeAtIndexPath:indexPaths[k]];
114+
}
118115

119116
dispatch_group_async(layoutGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
120-
[subIndexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
121-
ASCellNode *node = nodes[j + idx];
122-
ASSizeRange constrainedSize;
123-
[nodeBoundSizes[idx] getValue:&constrainedSize];
117+
for (NSUInteger k = j; k < j + batchCount; k++) {
118+
ASCellNode *node = nodes[k];
119+
ASSizeRange constrainedSize = nodeBoundSizes[k];
124120
[node measureWithSizeRange:constrainedSize];
125-
node.frame = CGRectMake(0.0f, 0.0f, node.calculatedSize.width, node.calculatedSize.height);
126-
}];
121+
node.frame = CGRectMake(0, 0, node.calculatedSize.width, node.calculatedSize.height);
122+
}
127123
});
128124
}
129125

130126
// Block the _editingTransactionQueue from executing a new edit transaction until layout is done & _editingNodes array is updated.
131127
dispatch_group_wait(layoutGroup, DISPATCH_TIME_FOREVER);
132-
128+
free(nodeBoundSizes);
133129
// Insert finished nodes into data storage
134130
[self _insertNodes:nodes atIndexPaths:indexPaths withAnimationOptions:animationOptions];
135131
}

0 commit comments

Comments
 (0)