From 9f9e8318657a90ef7d3c0dce1a870b6777ed1d85 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Sat, 21 Jul 2018 19:39:53 -0700 Subject: [PATCH] Pre-create sub-directories --- src/FSCache.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/FSCache.js b/src/FSCache.js index db5a2f9575a..f62cbc56bbc 100644 --- a/src/FSCache.js +++ b/src/FSCache.js @@ -22,14 +22,25 @@ class FSCache { ); } - async ensureDirExists(dir = this.dir) { - await fs.mkdirp(dir); + async ensureDirExists() { + if (this.dirExists) { + return; + } + + await fs.mkdirp(this.dir); + + // Create sub-directories for every possible hex value + // This speeds up large caches on many file systems since there are fewer files in a single directory. + for (let i = 0; i < 256; i++) { + await fs.mkdirp(path.join(this.dir, i.toString(16).padStart(2, '0'))); + } + this.dirExists = true; } getCacheFile(filename) { let hash = md5(this.optionsHash + filename); - return path.join(this.dir, hash.substring(0, 2), hash + '.json'); + return path.join(this.dir, hash.slice(0, 2), hash.slice(2) + '.json'); } async getLastModified(filename) { @@ -56,8 +67,8 @@ class FSCache { async write(filename, data) { try { + await this.ensureDirExists(); let cacheFile = this.getCacheFile(filename); - await this.ensureDirExists(path.dirname(cacheFile)); await this.writeDepMtimes(data); await fs.writeFile(cacheFile, JSON.stringify(data)); this.invalidated.delete(filename);