From 5917f4d49b8eda431208619abb20fadd371f2897 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 28 Mar 2019 23:37:07 +0100 Subject: [PATCH] module: add extra caching layer This adds an extra modules caching layer that operates on the parent's `path` property and the current require argument. That together can be used as unique identifier to speed up loading the same module more than once. It is a cache on top of the current modules cache. It has the nice feature that this cache does not only work in the same file but it works for the whole current directory. So if the same file is loaded in any other file from the same directory, it will also hit this cache instead of having to resolve the file again. To keep it backwards compatible with the old modules cache, it detects invalidation of that cache. PR-URL: https://github.com/nodejs/node/pull/26970 Refs: https://github.com/nodejs/node/pull/25362 Reviewed-By: Guy Bedford Reviewed-By: Matteo Collina Signed-off-by: Beth Griggs --- lib/internal/modules/cjs/loader.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index b8eed4b808a965..de97f03dd47abe 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -77,6 +77,8 @@ const { const isWindows = process.platform === 'win32'; +const relativeResolveCache = Object.create(null); + let requireDepth = 0; let statCache = null; function stat(filename) { @@ -568,14 +570,28 @@ Module._resolveLookupPaths = function(request, parent, newReturn) { // Then have it load the file contents before returning its exports // object. Module._load = function(request, parent, isMain) { + let relResolveCacheIdentifier; if (parent) { debug('Module._load REQUEST %s parent: %s', request, parent.id); + // Fast path for (lazy loaded) modules in the same directory. The indirect + // caching is required to allow cache invalidation without changing the old + // cache key names. + relResolveCacheIdentifier = `${parent.path}\x00${request}`; + const filename = relativeResolveCache[relResolveCacheIdentifier]; + if (filename !== undefined) { + const cachedModule = Module._cache[filename]; + if (cachedModule !== undefined) { + updateChildren(parent, cachedModule, true); + return cachedModule.exports; + } + delete relativeResolveCache[relResolveCacheIdentifier]; + } } const filename = Module._resolveFilename(request, parent, isMain); const cachedModule = Module._cache[filename]; - if (cachedModule) { + if (cachedModule !== undefined) { updateChildren(parent, cachedModule, true); return cachedModule.exports; } @@ -595,6 +611,9 @@ Module._load = function(request, parent, isMain) { } Module._cache[filename] = module; + if (parent !== undefined) { + relativeResolveCache[relResolveCacheIdentifier] = filename; + } let threw = true; try { @@ -603,6 +622,9 @@ Module._load = function(request, parent, isMain) { } finally { if (threw) { delete Module._cache[filename]; + if (parent !== undefined) { + delete relativeResolveCache[relResolveCacheIdentifier]; + } } }