From 4bd7dc003520425b64a4922bbb16786b68c6e858 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Mon, 3 Apr 2017 04:40:27 -0700 Subject: [PATCH] packager: TransformCache: do not rehash transformOptions every single time Reviewed By: davidaurelio Differential Revision: D4809963 fbshipit-source-id: 9d3ac907e822596567ee7af4427bcd24f1bac73f --- packager/src/lib/TransformCache.js | 8 +++++--- packager/src/lib/__tests__/TransformCache-test.js | 8 +++++++- packager/src/node-haste/Module.js | 14 +++++++++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packager/src/lib/TransformCache.js b/packager/src/lib/TransformCache.js index 3dabddfd668..ffcfb5c4cd0 100644 --- a/packager/src/lib/TransformCache.js +++ b/packager/src/lib/TransformCache.js @@ -19,7 +19,6 @@ const fs = require('fs'); * this particular use case. */ const imurmurhash = require('imurmurhash'); -const jsonStableStringify = require('json-stable-stringify'); const mkdirp = require('mkdirp'); const path = require('path'); const rimraf = require('rimraf'); @@ -64,6 +63,7 @@ function hashSourceCode(props: { sourceCode: string, getTransformCacheKey: GetTransformCacheKey, transformOptions: TransformOptions, + transformOptionsKey: string, }): string { return imurmurhash(props.getTransformCacheKey( props.sourceCode, @@ -79,11 +79,11 @@ function hashSourceCode(props: { */ function getCacheFilePaths(props: { filePath: string, - transformOptions: TransformOptions, + transformOptionsKey: string, }): CacheFilePaths { const hasher = imurmurhash() .hash(props.filePath) - .hash(jsonStableStringify(props.transformOptions) || ''); + .hash(props.transformOptionsKey); const hash = toFixedHex(8, hasher.result()); const prefix = hash.substr(0, 2); const fileName = `${hash.substr(2)}${path.basename(props.filePath)}`; @@ -135,6 +135,7 @@ function writeSync(props: { sourceCode: string, getTransformCacheKey: GetTransformCacheKey, transformOptions: TransformOptions, + transformOptionsKey: string, result: CachedResult, }): void { const cacheFilePath = getCacheFilePaths(props); @@ -296,6 +297,7 @@ export type ReadTransformProps = { filePath: string, sourceCode: string, transformOptions: TransformOptions, + transformOptionsKey: string, getTransformCacheKey: GetTransformCacheKey, cacheOptions: CacheOptions, }; diff --git a/packager/src/lib/__tests__/TransformCache-test.js b/packager/src/lib/__tests__/TransformCache-test.js index 9de6c9477f7..c20923429ae 100644 --- a/packager/src/lib/__tests__/TransformCache-test.js +++ b/packager/src/lib/__tests__/TransformCache-test.js @@ -15,9 +15,12 @@ jest .dontMock('../TransformCache') .dontMock('../toFixedHex') .dontMock('left-pad') - .dontMock('lodash/throttle'); + .dontMock('lodash/throttle') + .dontMock('crypto'); const imurmurhash = require('imurmurhash'); +const crypto = require('crypto'); +const jsonStableStringify = require('json-stable-stringify'); const memoryFS = new Map(); @@ -66,6 +69,8 @@ describe('TransformCache', () => { getTransformCacheKey: () => 'abcdef', filePath, transformOptions, + transformOptionsKey: crypto.createHash('md5') + .update(jsonStableStringify(transformOptions)).digest('hex'), result: { code: `/* result for ${key} */`, dependencies: ['foo', `dep of ${key}`], @@ -100,6 +105,7 @@ describe('TransformCache', () => { getTransformCacheKey: () => transformCacheKey, filePath: 'test.js', transformOptions: {foo: 1}, + transformOptionsKey: 'boo!', result: { code: `/* result for ${key} */`, dependencies: ['foo', `dep of ${key}`], diff --git a/packager/src/node-haste/Module.js b/packager/src/node-haste/Module.js index fa45c76251b..8fb916a8cac 100644 --- a/packager/src/node-haste/Module.js +++ b/packager/src/node-haste/Module.js @@ -360,7 +360,7 @@ class Module { if (this._readResultsByOptionsKey.has(key)) { return this._readResultsByOptionsKey.get(key); } - const result = this._readFromTransformCache(transformOptions); + const result = this._readFromTransformCache(transformOptions, key); this._readResultsByOptionsKey.set(key, result); return result; } @@ -369,8 +369,11 @@ class Module { * Read again from the TransformCache, on disk. `readCached` should be favored * so it's faster in case the results are already in memory. */ - _readFromTransformCache(transformOptions: TransformOptions): ?ReadResult { - const cacheProps = this._getCacheProps(transformOptions); + _readFromTransformCache( + transformOptions: TransformOptions, + transformOptionsKey: string, + ): ?ReadResult { + const cacheProps = this._getCacheProps(transformOptions, transformOptionsKey); const cachedResult = TransformCache.readSync(cacheProps); if (cachedResult) { return this._finalizeReadResult(cacheProps.sourceCode, cachedResult); @@ -391,7 +394,7 @@ class Module { return promise; } const freshPromise = Promise.resolve().then(() => { - const cacheProps = this._getCacheProps(transformOptions); + const cacheProps = this._getCacheProps(transformOptions, key); return new Promise((resolve, reject) => { this._getAndCacheTransformedCode( cacheProps, @@ -413,7 +416,7 @@ class Module { return freshPromise; } - _getCacheProps(transformOptions: TransformOptions) { + _getCacheProps(transformOptions: TransformOptions, transformOptionsKey: string) { const sourceCode = this._readSourceCode(); const moduleDocBlock = this._readDocBlock(); const getTransformCacheKey = this._getTransformCacheKey; @@ -428,6 +431,7 @@ class Module { sourceCode, getTransformCacheKey, transformOptions, + transformOptionsKey, cacheOptions: { resetCache: this._options.resetCache, reporter: this._reporter,