From cac0cce0b04821f99149060d339c669d3d4799f4 Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Thu, 14 Jul 2016 16:11:28 -0700 Subject: [PATCH] [ASLayoutTransition] Make findNodesInLayoutAtIndexesWithFilteredNodes faster --- AsyncDisplayKit/Private/ASLayoutTransition.mm | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/AsyncDisplayKit/Private/ASLayoutTransition.mm b/AsyncDisplayKit/Private/ASLayoutTransition.mm index f054eea6..921008dc 100644 --- a/AsyncDisplayKit/Private/ASLayoutTransition.mm +++ b/AsyncDisplayKit/Private/ASLayoutTransition.mm @@ -199,21 +199,27 @@ static inline void findNodesInLayoutAtIndexesWithFilteredNodes(ASLayout *layout, NSArray * __strong *storedNodes, std::vector *storedPositions) { - NSMutableArray *nodes = [NSMutableArray array]; + NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:indexes.count]; std::vector positions = std::vector(); - NSUInteger idx = [indexes firstIndex]; - while (idx != NSNotFound) { - ASDisplayNode *node = (ASDisplayNode *)layout.sublayouts[idx].layoutableObject; - ASDisplayNodeCAssert(node, @"A flattened layout must consist exclusively of node sublayouts"); - // Ignore the odd case in which a non-node sublayout is accessed and the type cast fails - if (node != nil) { - BOOL notFiltered = (filteredNodes == nil || [filteredNodes indexOfObjectIdenticalTo:node] == NSNotFound); - if (notFiltered) { - [nodes addObject:node]; - positions.push_back(idx); + // From inspection, this is how enumerateObjectsAtIndexes: works under the hood + NSUInteger firstIndex = indexes.firstIndex; + NSUInteger lastIndex = indexes.lastIndex; + NSUInteger idx = 0; + for (ASLayout *sublayout in layout.sublayouts) { + if (idx > lastIndex) { break; } + if (idx >= firstIndex && [indexes containsIndex:idx]) { + ASDisplayNode *node = (ASDisplayNode *)sublayout.layoutableObject; + ASDisplayNodeCAssert(node, @"A flattened layout must consist exclusively of node sublayouts"); + // Ignore the odd case in which a non-node sublayout is accessed and the type cast fails + if (node != nil) { + BOOL notFiltered = (filteredNodes == nil || [filteredNodes indexOfObjectIdenticalTo:node] == NSNotFound); + if (notFiltered) { + [nodes addObject:node]; + positions.push_back(idx); + } } } - idx = [indexes indexGreaterThanIndex:idx]; + idx += 1; } *storedNodes = nodes; *storedPositions = positions;