From 470da6e584ec2a2730f6fe92dc62cfd4cff9c269 Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Mon, 17 Sep 2018 19:35:39 -0700 Subject: [PATCH] Remove ASRectMap, which is not worth its own weight --- AsyncDisplayKit.xcodeproj/project.pbxproj | 13 ---- CHANGELOG.md | 1 + Source/Layout/ASLayout.mm | 18 ++---- Source/Private/ASRectMap.h | 48 --------------- Source/Private/ASRectMap.mm | 74 ----------------------- Tests/ASRectMapTests.m | 51 ---------------- 6 files changed, 7 insertions(+), 198 deletions(-) delete mode 100644 Source/Private/ASRectMap.h delete mode 100644 Source/Private/ASRectMap.mm delete mode 100644 Tests/ASRectMapTests.m diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index e916d4bf5..52ac52e89 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -459,9 +459,6 @@ DECBD6EA1BE56E1900CF4905 /* ASButtonNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = DECBD6E61BE56E1900CF4905 /* ASButtonNode.mm */; }; DEFAD8131CC48914000527C4 /* ASVideoNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = AEEC47E01C20C2DD00EC1693 /* ASVideoNode.mm */; }; E51B78BF1F028ABF00E32604 /* ASLayoutFlatteningTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E51B78BD1F01A0EE00E32604 /* ASLayoutFlatteningTests.m */; }; - E52AC9BA1FEA90EB00AA4040 /* ASRectMap.mm in Sources */ = {isa = PBXBuildFile; fileRef = E52AC9B81FEA90EB00AA4040 /* ASRectMap.mm */; }; - E52AC9BB1FEA90EB00AA4040 /* ASRectMap.h in Headers */ = {isa = PBXBuildFile; fileRef = E52AC9B91FEA90EB00AA4040 /* ASRectMap.h */; settings = {ATTRIBUTES = (Private, ); }; }; - E52AC9C01FEA916C00AA4040 /* ASRectMapTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E52AC9BE1FEA915D00AA4040 /* ASRectMapTests.m */; }; E54E00721F1D3828000B30D7 /* ASPagerNode+Beta.h in Headers */ = {isa = PBXBuildFile; fileRef = E54E00711F1D3828000B30D7 /* ASPagerNode+Beta.h */; settings = {ATTRIBUTES = (Public, ); }; }; E54E81FC1EB357BD00FFE8E1 /* ASPageTable.h in Headers */ = {isa = PBXBuildFile; fileRef = E54E81FA1EB357BD00FFE8E1 /* ASPageTable.h */; }; E54E81FD1EB357BD00FFE8E1 /* ASPageTable.m in Sources */ = {isa = PBXBuildFile; fileRef = E54E81FB1EB357BD00FFE8E1 /* ASPageTable.m */; }; @@ -990,9 +987,6 @@ E51B78BD1F01A0EE00E32604 /* ASLayoutFlatteningTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASLayoutFlatteningTests.m; sourceTree = ""; }; E52405B21C8FEF03004DC8E7 /* ASLayoutTransition.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASLayoutTransition.mm; sourceTree = ""; }; E52405B41C8FEF16004DC8E7 /* ASLayoutTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASLayoutTransition.h; sourceTree = ""; }; - E52AC9B81FEA90EB00AA4040 /* ASRectMap.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASRectMap.mm; sourceTree = ""; }; - E52AC9B91FEA90EB00AA4040 /* ASRectMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASRectMap.h; sourceTree = ""; }; - E52AC9BE1FEA915D00AA4040 /* ASRectMapTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASRectMapTests.m; sourceTree = ""; }; E54E00711F1D3828000B30D7 /* ASPagerNode+Beta.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ASPagerNode+Beta.h"; sourceTree = ""; }; E54E81FA1EB357BD00FFE8E1 /* ASPageTable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASPageTable.h; sourceTree = ""; }; E54E81FB1EB357BD00FFE8E1 /* ASPageTable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASPageTable.m; sourceTree = ""; }; @@ -1258,7 +1252,6 @@ isa = PBXGroup; children = ( F3F698D1211CAD4600800CB1 /* ASDisplayViewAccessibilityTests.mm */, - CC35CEC520DD87280006448D /* ASCollectionsTests.m */, DBC452DD1C5C6A6A00B16017 /* ArrayDiffingTests.m */, AC026B571BD3F61800BBC17E /* ASAbsoluteLayoutSpecSnapshotTests.m */, 696FCB301D6E46050093471E /* ASBackgroundLayoutSpecSnapshotTests.mm */, @@ -1308,7 +1301,6 @@ CC8B05D51D73836400F54286 /* ASPerformanceTestContext.m */, CC7FD9E01BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m */, ACF6ED5A1B178DC700DA7C62 /* ASRatioLayoutSpecSnapshotTests.mm */, - E52AC9BE1FEA915D00AA4040 /* ASRectMapTests.m */, CCAA0B81206ADECB0057B336 /* ASRecursiveUnfairLockTests.m */, 7AB338681C55B97B0055FDE8 /* ASRelativeLayoutSpecSnapshotTests.mm */, 4E9127681F64157600499623 /* ASRunLoopQueueTests.m */, @@ -1516,8 +1508,6 @@ CCED5E402020D41600395C40 /* ASNetworkImageLoadInfo+Private.h */, CC3B20811C3F76D600798563 /* ASPendingStateController.h */, CC3B20821C3F76D600798563 /* ASPendingStateController.mm */, - E52AC9B91FEA90EB00AA4040 /* ASRectMap.h */, - E52AC9B81FEA90EB00AA4040 /* ASRectMap.mm */, CC55A70F1E52A0F200594372 /* ASResponderChainEnumerator.h */, CC55A7101E52A0F200594372 /* ASResponderChainEnumerator.m */, CC512B841DAC45C60054848E /* ASTableView+Undeprecated.h */, @@ -1968,7 +1958,6 @@ E5775B021F16759300CAC9BC /* ASCollectionLayoutCache.h in Headers */, E5775B001F13D25400CAC9BC /* ASCollectionLayoutState+Private.h in Headers */, E5667E8C1F33871300FA6FC0 /* _ASCollectionGalleryLayoutInfo.h in Headers */, - E52AC9BB1FEA90EB00AA4040 /* ASRectMap.h in Headers */, E5775AFC1F13CE9F00CAC9BC /* _ASCollectionGalleryLayoutItem.h in Headers */, E5855DF01EBB4D83003639AE /* ASCollectionLayoutDefines.h in Headers */, E5B5B9D11E9BAD9800A6B726 /* ASCollectionLayoutContext+Private.h in Headers */, @@ -2332,7 +2321,6 @@ 05EA6FE71AC0966E00E35788 /* ASSnapshotTestCase.m in Sources */, CC35CEC620DD87280006448D /* ASCollectionsTests.m in Sources */, ACF6ED631B178DC700DA7C62 /* ASStackLayoutSpecSnapshotTests.mm in Sources */, - E52AC9C01FEA916C00AA4040 /* ASRectMapTests.m in Sources */, CCE4F9BA1F0DBB5000062E4E /* ASLayoutTestNode.mm in Sources */, CCAA0B82206ADECB0057B336 /* ASRecursiveUnfairLockTests.m in Sources */, 81E95C141D62639600336598 /* ASTextNodeSnapshotTests.m in Sources */, @@ -2362,7 +2350,6 @@ 3917EBD51E9C2FC400D04A01 /* _ASCollectionReusableView.m in Sources */, CCA282D11E9EBF6C0037E8B7 /* ASTipsWindow.m in Sources */, CCCCCCE41EC3EF060087FE10 /* NSParagraphStyle+ASText.m in Sources */, - E52AC9BA1FEA90EB00AA4040 /* ASRectMap.mm in Sources */, 8BBBAB8D1CEBAF1E00107FC6 /* ASDefaultPlaybackButton.m in Sources */, B30BF6541C59D889004FCD53 /* ASLayoutManager.m in Sources */, 92DD2FE71BF4D0850074C9DD /* ASMapNode.mm in Sources */, diff --git a/CHANGELOG.md b/CHANGELOG.md index 85cf0c475..c86e1ff5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ - Improve TextNode2 by skipping an unneeded copy during measurement. [Adlai Holler](https://github.com/Adlai-Holler) - Improve locking around clearContents [Michael Schneider](https://github.com/maicki) - Unlock before cleanup and calling out to subclass hooks for animated images. [Michael Schneider](https://github.com/maicki) [#1087](https://github.com/TextureGroup/Texture/pull/1087) +- Optimize layout process by removing `ASRectMap`. [Adlai Holler](https://github.com/Adlai-Holler) ## 2.7 - Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877) diff --git a/Source/Layout/ASLayout.mm b/Source/Layout/ASLayout.mm index 1c8343388..ed9271448 100644 --- a/Source/Layout/ASLayout.mm +++ b/Source/Layout/ASLayout.mm @@ -20,7 +20,6 @@ #import #import #import -#import CGPoint const ASPointNull = {NAN, NAN}; @@ -54,9 +53,6 @@ @interface ASLayout () ASLayoutElementType _layoutElementType; std::atomic_bool _retainSublayoutElements; } - -@property (nonatomic, readonly) ASRectMap *elementToRectMap; - @end @implementation ASLayout @@ -110,13 +106,6 @@ - (instancetype)initWithLayoutElement:(id)layoutElement } _sublayouts = [sublayouts copy] ?: @[]; - - if (_sublayouts.count > 0) { - _elementToRectMap = [ASRectMap rectMapForWeakObjectPointers]; - for (ASLayout *layout in sublayouts) { - [_elementToRectMap setRect:layout.frame forKey:layout.layoutElement]; - } - } if ([ASLayout shouldRetainSublayoutLayoutElements]) { [self retainSublayoutElements]; @@ -294,7 +283,12 @@ - (ASLayoutElementType)type - (CGRect)frameForElement:(id)layoutElement { - return _elementToRectMap ? [_elementToRectMap rectForKey:layoutElement] : CGRectNull; + for (ASLayout *l in _sublayouts) { + if (l->_layoutElement == layoutElement) { + return l.frame; + } + } + return CGRectNull; } - (CGRect)frame diff --git a/Source/Private/ASRectMap.h b/Source/Private/ASRectMap.h deleted file mode 100644 index 88e8c8a2d..000000000 --- a/Source/Private/ASRectMap.h +++ /dev/null @@ -1,48 +0,0 @@ -// -// ASRectMap.h -// Texture -// -// Copyright (c) Pinterest, Inc. All rights reserved. -// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 -// - -#import -#import - -NS_ASSUME_NONNULL_BEGIN - -/** - * A category for indexing weak pointers to CGRects. Similar to ASIntegerMap. - */ -@interface ASRectMap : NSObject - -/** - * Creates a new rect map. The keys are never retained. - */ -+ (ASRectMap *)rectMapForWeakObjectPointers NS_RETURNS_RETAINED; - -/** - * Retrieves the rect for a given key, or CGRectNull if the key is not found. - * - * @param key An object to lookup the rect for. - */ -- (CGRect)rectForKey:(id)key; - -/** - * Sets the given rect for the associated key. Key *will not be retained!* - * - * @param rect The rect to store as value. - * @param key The key to use for the rect. - */ -- (void)setRect:(CGRect)rect forKey:(id)key; - -/** - * Removes the rect for the given key, if one exists. - * - * @param key The key to remove. - */ -- (void)removeRectForKey:(id)key; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Source/Private/ASRectMap.mm b/Source/Private/ASRectMap.mm deleted file mode 100644 index ed7b82ba0..000000000 --- a/Source/Private/ASRectMap.mm +++ /dev/null @@ -1,74 +0,0 @@ -// -// ASRectMap.mm -// Texture -// -// Copyright (c) Pinterest, Inc. All rights reserved. -// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 -// - -#import "ASRectMap.h" -#import "ASObjectDescriptionHelpers.h" -#import -#import - -@implementation ASRectMap { - std::unordered_map _map; -} - -+ (ASRectMap *)rectMapForWeakObjectPointers NS_RETURNS_RETAINED -{ - return [[self alloc] init]; -} - -- (CGRect)rectForKey:(id)key -{ - let result = _map.find((__bridge void *)key); - if (result != _map.end()) { - // result->first is the key; result->second is the value, a CGRect. - return result->second; - } else { - return CGRectNull; - } -} - -- (void)setRect:(CGRect)rect forKey:(id)key -{ - if (key) { - _map[(__bridge void *)key] = rect; - } -} - -- (void)removeRectForKey:(id)key -{ - if (key) { - _map.erase((__bridge void *)key); - } -} - -- (id)copyWithZone:(NSZone *)zone -{ - ASRectMap *copy = [ASRectMap rectMapForWeakObjectPointers]; - copy->_map = _map; - return copy; -} - -- (NSMutableArray *)propertiesForDescription -{ - NSMutableArray *result = [NSMutableArray array]; - - // { ptr1->rect1 ptr2->rect2 ptr3->rect3 } - NSMutableString *str = [NSMutableString string]; - for (let &e : _map) { - [str appendFormat:@" %@->%@", e.first, NSStringFromCGRect(e.second)]; - } - [result addObject:@{ @"ASRectMap": str }]; - - return result; -} - -- (NSString *)description -{ - return ASObjectDescriptionMakeWithoutObject([self propertiesForDescription]); -} - -@end diff --git a/Tests/ASRectMapTests.m b/Tests/ASRectMapTests.m deleted file mode 100644 index be5e2ef3d..000000000 --- a/Tests/ASRectMapTests.m +++ /dev/null @@ -1,51 +0,0 @@ -// -// ASRectMapTests.m -// Texture -// -// Copyright (c) Pinterest, Inc. All rights reserved. -// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 -// - -#import - -#import "ASRectMap.h" -#import "ASXCTExtensions.h" - -@interface ASRectMapTests : XCTestCase -@end - -@implementation ASRectMapTests - -- (void)testThatItStoresRects -{ - ASRectMap *table = [ASRectMap rectMapForWeakObjectPointers]; - NSObject *key0 = [[NSObject alloc] init]; - NSObject *key1 = [[NSObject alloc] init]; - ASXCTAssertEqualRects([table rectForKey:key0], CGRectNull); - ASXCTAssertEqualRects([table rectForKey:key1], CGRectNull); - CGRect rect0 = CGRectMake(0, 0, 100, 100); - CGRect rect1 = CGRectMake(0, 0, 50, 50); - [table setRect:rect0 forKey:key0]; - [table setRect:rect1 forKey:key1]; - - ASXCTAssertEqualRects([table rectForKey:key0], rect0); - ASXCTAssertEqualRects([table rectForKey:key1], rect1); -} - - -- (void)testCopying -{ - ASRectMap *table = [ASRectMap rectMapForWeakObjectPointers]; - NSObject *key = [[NSObject alloc] init]; - ASXCTAssertEqualRects([table rectForKey:key], CGRectNull); - CGRect rect0 = CGRectMake(0, 0, 100, 100); - CGRect rect1 = CGRectMake(0, 0, 50, 50); - [table setRect:rect0 forKey:key]; - ASRectMap *copy = [table copy]; - [copy setRect:rect1 forKey:key]; - - ASXCTAssertEqualRects([table rectForKey:key], rect0); - ASXCTAssertEqualRects([copy rectForKey:key], rect1); -} - -@end