Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: add cache of request and filename to optimize require perf #21404

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions benchmark/module/module-filename-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const fs = require('fs');
const path = require('path');
const common = require('../common.js');

const tmpdir = require('../../test/common/tmpdir');
const benchmarkDirectory = path.join(
tmpdir.path,
'nodejs-benchmark-module-filename-cache'
);

const bench = common.createBenchmark(main, {
requireCount: [1e4, 1e5, 1e6, 1e7]
});

function main({ requireCount }) {
tmpdir.refresh();
try { fs.mkdirSync(benchmarkDirectory); } catch {}

fs.mkdirSync(`${benchmarkDirectory}${requireCount}`);
const filename = `${benchmarkDirectory}${requireCount}/index.js`;
fs.writeFileSync(
filename,
'module.exports = "";'
);

bench.start();
for (let i = 0; i <= requireCount; i++) {
require(filename);
}
bench.end(requireCount);

tmpdir.refresh();
}
19 changes: 18 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ function Module(id, parent) {
this.filename = null;
this.loaded = false;
this.children = [];
// Cache for children's requests and filenames
this.resolvedFilenameCache = {};
}

const builtinModules = [];
Expand Down Expand Up @@ -619,6 +621,17 @@ Module._resolveFilename = function(request, parent, isMain, options) {
return request;
}

var filename;
// Don't use cache when:
// 1. require.resolve('you/request', {paths: []})
if (!(options && options.paths)) {
if (parent) {
filename = parent.resolvedFilenameCache &&
parent.resolvedFilenameCache[request];
if (filename) return filename;
}
}

Copy link
Member

@jdalton jdalton Feb 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of caching per parent would it be more beneficial to cache from the path.dirname(parent.filename). Something like:

cacheKey =
  request + "\0" +
  fromPath + "\0" +
  (isMain ? "1" : "")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that using a global cache, which uses request + parentDirName + isMain as keys?
But this will not be deleted when people use delete require.cache[filename]. I'm a little concerned about that a global cache may cause some side effects when deleting cache.

var paths;

if (typeof options === 'object' && options !== null &&
Expand All @@ -642,7 +655,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
}

// Look up the filename first, since that's the cache key.
var filename = Module._findPath(request, paths, isMain);
filename = Module._findPath(request, paths, isMain);
if (!filename) {
const requireStack = [];
for (var cursor = parent;
Expand All @@ -660,6 +673,10 @@ Module._resolveFilename = function(request, parent, isMain, options) {
err.requireStack = requireStack;
throw err;
}
if (parent) {
parent.resolvedFilenameCache = parent.resolvedFilenameCache || {};
parent.resolvedFilenameCache[request] = filename;
}
return filename;
};

Expand Down