Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Chore: refactor config hash caching in CLIEngine (#9260)
Previously, CLIEngine would store the last config object when executing on multiple files, as a performance optimization to avoid rehashing configs if the same config was used for multiple files.

This can be better implemented by using a WeakMap to map config objects to hash results.
  • Loading branch information
not-an-aardvark committed Sep 8, 2017
1 parent 1a76c4d commit 5d7eb81
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions lib/cli-engine.js
Expand Up @@ -372,6 +372,8 @@ function getCacheFile(cacheFile, cwd) {
return resolvedCacheFile;
}

const configHashCache = new WeakMap();

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -496,7 +498,6 @@ class CLIEngine {
const options = this.options,
fileCache = this._fileCache,
configHelper = this.config;
let prevConfig; // the previous configuration used
const cacheFile = getCacheFile(this.options.cacheLocation || this.options.cacheFile, this.options.cwd);

if (!options.cache && fs.existsSync(cacheFile)) {
Expand All @@ -511,25 +512,11 @@ class CLIEngine {
function hashOfConfigFor(filename) {
const config = configHelper.getConfig(filename);

if (!prevConfig) {
prevConfig = {};
}

// reuse the previously hashed config if the config hasn't changed
if (prevConfig.config !== config) {

/*
* config changed so we need to calculate the hash of the config
* and the hash of the plugins being used
*/
prevConfig.config = config;

const eslintVersion = pkg.version;

prevConfig.hash = hash(`${eslintVersion}_${stringify(config)}`);
if (!configHashCache.has(config)) {
configHashCache.set(config, hash(`${pkg.version}_${stringify(config)}`));
}

return prevConfig.hash;
return configHashCache.get(config);
}

const startTime = Date.now();
Expand Down

0 comments on commit 5d7eb81

Please sign in to comment.