From d4ce1bcdec0904e0f14d8baeb642f339e6dcadb4 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Fri, 6 Jul 2018 12:54:03 -0700 Subject: [PATCH] Use std collections to avoid NSNumber boxing --- AsyncDisplayKit.xcodeproj/project.pbxproj | 8 ++--- .../{NSArray+Diffing.m => NSArray+Diffing.mm} | 30 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) rename Source/Details/{NSArray+Diffing.m => NSArray+Diffing.mm} (88%) diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index d01f0a005..4ce8706e6 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -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 */; }; @@ -968,7 +968,7 @@ DB55C2601C6408D6004EDCF5 /* _ASTransitionContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = _ASTransitionContext.m; path = ../_ASTransitionContext.m; sourceTree = ""; }; DB55C2651C641AE4004EDCF5 /* ASContextTransitioning.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASContextTransitioning.h; sourceTree = ""; }; DBC452D91C5BF64600B16017 /* NSArray+Diffing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Diffing.h"; sourceTree = ""; }; - DBC452DA1C5BF64600B16017 /* NSArray+Diffing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Diffing.m"; sourceTree = ""; }; + DBC452DA1C5BF64600B16017 /* NSArray+Diffing.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSArray+Diffing.mm"; sourceTree = ""; }; DBC452DD1C5C6A6A00B16017 /* ArrayDiffingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ArrayDiffingTests.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; DBC453211C5FD97200B16017 /* ASDisplayNodeImplicitHierarchyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ASDisplayNodeImplicitHierarchyTests.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; DBDB83921C6E879900D0098C /* ASPagerFlowLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASPagerFlowLayout.h; sourceTree = ""; }; @@ -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 */, @@ -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 */, diff --git a/Source/Details/NSArray+Diffing.m b/Source/Details/NSArray+Diffing.mm similarity index 88% rename from Source/Details/NSArray+Diffing.m rename to Source/Details/NSArray+Diffing.mm index d8714f453..05767fe8d 100644 --- a/Source/Details/NSArray+Diffing.m +++ b/Source/Details/NSArray+Diffing.mm @@ -16,6 +16,8 @@ // #import +#import +#import #import @implementation NSArray (Diffing) @@ -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 **)moves compareBlock:(compareBlock)comparison { + typedef std::unordered_map> 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 *> *potentialMoves = nil; + NSAssert(moves == nil || comparison == [NSArray defaultCompareBlock], @"move detection requires isEqual: and hash (no custom compare)"); + std::unique_ptr potentialMoves(nullptr); NSMutableArray *moveIndexPaths = nil; NSMutableIndexSet *insertionIndexes = nil, *deletionIndexes = nil; if (moves) { - potentialMoves = [NSMutableDictionary new]; + potentialMoves = std::unique_ptr(new move_map()); moveIndexPaths = [NSMutableArray new]; } NSMutableIndexSet *commonIndexes = [[self _asdk_commonIndexesWithArray:array compareBlock:comparison] mutableCopy]; @@ -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); } + } } @@ -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};