optimize. avoid unncessary copies.

Summary: Reduce and avoid unnecessary copies in IGListAdapter and IGListSectionMap.

Reviewed By: rnystrom

Differential Revision: D4555347

fbshipit-source-id: 3ade3311955fe5d12fc7617ad72feba9dafb60fb
This commit is contained in:
Jesse Squires 2017-02-14 15:21:11 -08:00 committed by Facebook Github Bot
parent 9c37fbf8a4
commit 7a94167be1
2 changed files with 19 additions and 16 deletions

View file

@ -259,13 +259,13 @@
return;
}
NSArray *fromObjects = [self.sectionMap.objects copy];
NSArray *newItems = [[dataSource objectsForListAdapter:self] copy];
NSArray *fromObjects = self.sectionMap.objects;
NSArray *newObjects = [dataSource objectsForListAdapter:self];
__weak __typeof__(self) weakSelf = self;
[self.updater performUpdateWithCollectionView:collectionView
fromObjects:fromObjects
toObjects:newItems
toObjects:newObjects
animated:animated
objectTransitionBlock:^(NSArray *toObjects) {
// temporarily capture the item map that we are transitioning from in case

View file

@ -14,10 +14,10 @@
@interface IGListSectionMap ()
// both of these maps allow fast lookups of objects, list objects, and indexes
@property (nonatomic, strong, readonly) NSMapTable<id, IGListSectionController<IGListSectionType> *> *objectToSectionControllerMap;
@property (nonatomic, strong, readonly) NSMapTable<IGListSectionController<IGListSectionType> *, NSNumber *> *sectionControllerToSectionMap;
@property (nonatomic, strong, readonly, nonnull) NSMapTable<id, IGListSectionController<IGListSectionType> *> *objectToSectionControllerMap;
@property (nonatomic, strong, readonly, nonnull) NSMapTable<IGListSectionController<IGListSectionType> *, NSNumber *> *sectionControllerToSectionMap;
@property (nonatomic, strong, readwrite) NSArray *objects;
@property (nonatomic, strong, nonnull) NSMutableArray *mObjects;
@end
@ -33,7 +33,7 @@
_sectionControllerToSectionMap = [[NSMapTable alloc] initWithKeyOptions:NSMapTableStrongMemory | NSMapTableObjectPointerPersonality
valueOptions:NSMapTableStrongMemory
capacity:0];
_objects = [NSArray new];
_mObjects = [NSMutableArray new];
}
return self;
}
@ -41,6 +41,10 @@
#pragma mark - Public API
- (NSArray *)objects {
return [self.mObjects copy];
}
- (NSInteger)sectionForSectionController:(IGListSectionController <IGListSectionType> *)sectionController {
IGParameterAssert(sectionController != nil);
@ -55,7 +59,7 @@
- (void)updateWithObjects:(NSArray *)objects sectionControllers:(NSArray *)sectionControllers {
IGParameterAssert(objects.count == sectionControllers.count);
self.objects = [objects copy];
self.mObjects = [objects mutableCopy];
[self reset];
@ -74,8 +78,8 @@
return [self.objectToSectionControllerMap objectForKey:object];
}
- (id)objectForSection:(NSInteger)section {
NSArray *objects = self.objects;
- (nullable id)objectForSection:(NSInteger)section {
NSArray *objects = self.mObjects;
if (section < objects.count) {
return objects[section];
} else {
@ -105,10 +109,7 @@
id sectionController = [self sectionControllerForObject:object];
[self.sectionControllerToSectionMap setObject:@(section) forKey:sectionController];
[self.objectToSectionControllerMap setObject:sectionController forKey:object];
NSMutableArray *mobjects = [self.objects mutableCopy];
mobjects[section] = object;
self.objects = [mobjects copy];
self.mObjects[section] = object;
}
- (void)enumerateUsingBlock:(void (^)(id object, IGListSectionController <IGListSectionType> *sectionController, NSInteger section, BOOL *stop))block {
@ -131,8 +132,10 @@
- (id)copyWithZone:(NSZone *)zone {
IGListSectionMap *copy = [[IGListSectionMap allocWithZone:zone] initWithMapTable:self.objectToSectionControllerMap];
copy->_sectionControllerToSectionMap = [self.sectionControllerToSectionMap copy];
copy->_objects = [self.objects copy];
if (copy != nil) {
copy->_sectionControllerToSectionMap = [self.sectionControllerToSectionMap copy];
copy->_mObjects = [self.mObjects mutableCopy];
}
return copy;
}