From 65120d70527fc5c4ec958dae5a61ea5004417291 Mon Sep 17 00:00:00 2001 From: Adam Ernst Date: Thu, 7 Jul 2016 13:31:21 -0700 Subject: [PATCH] Avoid dispatching to the global queue to read the 4 byte RAM bundle magic number Summary: Reading four bytes is not slow. Don't bother dispatching for that. Reviewed By: javache Differential Revision: D3518405 fbshipit-source-id: 910079cec2a1f624dd71760438765bd035055229 --- React/Base/RCTJavaScriptLoader.m | 64 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/React/Base/RCTJavaScriptLoader.m b/React/Base/RCTJavaScriptLoader.m index c4341844f95..fbc5cc1c9a0 100755 --- a/React/Base/RCTJavaScriptLoader.m +++ b/React/Base/RCTJavaScriptLoader.m @@ -41,47 +41,47 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init) // Load local script file if (scriptURL.fileURL) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - // Load the first 4 bytes to check if the bundle is regular or RAM ("Random Access Modules" bundle). - // The RAM bundle has a magic number in the 4 first bytes `(0xFB0BD1E5)`. - // The benefit of RAM bundle over a regular bundle is that we can lazily inject - // modules into JSC as they're required. - FILE *bundle = fopen(scriptURL.path.UTF8String, "r"); - if (!bundle) { - onComplete(RCTErrorWithMessage([NSString stringWithFormat:@"Error opening bundle %@", scriptURL.path]), nil, 0); - return; - } + // Load the first 4 bytes to check if the bundle is regular or RAM ("Random Access Modules" bundle). + // The RAM bundle has a magic number in the 4 first bytes `(0xFB0BD1E5)`. + // The benefit of RAM bundle over a regular bundle is that we can lazily inject + // modules into JSC as they're required. + FILE *bundle = fopen(scriptURL.path.UTF8String, "r"); + if (!bundle) { + onComplete(RCTErrorWithMessage([NSString stringWithFormat:@"Error opening bundle %@", scriptURL.path]), nil, 0); + return; + } - uint32_t magicNumber; - size_t readResult = fread(&magicNumber, sizeof(magicNumber), 1, bundle); - fclose(bundle); - if (readResult != 1) { - onComplete(RCTErrorWithMessage(@"Error reading bundle"), nil, 0); - return; - } + uint32_t magicNumber; + size_t readResult = fread(&magicNumber, sizeof(magicNumber), 1, bundle); + fclose(bundle); + if (readResult != 1) { + onComplete(RCTErrorWithMessage(@"Error reading bundle"), nil, 0); + return; + } - magicNumber = NSSwapLittleIntToHost(magicNumber); + magicNumber = NSSwapLittleIntToHost(magicNumber); + if (magicNumber == RCTRAMBundleMagicNumber) { + NSData *source = [NSData dataWithBytes:&magicNumber length:sizeof(magicNumber)]; NSError *error = nil; - NSData *source = nil; int64_t sourceLength = 0; - if (magicNumber == RCTRAMBundleMagicNumber) { - source = [NSData dataWithBytes:&magicNumber length:sizeof(magicNumber)]; - struct stat statInfo; - if (stat(scriptURL.path.UTF8String, &statInfo) != 0) { - error = RCTErrorWithMessage(@"Error reading bundle"); - } else { - sourceLength = statInfo.st_size; - } + struct stat statInfo; + if (stat(scriptURL.path.UTF8String, &statInfo) != 0) { + error = RCTErrorWithMessage(@"Error reading bundle"); } else { - source = [NSData dataWithContentsOfFile:scriptURL.path - options:NSDataReadingMappedIfSafe - error:&error]; - sourceLength = source.length; + sourceLength = statInfo.st_size; } - onComplete(error, source, sourceLength); + } + + // Reading in a large bundle can be slow. Dispatch to the background queue to do it. + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + NSError *error = nil; + NSData *source = [NSData dataWithContentsOfFile:scriptURL.path + options:NSDataReadingMappedIfSafe + error:&error]; + onComplete(error, source, source.length); }); return; }