From 1f07e8953872b2d7fe441dea588c99993d869d8f Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 14 Dec 2016 08:27:31 -0800 Subject: [PATCH] packager: don't keep on using global cache forever if it misses Summary: Keep track of the number of cache misses. If we have a series of misses but then we start seeing hits, then we progressively reset the counter. If there are too many misses, we just give up on the global cache. Reviewed By: cpojer Differential Revision: D4326689 fbshipit-source-id: 5606fc860cbb9d7e9e877c0d1dc4e6a6a1cbcd98 --- .../react-packager/src/node-haste/Module.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packager/react-packager/src/node-haste/Module.js b/packager/react-packager/src/node-haste/Module.js index 7bf4976b3..2f914585a 100644 --- a/packager/react-packager/src/node-haste/Module.js +++ b/packager/react-packager/src/node-haste/Module.js @@ -32,6 +32,12 @@ import type Cache from './Cache'; import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; import type ModuleCache from './ModuleCache'; +/** + * If the global cache returns empty that many times, we give up using the + * global cache for that instance. This speeds up the build. + */ +const GLOBAL_CACHE_MAX_MISSES = 250; + type ReadResult = { code: string, dependencies?: ?Array, @@ -78,6 +84,7 @@ class Module { _readPromises: Map>; static _globalCacheRetries: number; + static _globalCacheMaxMisses: number; constructor({ file, @@ -261,7 +268,9 @@ class Module { callback: (error: ?Error, result: ?TransformedCode) => void, ) { const globalCache = GlobalTransformCache.get(); - if (Module._globalCacheRetries <= 0 || globalCache == null) { + const noMoreRetries = Module._globalCacheRetries <= 0; + const tooManyMisses = Module._globalCacheMaxMisses <= 0; + if (globalCache == null || noMoreRetries || tooManyMisses) { this._transformCodeForCallback(cacheProps, callback); return; } @@ -280,9 +289,19 @@ class Module { } } if (globalCachedResult == null) { + --Module._globalCacheMaxMisses; + if (Module._globalCacheMaxMisses === 0) { + terminal.log( + 'warning: global cache is now disabled because it ' + + 'has been missing too many consecutive keys.', + ); + } this._transformAndStoreCodeGlobally(cacheProps, globalCache, callback); return; } + if (Module._globalCacheMaxMisses < GLOBAL_CACHE_MAX_MISSES) { + ++Module._globalCacheMaxMisses; + } callback(undefined, globalCachedResult); }); } @@ -382,6 +401,7 @@ class Module { } Module._globalCacheRetries = 4; +Module._globalCacheMaxMisses = GLOBAL_CACHE_MAX_MISSES; // use weak map to speed up hash creation of known objects const knownHashes = new WeakMap();