From 915e5826efba758c42aefeeab69499ddcdd9cf50 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 10 Feb 2016 12:48:41 -0800 Subject: [PATCH] Restricted image decoding to 2 simultaneous threads Reviewed By: zjj010104 Differential Revision: D2922292 fb-gh-sync-id: eddd47d02fc721acc1da69e7483c6570997320b5 shipit-source-id: eddd47d02fc721acc1da69e7483c6570997320b5 --- Libraries/Image/RCTImageLoader.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index 0d7b06f5c1d578..22bffa4a3d2661 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -38,6 +38,7 @@ @implementation RCTImageLoader { NSArray> *_loaders; NSArray> *_decoders; + NSOperationQueue *_imageDecodeQueue; dispatch_queue_t _URLCacheQueue; NSURLCache *_URLCache; } @@ -474,7 +475,13 @@ - (RCTImageLoaderCancellationBlock)decodeImageDataWithoutClipping:(NSData *)data completionHandler:completionHandler]; } else { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + // Serialize decoding to prevent excessive memory usage + if (!_imageDecodeQueue) { + _imageDecodeQueue = [NSOperationQueue new]; + _imageDecodeQueue.name = @"com.facebook.react.ImageDecoderQueue"; + _imageDecodeQueue.maxConcurrentOperationCount = 2; + } + [_imageDecodeQueue addOperationWithBlock:^{ if (cancelled) { return; } @@ -501,7 +508,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageDataWithoutClipping:(NSData *)data NSError *finalError = RCTErrorWithMessage(errorMessage); completionHandler(finalError, nil); } - }); + }]; return ^{ OSAtomicOr32Barrier(1, &cancelled);