From 11cc2265516911c6cd2912254d0b0d9d2d42a398 Mon Sep 17 00:00:00 2001 From: Philipp Uhl Date: Fri, 27 Jan 2023 13:04:31 +0100 Subject: [PATCH 1/2] Fixes slow rehashing on image instance Fixes #171 --- img.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/img.js b/img.js index 30ac266..5947f2b 100644 --- a/img.js +++ b/img.js @@ -292,6 +292,10 @@ class Image { } getHash() { + if (this.computedHash) { + return this.computedHash; + } + let hash = createHash("sha256"); if(fs.existsSync(this.src)) { @@ -342,7 +346,9 @@ class Image { // replace with hash.digest('base64url') let base64hash = hash.digest('base64').replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); - return base64hash.slice(0, this.options.hashLength); + const resultHash = base64hash.substring(0, this.options.hashLength); + this.computedHash = resultHash; + return resultHash; } getStat(outputFormat, width, height) { From 4db05da567d3509f434020a84a00ca0503e4a128 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Mon, 30 Jan 2023 16:07:35 -0600 Subject: [PATCH 2/2] A bit more useful debug logging here to analyze this one! --- img.js | 12 ++++++++++-- memory-cache.js | 10 ++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/img.js b/img.js index 5947f2b..c0ec9b9 100644 --- a/img.js +++ b/img.js @@ -172,6 +172,7 @@ class Image { // TODO @zachleat add a smarter cache here (not too aggressive! must handle input file changes) if(!this._contents) { + debug("Reading from file system: %o", this.src); this._contents = fs.readFileSync(this.src); } @@ -293,6 +294,7 @@ class Image { getHash() { if (this.computedHash) { + debug("Re-using computed hash for %o: %o", this.src, this.computedHash); return this.computedHash; } @@ -347,7 +349,9 @@ class Image { let base64hash = hash.digest('base64').replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); const resultHash = base64hash.substring(0, this.options.hashLength); + this.computedHash = resultHash; + return resultHash; } @@ -530,7 +534,10 @@ class Image { })); } } - debug( "Wrote %o", stat.outputPath ); + + if(stat.outputPath) { + debug( "Wrote %o", stat.outputPath ); + } } } @@ -620,11 +627,12 @@ function queueImage(src, opts) { key = img.getInMemoryCacheKey(); let cached = memCache.get(key); if(cached) { - debug("Found cached, returning %o", cached); return cached; } } + debug("In-memory cache miss for %o, options: %o", src, opts); + let promise = processingQueue.add(async () => { if(typeof src === "string" && opts && opts.statsOnly) { if(Util.isRemoteUrl(src)) { diff --git a/memory-cache.js b/memory-cache.js index c237afe..f88e50e 100644 --- a/memory-cache.js +++ b/memory-cache.js @@ -3,23 +3,21 @@ const debug = require("debug")("EleventyImg"); class MemoryCache { constructor() { this.cache = {}; - debug("New cache."); } add(key, results) { - debug("Before add cache size %o", Object.keys(this.cache).length); - debug("Added %o to cache: %o", key, results); - this.cache[key] = { results }; + + debug("Added %o to cache (size: %o)", key, Object.keys(this.cache).length); } get(key) { if(this.cache[key]) { // may return promise - debug("Cache size %o", Object.keys(this.cache).length); - debug("Found cached for %o %o", key, this.cache[key].results); + // debug("Cache size %o", Object.keys(this.cache).length); + // debug("Found cached for %o", key); return this.cache[key].results; }