Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reclaim CALayer asyncTransactions, use pointer-based NSMutableSet instead #1510

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
@class _ASAsyncTransaction;

@interface CALayer (ASAsyncTransactionContainerTransactions)
@property (nonatomic, nullable, setter=asyncdisplaykit_setAsyncLayerTransactions:) NSHashTable<_ASAsyncTransaction *> *asyncdisplaykit_asyncLayerTransactions;
@property (nonatomic, nullable, setter=asyncdisplaykit_setAsyncLayerTransactions:) NSMutableSet<_ASAsyncTransaction *> *asyncdisplaykit_asyncLayerTransactions;

- (void)asyncdisplaykit_asyncTransactionContainerWillBeginTransaction:(_ASAsyncTransaction *)transaction;
- (void)asyncdisplaykit_asyncTransactionContainerDidCompleteTransaction:(_ASAsyncTransaction *)transaction;
Expand Down
13 changes: 11 additions & 2 deletions Source/Details/Transactions/_ASAsyncTransactionContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <AsyncDisplayKit/_ASAsyncTransactionContainer+Private.h>

#import <AsyncDisplayKit/ASConfigurationInternal.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/_ASAsyncTransaction.h>
#import <AsyncDisplayKit/_ASAsyncTransactionGroup.h>

Expand Down Expand Up @@ -49,9 +50,9 @@ - (_ASAsyncTransaction *)asyncdisplaykit_asyncTransaction
{
_ASAsyncTransaction *transaction = self.asyncdisplaykit_currentAsyncTransaction;
if (transaction == nil) {
NSHashTable *transactions = self.asyncdisplaykit_asyncLayerTransactions;
NSMutableSet<_ASAsyncTransaction *> *transactions = self.asyncdisplaykit_asyncLayerTransactions;
if (transactions == nil) {
transactions = [NSHashTable hashTableWithOptions:NSHashTableObjectPointerPersonality];
transactions = ASCreatePointerBasedMutableSet();
self.asyncdisplaykit_asyncLayerTransactions = transactions;
}
__weak CALayer *weakSelf = self;
Expand All @@ -62,6 +63,10 @@ - (_ASAsyncTransaction *)asyncdisplaykit_asyncTransaction
return;
}
[self.asyncdisplaykit_asyncLayerTransactions removeObject:completedTransaction];
if (self.asyncdisplaykit_asyncLayerTransactions.count == 0) {
// Reclaim object memory.
self.asyncdisplaykit_asyncLayerTransactions = nil;
}
[self asyncdisplaykit_asyncTransactionContainerDidCompleteTransaction:completedTransaction];
}];
} else {
Expand All @@ -71,6 +76,10 @@ - (_ASAsyncTransaction *)asyncdisplaykit_asyncTransaction
return;
}
[transactions removeObject:completedTransaction];
if (transactions.count == 0) {
// Reclaim object memory.
self.asyncdisplaykit_asyncLayerTransactions = nil;
}
[self asyncdisplaykit_asyncTransactionContainerDidCompleteTransaction:completedTransaction];
}];
}
Expand Down
5 changes: 5 additions & 0 deletions Source/Private/ASInternalHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT ASImageDownloaderPriority ASImageDown
- (NSComparisonResult)asdk_inverseCompare:(NSIndexPath *)otherIndexPath;
@end

/**
* Create an NSMutableSet that uses pointers for hash & equality.
*/
AS_EXTERN NSMutableSet *ASCreatePointerBasedMutableSet(void);

NS_ASSUME_NONNULL_END

#ifndef AS_INITIALIZE_FRAMEWORK_MANUALLY
Expand Down
12 changes: 12 additions & 0 deletions Source/Private/ASInternalHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,15 @@ - (NSComparisonResult)asdk_inverseCompare:(NSIndexPath *)otherIndexPath
}

@end

NSMutableSet *ASCreatePointerBasedMutableSet()
{
static CFSetCallBacks callbacks;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
callbacks = kCFTypeSetCallBacks;
callbacks.equal = nullptr;
callbacks.hash = nullptr;
});
return (__bridge_transfer NSMutableSet *)CFSetCreateMutable(NULL, 0, &callbacks);
}