From 3adb0b1b93d2c6515c258deebea8e1d601df7080 Mon Sep 17 00:00:00 2001 From: Li Tan Date: Tue, 6 Jan 2015 17:41:47 -0800 Subject: [PATCH 1/2] Batch insertion for reload --- AsyncDisplayKit/Details/ASDataController.mm | 38 ++++++++++++--------- examples/Kittens/Sample/ViewController.m | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index 07b634c155..1a394685b5 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -238,7 +238,7 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; INSERT_SECTIONS(_nodes , indexSet, sectionArray); }]; - [self _insertNodes:nodes atIndexPaths:indexPaths]; + [self _batchInsertNodes:nodes atIndexPaths:indexPaths]; }); } @@ -278,7 +278,7 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; }]; // reinsert the elements - [self _insertNodes:updatedNodes atIndexPaths:updatedIndexPaths]; + [self _batchInsertNodes:updatedNodes atIndexPaths:updatedIndexPaths]; }); } @@ -344,23 +344,29 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; } } -- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths { +- (void)_batchInsertNodes:(NSArray *)nodes + atIndexPaths:(NSArray *)indexPaths { NSUInteger blockSize = [[ASDataController class] parallelProcessorCount] * kASDataControllerSizingCountPerProcessor; + // Processing in batches + for (NSUInteger i = 0; i < indexPaths.count; i += blockSize) { + NSRange batchedRange = NSMakeRange(i, MIN(indexPaths.count - i, blockSize)); + NSArray *batchedIndexPaths = [indexPaths subarrayWithRange:batchedRange]; + NSArray *batchedNodes = [nodes subarrayWithRange:batchedRange]; + + [self _insertNodes:batchedNodes atIndexPaths:batchedIndexPaths]; + } +} + +- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths { // sort indexPath to avoid messing up the index when inserting in several batches NSArray *sortedIndexPaths = [indexPaths sortedArrayUsingSelector:@selector(compare:)]; - - // Processing in batches - for (NSUInteger i = 0; i < sortedIndexPaths.count; i += blockSize) { - NSArray *batchedIndexPaths = [sortedIndexPaths subarrayWithRange:NSMakeRange(i, MIN(sortedIndexPaths.count - i, blockSize))]; - NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:batchedIndexPaths.count]; - - [batchedIndexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) { - [nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]]; - }]; - - [self _insertNodes:nodes atIndexPaths:batchedIndexPaths]; + NSMutableArray *nodes = [[NSMutableArray alloc] initWithCapacity:indexPaths.count]; + for (NSUInteger i = 0; i < sortedIndexPaths.count; i++) { + [nodes addObject:[_dataSource dataController:self nodeAtIndexPath:sortedIndexPaths[i]]]; } + + [self _batchInsertNodes:nodes atIndexPaths:indexPaths]; } - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths { @@ -388,7 +394,7 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; DELETE_NODES(_nodes, indexPaths); }]; - [self _insertNodes:nodes atIndexPaths:indexPaths]; + [self _batchInsertNodes:nodes atIndexPaths:indexPaths]; }); } @@ -444,7 +450,7 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; }]; - [self _insertNodes:updatedNodes atIndexPaths:updatedIndexPaths]; + [self _batchInsertNodes:updatedNodes atIndexPaths:updatedIndexPaths]; }); } diff --git a/examples/Kittens/Sample/ViewController.m b/examples/Kittens/Sample/ViewController.m index b0a865deab..97d39368b2 100644 --- a/examples/Kittens/Sample/ViewController.m +++ b/examples/Kittens/Sample/ViewController.m @@ -16,7 +16,7 @@ #import "BlurbNode.h" #import "KittenNode.h" -static const NSInteger kLitterSize = 20; +static const NSInteger kLitterSize = 5000; @interface ViewController () From a8dbec9dfaf5ccb6cbff69d30cda31e7b316fdb4 Mon Sep 17 00:00:00 2001 From: Li Tan Date: Tue, 6 Jan 2015 17:45:48 -0800 Subject: [PATCH 2/2] fix --- examples/Kittens/Sample/ViewController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Kittens/Sample/ViewController.m b/examples/Kittens/Sample/ViewController.m index 97d39368b2..b0a865deab 100644 --- a/examples/Kittens/Sample/ViewController.m +++ b/examples/Kittens/Sample/ViewController.m @@ -16,7 +16,7 @@ #import "BlurbNode.h" #import "KittenNode.h" -static const NSInteger kLitterSize = 5000; +static const NSInteger kLitterSize = 20; @interface ViewController ()