packager: TransformCache: trivial optimization wins
Reviewed By: davidaurelio Differential Revision: D5068811 fbshipit-source-id: 74e4c3218581b2cb081cb7da7639356d0a7d0430
This commit is contained in:
parent
7eb876e269
commit
40a53918ec
|
@ -138,31 +138,37 @@ class TransformCache {
|
||||||
*/
|
*/
|
||||||
_readSync(props: ReadTransformProps): TransformCacheResult {
|
_readSync(props: ReadTransformProps): TransformCacheResult {
|
||||||
this._collectIfNecessarySync(props.cacheOptions);
|
this._collectIfNecessarySync(props.cacheOptions);
|
||||||
const cacheFilePaths = this._getCacheFilePaths(props);
|
|
||||||
let metadata, transformedCode;
|
|
||||||
try {
|
try {
|
||||||
metadata = readMetadataFileSync(cacheFilePaths.metadata);
|
return this._readFilesSync(props);
|
||||||
if (metadata == null) {
|
|
||||||
return {result: null, outdatedDependencies: EMPTY_ARRAY};
|
|
||||||
}
|
|
||||||
const sourceHash = hashSourceCode(props);
|
|
||||||
if (sourceHash !== metadata.cachedSourceHash) {
|
|
||||||
return {result: null, outdatedDependencies: metadata.dependencies};
|
|
||||||
}
|
|
||||||
transformedCode = fs.readFileSync(cacheFilePaths.transformedCode, 'utf8');
|
|
||||||
const codeHash = crypto
|
|
||||||
.createHash('sha1')
|
|
||||||
.update(transformedCode)
|
|
||||||
.digest('hex');
|
|
||||||
if (metadata.cachedResultHash !== codeHash) {
|
|
||||||
return {result: null, outdatedDependencies: metadata.dependencies};
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === 'ENOENT') {
|
if (error.code === 'ENOENT') {
|
||||||
return {result: null, outdatedDependencies: EMPTY_ARRAY};
|
return {result: null, outdatedDependencies: EMPTY_ARRAY};
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_readFilesSync(props: ReadTransformProps): TransformCacheResult {
|
||||||
|
const cacheFilePaths = this._getCacheFilePaths(props);
|
||||||
|
const metadata = readMetadataFileSync(cacheFilePaths.metadata);
|
||||||
|
if (metadata == null) {
|
||||||
|
return {result: null, outdatedDependencies: EMPTY_ARRAY};
|
||||||
|
}
|
||||||
|
const sourceHash = hashSourceCode(props);
|
||||||
|
if (sourceHash !== metadata.cachedSourceHash) {
|
||||||
|
return {result: null, outdatedDependencies: metadata.dependencies};
|
||||||
|
}
|
||||||
|
const transformedCode = fs.readFileSync(
|
||||||
|
cacheFilePaths.transformedCode,
|
||||||
|
'utf8',
|
||||||
|
);
|
||||||
|
const codeHash = crypto
|
||||||
|
.createHash('sha1')
|
||||||
|
.update(transformedCode)
|
||||||
|
.digest('hex');
|
||||||
|
if (metadata.cachedResultHash !== codeHash) {
|
||||||
|
return {result: null, outdatedDependencies: metadata.dependencies};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
result: {
|
result: {
|
||||||
code: transformedCode,
|
code: transformedCode,
|
||||||
|
@ -336,15 +342,7 @@ function readMetadataFileSync(
|
||||||
sourceMap: ?MappingsMap,
|
sourceMap: ?MappingsMap,
|
||||||
} {
|
} {
|
||||||
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
|
const metadataStr = fs.readFileSync(metadataFilePath, 'utf8');
|
||||||
let metadata;
|
const metadata = tryParseJSON(metadataStr);
|
||||||
try {
|
|
||||||
metadata = JSON.parse(metadataStr);
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof SyntaxError) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
if (!Array.isArray(metadata)) {
|
if (!Array.isArray(metadata)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -375,6 +373,17 @@ function readMetadataFileSync(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tryParseJSON(str: string): any {
|
||||||
|
try {
|
||||||
|
return JSON.parse(str);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof SyntaxError) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function hashSourceCode(props: {
|
function hashSourceCode(props: {
|
||||||
filePath: string,
|
filePath: string,
|
||||||
sourceCode: string,
|
sourceCode: string,
|
||||||
|
|
Loading…
Reference in New Issue