Skip to content

Commit 5c39687

Browse files
BridgeARBethGriggs
authored andcommitted
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: #26970 Refs: #25362 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent 9b27d5e commit 5c39687

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/internal/modules/cjs/loader.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const {
7777

7878
const isWindows = process.platform === 'win32';
7979

80+
const relativeResolveCache = Object.create(null);
81+
8082
let requireDepth = 0;
8183
let statCache = null;
8284
function stat(filename) {
@@ -568,14 +570,28 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
568570
// Then have it load the file contents before returning its exports
569571
// object.
570572
Module._load = function(request, parent, isMain) {
573+
let relResolveCacheIdentifier;
571574
if (parent) {
572575
debug('Module._load REQUEST %s parent: %s', request, parent.id);
576+
// Fast path for (lazy loaded) modules in the same directory. The indirect
577+
// caching is required to allow cache invalidation without changing the old
578+
// cache key names.
579+
relResolveCacheIdentifier = `${parent.path}\x00${request}`;
580+
const filename = relativeResolveCache[relResolveCacheIdentifier];
581+
if (filename !== undefined) {
582+
const cachedModule = Module._cache[filename];
583+
if (cachedModule !== undefined) {
584+
updateChildren(parent, cachedModule, true);
585+
return cachedModule.exports;
586+
}
587+
delete relativeResolveCache[relResolveCacheIdentifier];
588+
}
573589
}
574590

575591
const filename = Module._resolveFilename(request, parent, isMain);
576592

577593
const cachedModule = Module._cache[filename];
578-
if (cachedModule) {
594+
if (cachedModule !== undefined) {
579595
updateChildren(parent, cachedModule, true);
580596
return cachedModule.exports;
581597
}
@@ -595,6 +611,9 @@ Module._load = function(request, parent, isMain) {
595611
}
596612

597613
Module._cache[filename] = module;
614+
if (parent !== undefined) {
615+
relativeResolveCache[relResolveCacheIdentifier] = filename;
616+
}
598617

599618
let threw = true;
600619
try {
@@ -603,6 +622,9 @@ Module._load = function(request, parent, isMain) {
603622
} finally {
604623
if (threw) {
605624
delete Module._cache[filename];
625+
if (parent !== undefined) {
626+
delete relativeResolveCache[relResolveCacheIdentifier];
627+
}
606628
}
607629
}
608630

0 commit comments

Comments
 (0)