Skip to content

Commit

Permalink
Reduce _ASAsyncTransaction allocations by ensuring nodes without disp…
Browse files Browse the repository at this point in the history
…lay methods don't create them.
  • Loading branch information
Scott Goodson committed Sep 25, 2015
1 parent d76c199 commit 47be240
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
37 changes: 23 additions & 14 deletions AsyncDisplayKit/Private/ASDisplayNode+AsyncDisplay.mm
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,14 @@ - (void)displayAsyncLayer:(_ASDisplayLayer *)asyncLayer asynchronously:(BOOL)asy
return BOOL(displaySentinelValue != displaySentinel.value);
};

// If we're participating in an ancestor's asyncTransaction, find it here
ASDisplayNodeAssert(_layer, @"Expect _layer to be not nil");
CALayer *containerLayer = _layer.asyncdisplaykit_parentTransactionContainer ?: _layer;
_ASAsyncTransaction *transaction = containerLayer.asyncdisplaykit_asyncTransaction;

// Set up displayBlock to call either display or draw on the delegate and return a UIImage contents
asyncdisplaykit_async_transaction_operation_block_t displayBlock = [self _displayBlockWithAsynchronous:asynchronously isCancelledBlock:isCancelledBlock rasterizing:NO];

if (!displayBlock) {
return;
}

ASDisplayNodeAssert(_layer, @"Expect _layer to be not nil");

// This block is called back on the main thread after rendering at the completion of the current async transaction, or immediately if !asynchronously
asyncdisplaykit_async_transaction_operation_completion_block_t completionBlock = ^(id<NSObject> value, BOOL canceled){
Expand All @@ -335,16 +333,27 @@ - (void)displayAsyncLayer:(_ASDisplayLayer *)asyncLayer asynchronously:(BOOL)asy
}
};

if (displayBlock != NULL) {
// Call willDisplay immediately in either case
[self willDisplayAsyncLayer:self.asyncLayer];
// Call willDisplay immediately in either case
[self willDisplayAsyncLayer:self.asyncLayer];

if (asynchronously) {
[transaction addOperationWithBlock:displayBlock queue:[_ASDisplayLayer displayQueue] completion:completionBlock];
} else {
UIImage *contents = (UIImage *)displayBlock();
completionBlock(contents, NO);
}
if (asynchronously) {
// Async rendering operations are contained by a transaction, which allows them to proceed and concurrently
// while synchronizing the final application of the results to the layer's contents property (completionBlock).

// First, look to see if we are expected to join a parent's transaction container.
CALayer *containerLayer = _layer.asyncdisplaykit_parentTransactionContainer ?: _layer;

// In the case that a transaction does not yet exist (such as for an individual node outside of a container),
// this call will allocate the transaction and add it to _ASAsyncTransactionGroup.
// It will automatically commit the transaction at the end of the runloop.
_ASAsyncTransaction *transaction = containerLayer.asyncdisplaykit_asyncTransaction;

// Adding this displayBlock operation to the transaction will start it IMMEDIATELY.
// The only function of the transaction commit is to gate the calling of the completionBlock.
[transaction addOperationWithBlock:displayBlock queue:[_ASDisplayLayer displayQueue] completion:completionBlock];
} else {
UIImage *contents = (UIImage *)displayBlock();
completionBlock(contents, NO);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ SPEC CHECKSUMS:
FBSnapshotTestCase: 3dc3899168747a0319c5278f5b3445c13a6532dd
OCMock: a6a7dc0e3997fb9f35d99f72528698ebf60d64f2

COCOAPODS: 0.37.2
COCOAPODS: 0.38.2
4 changes: 2 additions & 2 deletions examples/Kittens/Sample/KittenNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ @interface KittenNode ()

@implementation KittenNode

// lorem ipsum text courtesy http://kittyipsum.com/ <3
// lorem ipsum text courtesy https://kittyipsum.com/ <3
+ (NSArray *)placeholders
{
static NSArray *placeholders = nil;
Expand Down Expand Up @@ -82,7 +82,7 @@ - (instancetype)initWithKittenOfSize:(CGSize)size
// kitten image, with a solid background colour serving as placeholder
_imageNode = [[ASNetworkImageNode alloc] init];
_imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor();
_imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://placekitten.com/%zd/%zd",
_imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://placekitten.com/%zd/%zd",
(NSInteger)roundl(_kittenSize.width),
(NSInteger)roundl(_kittenSize.height)]];
// _imageNode.contentMode = UIViewContentModeCenter;
Expand Down

0 comments on commit 47be240

Please sign in to comment.