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

module: optimize platform lookup and initPath #3582

wants to merge 1 commit into from

Conversation

zertosh
Copy link
Contributor

@zertosh zertosh commented Oct 29, 2015

This caches the value of process.platform in a style that is consistent with the fs, os and path modules. It also improves Module._initPath in both legibility and simplicity. Instead of creating the modulePaths array from right-to-left via concats and unshifts, its now built left-to-right with pushs. Although it is a micro-optimization, the changes make it much easier to read and understand.

This caches the value of `process.platform`, and creates the
`modulePaths` array in a way that is more efficient and easier to
read and understand (left-to-right instead of right-to-left).
@cjihrig
Copy link
Contributor

cjihrig commented Oct 29, 2015

Without having looked at the changes, I just want to say that this churn in the module system makes me uncomfortable. It has the potential to break a lot of code.

@Trott
Copy link
Member

Trott commented Oct 29, 2015

@mscdex mscdex added the module Issues and PRs related to the module subsystem. label Oct 29, 2015
@mscdex
Copy link
Contributor

mscdex commented Oct 29, 2015

LGTM

@@ -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

@jasnell
Copy link
Member

jasnell commented Oct 29, 2015

@nodejs/ctc

@Fishrock123
Copy link
Contributor

cc @bmeck

@bmeck
Copy link
Member

bmeck commented Nov 2, 2015

+1 on cache of isWindows

Is the goal here otherwise to improve startup time? This does have some
breakage in the wild.

This affects tests that mutate NODE_PATH:
https://github.com/nodejs/node/search?utf8=%E2%9C%93&q=_initPaths&type=Code

Don't know the history of things that used the trick in these tests, but
might be a good place to research before pulling.

Also some modules (minimal usage but still):
http://searchnod.es/#!_initPaths/1

On Mon, Nov 2, 2015 at 10:41 AM, Jeremiah Senkpiel <notifications@github.com

wrote:

cc @bmeck https://github.com/bmeck


Reply to this email directly or view it on GitHub
#3582 (comment).

@bmeck
Copy link
Member

bmeck commented Nov 2, 2015

-0.5 on rest*

On Mon, Nov 2, 2015 at 10:56 AM, Bradley Meck bradley.meck@gmail.com
wrote:

+1 on cache of isWindows

Is the goal here otherwise to improve startup time? This does have some
breakage in the wild.

This affects tests that mutate NODE_PATH:
https://github.com/nodejs/node/search?utf8=%E2%9C%93&q=_initPaths&type=Code

Don't know the history of things that used the trick in these tests, but
might be a good place to research before pulling.

Also some modules (minimal usage but still):
http://searchnod.es/#!_initPaths/1

On Mon, Nov 2, 2015 at 10:41 AM, Jeremiah Senkpiel <
notifications@github.com> wrote:

cc @bmeck https://github.com/bmeck


Reply to this email directly or view it on GitHub
#3582 (comment).

@zertosh
Copy link
Contributor Author

zertosh commented Nov 2, 2015

@bmeck, another goal of the changes in _initPath is to improve legibility – no functionally changes. If that's a blocker for just the isWindows change, then I'd be more than happy to just do that then.

@zertosh
Copy link
Contributor Author

zertosh commented Feb 6, 2016

Not sure this is worth it anymore. Closing to reduce noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants