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: optimize platform lookup and initPath #3582

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 13 additions & 18 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path');
const internalModuleReadFile = process.binding('fs').internalModuleReadFile;
const internalModuleStat = process.binding('fs').internalModuleStat;

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

// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
Expand Down Expand Up @@ -191,7 +192,7 @@ Module._nodeModulePaths = function(from) {
// note: this approach *only* works when the path is guaranteed
// to be absolute. Doing a fully-edge-case-correct path.split
// that works on both Windows and Posix is non-trivial.
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
var splitRe = isWindows ? /[\/\\]/ : /\//;
var paths = [];
var parts = from.split(splitRe);

Expand Down Expand Up @@ -460,29 +461,23 @@ Module.runMain = function() {
};

Module._initPaths = function() {
const isWindows = process.platform === 'win32';
modulePaths = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. modulePaths is global to this module, and calling _initPaths, resets its value.

modulePaths = paths;

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd use modulePaths.length = 0.
That doesn't create a new object which has to be garbage collected

Copy link
Member

Choose a reason for hiding this comment

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

@meinklaus _initPaths is called only once and modulePaths is undefined at the beginning.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at https://github.com/zertosh/node/blob/module-improv/lib/module.js#L41 I don't think it is undefined.
If _initPaths is only called once there is no need to assign modulePaths again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

arr.length = 0 is magical and AFIAK it's not a convention that's used here


if (isWindows) {
var homeDir = process.env.USERPROFILE;
} else {
var homeDir = process.env.HOME;
var nodePath = process.env['NODE_PATH'];
if (nodePath) {
var nodePaths = nodePath.split(path.delimiter);
for (var i = 0; i < nodePaths.length; i++) {
if (nodePaths[i]) modulePaths.push(nodePaths[i]);
}
}

var paths = [path.resolve(process.execPath, '..', '..', 'lib', 'node')];

var homeDir = isWindows ? process.env.USERPROFILE : process.env.HOME;
if (homeDir) {
paths.unshift(path.resolve(homeDir, '.node_libraries'));
paths.unshift(path.resolve(homeDir, '.node_modules'));
}

var nodePath = process.env['NODE_PATH'];
if (nodePath) {
paths = nodePath.split(path.delimiter).filter(function(path) {
return !!path;
}).concat(paths);
modulePaths.push(path.resolve(homeDir, '.node_modules'));
modulePaths.push(path.resolve(homeDir, '.node_libraries'));
}

modulePaths = paths;
modulePaths.push(path.resolve(process.execPath, '..', '..', 'lib', 'node'));

// clone as a read-only copy, for introspection.
Module.globalPaths = modulePaths.slice(0);
Expand Down