Skip to content

Commit

Permalink
Use std collections to avoid NSNumber boxing
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseoldduck committed Jul 6, 2018
1 parent beb204e commit d4ce1bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
509E68641B3AEDB7009B9150 /* ASCollectionViewLayoutController.m in Sources */ = {isa = PBXBuildFile; fileRef = 205F0E1C1B373A2C007741D0 /* ASCollectionViewLayoutController.m */; };
509E68651B3AEDC5009B9150 /* CoreGraphics+ASConvenience.h in Headers */ = {isa = PBXBuildFile; fileRef = 205F0E1F1B376416007741D0 /* CoreGraphics+ASConvenience.h */; settings = {ATTRIBUTES = (Public, ); }; };
509E68661B3AEDD7009B9150 /* CoreGraphics+ASConvenience.m in Sources */ = {isa = PBXBuildFile; fileRef = 205F0E201B376416007741D0 /* CoreGraphics+ASConvenience.m */; };
636EA1A41C7FF4EC00EE152F /* NSArray+Diffing.m in Sources */ = {isa = PBXBuildFile; fileRef = DBC452DA1C5BF64600B16017 /* NSArray+Diffing.m */; };
636EA1A41C7FF4EC00EE152F /* NSArray+Diffing.mm in Sources */ = {isa = PBXBuildFile; fileRef = DBC452DA1C5BF64600B16017 /* NSArray+Diffing.mm */; };
636EA1A51C7FF4EF00EE152F /* ASDefaultPlayButton.m in Sources */ = {isa = PBXBuildFile; fileRef = AEB7B0191C5962EA00662EF4 /* ASDefaultPlayButton.m */; };
680346941CE4052A0009FEB4 /* ASNavigationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 68FC85DC1CE29AB700EDD713 /* ASNavigationController.h */; settings = {ATTRIBUTES = (Public, ); }; };
68355B341CB579B9001D4E68 /* ASImageNode+AnimatedImage.mm in Sources */ = {isa = PBXBuildFile; fileRef = 68355B2E1CB5799E001D4E68 /* ASImageNode+AnimatedImage.mm */; };
Expand Down Expand Up @@ -968,7 +968,7 @@
DB55C2601C6408D6004EDCF5 /* _ASTransitionContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = _ASTransitionContext.m; path = ../_ASTransitionContext.m; sourceTree = "<group>"; };
DB55C2651C641AE4004EDCF5 /* ASContextTransitioning.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASContextTransitioning.h; sourceTree = "<group>"; };
DBC452D91C5BF64600B16017 /* NSArray+Diffing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Diffing.h"; sourceTree = "<group>"; };
DBC452DA1C5BF64600B16017 /* NSArray+Diffing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Diffing.m"; sourceTree = "<group>"; };
DBC452DA1C5BF64600B16017 /* NSArray+Diffing.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSArray+Diffing.mm"; sourceTree = "<group>"; };
DBC452DD1C5C6A6A00B16017 /* ArrayDiffingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ArrayDiffingTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
DBC453211C5FD97200B16017 /* ASDisplayNodeImplicitHierarchyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ASDisplayNodeImplicitHierarchyTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
DBDB83921C6E879900D0098C /* ASPagerFlowLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASPagerFlowLayout.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1420,7 +1420,7 @@
205F0E1F1B376416007741D0 /* CoreGraphics+ASConvenience.h */,
205F0E201B376416007741D0 /* CoreGraphics+ASConvenience.m */,
DBC452D91C5BF64600B16017 /* NSArray+Diffing.h */,
DBC452DA1C5BF64600B16017 /* NSArray+Diffing.m */,
DBC452DA1C5BF64600B16017 /* NSArray+Diffing.mm */,
CC4981BA1D1C7F65004E13CC /* NSIndexSet+ASHelpers.h */,
CC4981BB1D1C7F65004E13CC /* NSIndexSet+ASHelpers.m */,
058D09F5195D050800B7D73C /* NSMutableAttributedString+TextKitAdditions.h */,
Expand Down Expand Up @@ -2410,7 +2410,7 @@
B350624E1B010EFD0018CF92 /* ASDisplayNode+AsyncDisplay.mm in Sources */,
E5667E8E1F33872700FA6FC0 /* _ASCollectionGalleryLayoutInfo.m in Sources */,
25E327591C16819500A2170C /* ASPagerNode.m in Sources */,
636EA1A41C7FF4EC00EE152F /* NSArray+Diffing.m in Sources */,
636EA1A41C7FF4EC00EE152F /* NSArray+Diffing.mm in Sources */,
B35062501B010EFD0018CF92 /* ASDisplayNode+DebugTiming.mm in Sources */,
DEC146B91C37A16A004A0EE7 /* ASCollectionInternal.m in Sources */,
254C6B891BF94F8A003EC431 /* ASTextKitRenderer+Positioning.mm in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//

#import <AsyncDisplayKit/NSArray+Diffing.h>
#import <unordered_map>
#import <queue>
#import <AsyncDisplayKit/ASAssert.h>

@implementation NSArray (Diffing)
Expand Down Expand Up @@ -43,13 +45,14 @@ - (void)asdk_diffWithArray:(NSArray *)array insertions:(NSIndexSet **)insertions
- (void)asdk_diffWithArray:(NSArray *)array insertions:(NSIndexSet **)insertions deletions:(NSIndexSet **)deletions
moves:(NSArray<NSIndexPath *> **)moves compareBlock:(compareBlock)comparison
{
typedef std::unordered_map<NSUInteger, std::queue<NSUInteger>> move_map;
NSAssert(comparison != nil, @"Comparison block is required");
NSAssert(moves == nil || comparison == [NSArray defaultCompareBlock], @"move detection requires isEqual: and hash (no custom compare)");
NSMutableDictionary<NSNumber *, NSMutableArray<id> *> *potentialMoves = nil;
NSAssert(moves == nil || comparison == [NSArray defaultCompareBlock], @"move detection requires isEqual: and hash (no custom compare)");
std::unique_ptr<move_map> potentialMoves(nullptr);
NSMutableArray<NSIndexPath *> *moveIndexPaths = nil;
NSMutableIndexSet *insertionIndexes = nil, *deletionIndexes = nil;
if (moves) {
potentialMoves = [NSMutableDictionary new];
potentialMoves = std::unique_ptr<move_map>(new move_map());
moveIndexPaths = [NSMutableArray new];
}
NSMutableIndexSet *commonIndexes = [[self _asdk_commonIndexesWithArray:array compareBlock:comparison] mutableCopy];
Expand All @@ -60,12 +63,11 @@ - (void)asdk_diffWithArray:(NSArray *)array insertions:(NSIndexSet **)insertions
if (![commonIndexes containsIndex:i]) {
[deletionIndexes addIndex:i];
}
NSNumber *hash = @([self[i] hash]);
if (!potentialMoves[hash]) {
potentialMoves[hash] = [@[@(i)] mutableCopy];
} else {
[potentialMoves[hash] addObject:@(i)];
NSUInteger hash = [self[i] hash];
if (potentialMoves) {
(*potentialMoves)[hash].push(i);
}

}
}

Expand All @@ -75,13 +77,13 @@ - (void)asdk_diffWithArray:(NSArray *)array insertions:(NSIndexSet **)insertions
BOOL moveFound;
NSUInteger movedFrom = NSNotFound;
for (NSUInteger i = 0, j = 0; j < array.count; j++) {
NSNumber *hash = @([array[j] hash]);
moveFound = (potentialMoves[hash] != nil);
NSUInteger hash = [array[j] hash];
moveFound = (potentialMoves && potentialMoves->count(hash));
if (moveFound) {
movedFrom = [[potentialMoves[hash] firstObject] unsignedIntegerValue];
[potentialMoves[hash] removeObjectAtIndex:0];
if (![potentialMoves[hash] count]) {
potentialMoves[hash] = nil;
movedFrom = (*potentialMoves)[hash].front();
(*potentialMoves)[hash].pop();
if ((*potentialMoves)[hash].empty()) {
potentialMoves->erase(hash);
}
if (movedFrom != j) {
NSUInteger indexes[] = {movedFrom, j};
Expand Down

0 comments on commit d4ce1bc

Please sign in to comment.