From 5fa0bdf9d8108131966551211c226980ab29390f Mon Sep 17 00:00:00 2001 From: Siarhei Fedarovich Date: Fri, 18 Aug 2023 14:27:46 -0700 Subject: [PATCH] Reduce memory footprint of storing fs erros in cache --- lib/CachedInputFileSystem.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/CachedInputFileSystem.js b/lib/CachedInputFileSystem.js index 022e72c0..40e3f459 100644 --- a/lib/CachedInputFileSystem.js +++ b/lib/CachedInputFileSystem.js @@ -368,6 +368,19 @@ class CacheBackend { */ _storeResult(path, err, result) { if (this._data.has(path)) return; + + if (err) { + // Clone error object to be stored in cache to allow for the original + // error, and its internal representation to be picked up by GC. Thus + // reducing the memory footprint of caching such results in memory. + // Also, omitting expensive calculation of the stacktrace as it gets + // ignored most of the time anyway + const lightErrorProps = Object.getOwnPropertyDescriptors(err); + delete lightErrorProps.stack; + + err = Object.create(Object.getPrototypeOf(err), lightErrorProps); + } + const level = this._levels[this._currentLevel]; this._data.set(path, { err, result, level }); level.add(path);