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
This commit is contained in:
Adam Ernst 2016-07-07 13:31:21 -07:00 коммит произвёл Facebook Github Bot 2
Родитель 77752a0399
Коммит 65120d7052
1 изменённых файлов: 32 добавлений и 32 удалений

Просмотреть файл

@ -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;
}