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

src: implement IsInsideNodeModules() in C++ #55286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Oct 5, 2024

This previously compiles a script and run it in a new context to avoid global pollution, which is more complex than necessary and can be too slow for it to be reused in other cases. The new implementation just checks the frames in C++ which is safe from global pollution, faster and simpler.

The previous implementation also had a bug when the call site is in a ESM, because ESM have URLs as their script names, which don't start with '/' or '\' and will be skipped. The new implementation removes the skipping to fix it for ESM.

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Oct 5, 2024
@joyeecheung
Copy link
Member Author

The performance doesn't really matter all that much for new Buffer since it's deprecated, so I didn't add a benchmark. But since I plan to reuse IsInsideNodeModules() for require(esm) warning check when it comes from the parent-less require() built for imported CJS in #55217, I did some local benchmarking with this script, and it is faster than the original version:

const array = [];

console.time('new Buffer');
for (let i = 0; i < 10000; ++i) {
  array.push(new Buffer(10));
}
console.timeEnd('new Buffer');
❯ node node_modules/new-buffer.js
new Buffer: 52.04ms
❯ ../node/node node_modules/new-buffer.js
new Buffer: 26.604ms

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 5, 2024
This previously compiles a script and run it in a new context
to avoid global pollution, which is more complex than necessary
and can be too slow for it to be reused in other cases. The
new implementation just checks the frames in C++ which is safe
from global pollution, faster and simpler.

The previous implementation also had a bug when the call site
is in a ESM, because ESM have URLs as their script names,
which don't start with '/' or '\' and will be skipped. The new
implementation removes the skipping to fix it for ESM.
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Oct 5, 2024
Copy link
Contributor

github-actions bot commented Oct 5, 2024

Failed to start CI
   ⚠  No approving reviews found
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/11196042300

@juanarbol
Copy link
Member

Interesting

continue;
}

std::string script_name_str = Utf8Value(isolate, script_name).ToString();
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this string view and avoid a copy?

@@ -292,10 +292,47 @@ static void GetCallSite(const FunctionCallbackInfo<Value>& args) {

callsite_objects.push_back(obj);
}
}

static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) {
Copy link
Member

Choose a reason for hiding this comment

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

We can add V8 fast api to this to speed things up.

@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Oct 6, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 6, 2024
@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants