From f65b88153d6402d8fa70d71dcb3aeab7f1622265 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Fri, 18 Oct 2024 08:06:17 -0500 Subject: [PATCH] Simplify exists cache, remove unnecessary normalization --- src/Data/TemplateData.js | 9 ++----- src/TemplateConfig.js | 1 - src/Util/ExistsCache.js | 57 +++------------------------------------- test/ExistsCacheTest.js | 39 +++------------------------ 4 files changed, 10 insertions(+), 96 deletions(-) diff --git a/src/Data/TemplateData.js b/src/Data/TemplateData.js index 273fbf043..76e9ac773 100644 --- a/src/Data/TemplateData.js +++ b/src/Data/TemplateData.js @@ -495,12 +495,7 @@ class TemplateData { if (extension === "js" || extension === "cjs" || extension === "mjs") { // JS data file or require’d JSON (no preprocessing needed) - let localPath = TemplatePath.absolutePath(path); - let exists = this._fsExistsCache.exists(localPath); - // Make sure that relative lookups benefit from cache - this._fsExistsCache.markExists(path, exists); - - if (!exists) { + if (!this._fsExistsCache.exists(path)) { return {}; } @@ -515,7 +510,7 @@ class TemplateData { } // We always need to use `import()`, as `require` isn’t available in ESM. - let returnValue = await EleventyImport(localPath, type); + let returnValue = await EleventyImport(path, type); // TODO special exception for Global data `permalink.js` // module.exports = (data) => `${data.page.filePathStem}/`; // Does not work diff --git a/src/TemplateConfig.js b/src/TemplateConfig.js index 93528db96..0d99c2b6a 100644 --- a/src/TemplateConfig.js +++ b/src/TemplateConfig.js @@ -542,7 +542,6 @@ class TemplateConfig { get existsCache() { if (!this._existsCache) { this._existsCache = new ExistsCache(); - this._existsCache.setDirectoryCheck(true); } return this._existsCache; } diff --git a/src/Util/ExistsCache.js b/src/Util/ExistsCache.js index 0c49e9751..b7852de5d 100644 --- a/src/Util/ExistsCache.js +++ b/src/Util/ExistsCache.js @@ -1,5 +1,4 @@ import fs from "graceful-fs"; -import PathNormalizer from "./PathNormalizer.js"; // Checks both files and directories class ExistsCache { @@ -8,47 +7,21 @@ class ExistsCache { this.lookupCount = 0; } - setDirectoryCheck(check) { - this.shouldCacheDirectories = !!check; - } - get size() { return this._cache.size; } - parentsDoNotExist(path) { - if (!this.shouldCacheDirectories) { - return false; - } - - let allPaths = PathNormalizer.getAllPaths(path).filter((entry) => entry !== path); - for (let parentPath of allPaths) { - if (this._cache.has(parentPath)) { - if (this._cache.get(parentPath) === false) { - return true; // we know this parent doesn’t exist - } - } - } - - // if you’ve made it here: we don’t know if the parents exist or not - return undefined; - } - has(path) { return this._cache.has(path); } + // Relative paths (to root directory) expected exists(path) { - path = PathNormalizer.fullNormalization(path); - - if (this.parentsDoNotExist(path)) { - // we don’t need to check if we already know the parent directories do not exist - return false; - } else if (!this._cache.has(path)) { + if (!this._cache.has(path)) { let exists = fs.existsSync(path); this.lookupCount++; - this.markExistsWithParentDirectories(path, exists); + this.markExists(path, exists); return exists; } @@ -56,29 +29,7 @@ class ExistsCache { return this._cache.get(path); } - // if a file exists, we can mark the parent directories as existing also - // if a file does not exist, we don’t know if the parent directories exist or not (yet) - markExistsWithParentDirectories(path, exists = true) { - path = PathNormalizer.fullNormalization(path); - - if (!this.shouldCacheDirectories || !exists) { - // does not exist: only mark path - this.markExists(path, false, true); - return; - } - - // exists: mark path and parents - let paths = PathNormalizer.getAllPaths(path); - for (let fullpath of paths) { - this.markExists(fullpath, true, true); - } - } - - markExists(path, exists = true, alreadyNormalized = false) { - if (!alreadyNormalized) { - path = PathNormalizer.fullNormalization(path); - } - + markExists(path, exists = true) { this._cache.set(path, !!exists); } } diff --git a/test/ExistsCacheTest.js b/test/ExistsCacheTest.js index aca00df78..47ff54ac9 100644 --- a/test/ExistsCacheTest.js +++ b/test/ExistsCacheTest.js @@ -4,8 +4,10 @@ import ExistsCache from "../src/Util/ExistsCache.js"; test("Simple check (with directory checking)", async t => { let cache = new ExistsCache(); - cache.setDirectoryCheck(true); + t.is(cache.exists("test"), true); + t.is(cache.size, 1); + t.is(cache.lookupCount, 1); t.is(cache.exists("test"), true); t.is(cache.size, 1); t.is(cache.lookupCount, 1); @@ -17,28 +19,8 @@ test("Simple check (with directory checking)", async t => { t.is(cache.lookupCount, 3); }); -test("Simple check (parent directory is valid)", async t => { - let cache = new ExistsCache(); - cache.setDirectoryCheck(true); - - t.is(cache.exists("test/stubs"), true); - t.is(cache.size, 2); - t.is(cache.lookupCount, 1); - - // we already know this parent folder exists - t.is(cache.exists("test"), true); - t.is(cache.size, 2); - t.is(cache.lookupCount, 1); - - // we don’t know if this exists - t.is(cache.exists("test/stubs/does-not-exist-ever-hslkadjflk"), false); - t.is(cache.size, 3); - t.is(cache.lookupCount, 2); -}); - test("Simple check (parent directory already invalidated)", async t => { let cache = new ExistsCache(); - cache.setDirectoryCheck(true); t.is(cache.exists("test/folder-does-not-exist-askdfjkladjs"), false); t.is(cache.size, 1); @@ -46,19 +28,6 @@ test("Simple check (parent directory already invalidated)", async t => { // we already know this *doesn’t* exist. t.is(cache.exists("test/folder-does-not-exist-askdfjkladjs/file-we-already-know-does-not-exist.liquid"), false); - t.is(cache.size, 1); - t.is(cache.lookupCount, 1); -}); - - -test("Simple check (without directory checking)", async t => { - let cache = new ExistsCache(); - // cache.setDirectoryCheck(false); - - t.is(cache.exists("test/stubs"), true); - t.is(cache.size, 1); - t.is(cache.lookupCount, 1); - t.is(cache.exists("test"), true); - t.is(cache.size, 2); + t.is(cache.size, 2); t.is(cache.lookupCount, 2); });