Skip to content

module: addBuiltinLibsToObject refactoring #14085

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

Closed
wants to merge 1 commit into from
Closed

module: addBuiltinLibsToObject refactoring #14085

wants to merge 1 commit into from

Conversation

viktor-ku
Copy link
Contributor

  • Replace forEach loop by for loop for several reasons. One of them
    being performance. Loop over a small array is fast anyway, but this will
    make code more consistent across the project. Feels like Node.js is
    trying to be as fast as possible

  • Move setReal function definition out of the loop and make
    it bindable. Defining a function inside loop is considered bad
    practice in general.

  • Predefine configurable and enumerable as these are staying consistent
    across the function.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)
  • internal/module

@nodejs-github-bot nodejs-github-bot added the module Issues and PRs related to the module subsystem. label Jul 5, 2017
@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jul 5, 2017

@vsemozhetbyt vsemozhetbyt added the performance Issues and PRs related to the performance of Node.js. label Jul 5, 2017
// Make built-in modules available directly (loaded lazily).
builtinLibs.forEach((name) => {
const configurable = true;
const enumerable = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these are necessary. I would just keep these values inline to ensure no variable lookup has to be done by V8.

object[libname] = value;
}

for (var n = 0, len = builtinLibs.length; n < len; n++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to cache the length, V8 can work fine with using .length in the conditional part.

// Goals of this mechanism are:
// - Lazy loading of built-in modules
// - Having all built-in modules available as non-enumerable properties
// - Allowing the user to re-assign these variables as if there were no
// pre-existing globals with the same name.
const libname = builtinLibs[n];
const set = setReal.bind(null, libname);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why bind() this method but not the getter? I think we should be consistent. I do not know offhand if either method is more efficient, but since there are not so many built-in modules it may not make much difference.

@mscdex
Copy link
Contributor

mscdex commented Jul 5, 2017

Honestly I would be surprised if these changes made a noticeable improvement in startup time. As @vsemozhetbyt mentioned, it would be a good idea to show that it does via benchmark(s).

@viktor-ku
Copy link
Contributor Author

If benchmarks will not show a noticeable improvement I will just close this PR then.

@addaleax
Copy link
Member

addaleax commented Jul 5, 2017

This code is used for node --eval and for the REPL; in the latter case, microperformance is irrelevant, and I don’t think we have benchmarks for the former case, but I cannot imagine that this kind of refactoring has an impact given how much is going on during Node’s startup.

So I’d look at this patch based on the change in readablity rather than its value as a possible performance improvement. I’m pretty neutral on it – the previous code wasn’t too pretty, so I can understand that there’s room for enhancements.

@addaleax addaleax removed the performance Issues and PRs related to the performance of Node.js. label Jul 5, 2017
@viktor-ku
Copy link
Contributor Author

@addaleax where/how should I learn the codebase to do useful PR's? I would like to but I am new here. I will close this PR

Replace forEach-loop by for-loop for several reasons. One of them
being performance. Loop over a small array is fast anyway, but this will
make code more consistent across the project. Feels like Node.js is
trying to be as fast as possible.

Move "setReal" function definition out of the loop and make
it bindable. Defining a function inside loop is considered bad
practice in general.

Predefine configurable and enumerable as these are staying consistent
across the function.
@addaleax
Copy link
Member

addaleax commented Jul 5, 2017

@kuroljov One thing that might help with getting you going is that you might want to look at our test coverage and try to improve that: https://coverage.nodejs.org/ (Linux only atm).

If you’re specifically looking for performance improvements, I’m not sure myself. I would assume there are a few low-hanging fruits around since we switched to V8 5.9 on master, but the other people in this thread can probably give much more precise answers.

@viktor-ku
Copy link
Contributor Author

@addaleax where/how should I learn the codebase to do useful PR's? I would like to but I am new here. I will close this PR

@addaleax thank you. @vsemozhetbyt and @mscdex would you suggest something?

@viktor-ku viktor-ku closed this Jul 5, 2017
@mscdex
Copy link
Contributor

mscdex commented Jul 5, 2017

@kuroljov You might check the NodeTodo website for some suggestions.

@vsemozhetbyt
Copy link
Contributor

Cross-refs) #14062 (comment) ...

@viktor-ku
Copy link
Contributor Author

Thank you. This might be helpful

@viktor-ku viktor-ku deleted the internal-module branch July 6, 2017 02:58
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.

5 participants