Skip to content

Commit 250f7ab

Browse files
garrettmoonbernieperez
authored andcommitted
A couple performance tweaks for animated images #trivial (TextureGroup#634)
* A couple performance tweaks for animated images * @nguyenhuy's comments * Avoid calling animatedImageData twice. Thanks @maicki. * Fix call to background deallocation * Good catch by @Adlai-Holler
1 parent f3623f2 commit 250f7ab

File tree

2 files changed

+64
-46
lines changed

2 files changed

+64
-46
lines changed

Source/ASImageNode+AnimatedImage.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ - (void)_locked_setAnimatedImage:(id <ASAnimatedImageProtocol>)animatedImage
5555
}
5656

5757
id <ASAnimatedImageProtocol> previousAnimatedImage = _animatedImage;
58+
5859
_animatedImage = animatedImage;
5960

6061
if (animatedImage != nil) {
@@ -80,6 +81,11 @@ - (void)_locked_setAnimatedImage:(id <ASAnimatedImageProtocol>)animatedImage
8081
}
8182

8283
[self animatedImageSet:_animatedImage previousAnimatedImage:previousAnimatedImage];
84+
85+
// Animated image can take while to dealloc, do it off the main queue
86+
if (previousAnimatedImage != nil) {
87+
ASPerformBackgroundDeallocation(&previousAnimatedImage);
88+
}
8389
}
8490

8591
- (void)animatedImageSet:(id <ASAnimatedImageProtocol>)newAnimatedImage previousAnimatedImage:(id <ASAnimatedImageProtocol>)previousAnimatedImage

Source/ASNetworkImageNode.mm

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -713,55 +713,67 @@ - (void)_lazilyLoadImageIfNecessary
713713
} else {
714714
__weak __typeof__(self) weakSelf = self;
715715
auto finished = ^(id <ASImageContainerProtocol>imageContainer, NSError *error, id downloadIdentifier, ASNetworkImageSource imageSource) {
716-
717-
__typeof__(self) strongSelf = weakSelf;
718-
if (strongSelf == nil) {
719-
return;
720-
}
721-
722-
as_log_verbose(ASImageLoadingLog(), "Downloaded image for %@ img: %@ urls: %@", self, [imageContainer asdk_image], URLs);
723-
724-
// Grab the lock for the rest of the block
725-
ASDN::MutexLocker l(strongSelf->__instanceLock__);
726-
727-
//Getting a result back for a different download identifier, download must not have been successfully canceled
728-
if (ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) {
729-
return;
730-
}
716+
ASPerformBlockOnBackgroundThread(^{
717+
__typeof__(self) strongSelf = weakSelf;
718+
if (strongSelf == nil) {
719+
return;
720+
}
731721

732-
//No longer in preload range, no point in setting the results (they won't be cleared in exit preload range)
733-
if (ASInterfaceStateIncludesPreload(self->_interfaceState) == NO) {
734-
return;
735-
}
736-
737-
if (imageContainer != nil) {
738-
[strongSelf _locked_setCurrentImageQuality:1.0];
739-
if ([imageContainer asdk_animatedImageData] && strongSelf->_downloaderFlags.downloaderImplementsAnimatedImage) {
740-
id animatedImage = [strongSelf->_downloader animatedImageWithData:[imageContainer asdk_animatedImageData]];
741-
[strongSelf _locked_setAnimatedImage:animatedImage];
742-
} else {
743-
[strongSelf _locked__setImage:[imageContainer asdk_image]];
722+
as_log_verbose(ASImageLoadingLog(), "Downloaded image for %@ img: %@ urls: %@", self, [imageContainer asdk_image], URLs);
723+
724+
// Grab the lock for the rest of the block
725+
ASDN::MutexLocker l(strongSelf->__instanceLock__);
726+
727+
//Getting a result back for a different download identifier, download must not have been successfully canceled
728+
if (ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) {
729+
return;
744730
}
745-
strongSelf->_imageLoaded = YES;
746-
}
747-
748-
strongSelf->_downloadIdentifier = nil;
749-
strongSelf->_cacheUUID = nil;
750-
751-
if (imageContainer != nil) {
752-
if (strongSelf->_delegateFlags.delegateDidLoadImageWithInfo) {
753-
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
754-
ASNetworkImageNodeDidLoadInfo info = {};
755-
info.imageSource = imageSource;
756-
[delegate imageNode:strongSelf didLoadImage:strongSelf.image info:info];
757-
} else if (strongSelf->_delegateFlags.delegateDidLoadImage) {
758-
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
759-
[delegate imageNode:strongSelf didLoadImage:strongSelf.image];
731+
732+
//No longer in preload range, no point in setting the results (they won't be cleared in exit preload range)
733+
if (ASInterfaceStateIncludesPreload(self->_interfaceState) == NO) {
734+
return;
760735
}
761-
} else if (error && strongSelf->_delegateFlags.delegateDidFailWithError) {
762-
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
763-
[delegate imageNode:strongSelf didFailWithError:error];
764-
}
736+
737+
if (imageContainer != nil) {
738+
[strongSelf _locked_setCurrentImageQuality:1.0];
739+
NSData *animatedImageData = [imageContainer asdk_animatedImageData];
740+
if (animatedImageData && strongSelf->_downloaderFlags.downloaderImplementsAnimatedImage) {
741+
id animatedImage = [strongSelf->_downloader animatedImageWithData:animatedImageData];
742+
[strongSelf _locked_setAnimatedImage:animatedImage];
743+
} else {
744+
[strongSelf _locked__setImage:[imageContainer asdk_image]];
745+
}
746+
strongSelf->_imageLoaded = YES;
747+
}
748+
749+
strongSelf->_downloadIdentifier = nil;
750+
strongSelf->_cacheUUID = nil;
751+
752+
ASPerformBlockOnMainThread(^{
753+
__typeof__(self) strongSelf = weakSelf;
754+
if (strongSelf == nil) {
755+
return;
756+
}
757+
758+
// Grab the lock for the rest of the block
759+
ASDN::MutexLocker l(strongSelf->__instanceLock__);
760+
761+
if (imageContainer != nil) {
762+
if (strongSelf->_delegateFlags.delegateDidLoadImageWithInfo) {
763+
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
764+
ASNetworkImageNodeDidLoadInfo info = {};
765+
info.imageSource = imageSource;
766+
[delegate imageNode:strongSelf didLoadImage:strongSelf.image info:info];
767+
} else if (strongSelf->_delegateFlags.delegateDidLoadImage) {
768+
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
769+
[delegate imageNode:strongSelf didLoadImage:strongSelf.image];
770+
}
771+
} else if (error && strongSelf->_delegateFlags.delegateDidFailWithError) {
772+
ASDN::MutexUnlocker u(strongSelf->__instanceLock__);
773+
[delegate imageNode:strongSelf didFailWithError:error];
774+
}
775+
});
776+
});
765777
};
766778

767779
// As the _cache and _downloader is only set once in the intializer we don't have to use a

0 commit comments

Comments
 (0)