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

lib: reduce amount of caught URL errors #52658

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Apr 24, 2024

Since we have URL.parse() now, we don't need try/catch with new URL(). This will potentially improve the performance of hot paths.

cc @nodejs/url

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Apr 24, 2024
Copy link
Member

@RedYetiDev RedYetiDev left a comment

Choose a reason for hiding this comment

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

LGTM

@anonrig anonrig force-pushed the reduce-url-throws branch 2 times, most recently from 7bc1c6e to 9cfdd53 Compare April 24, 2024 02:46
} catch {
// responseURLObj not defined will throw in next branch.
}
responseURLObj = URL.parse(responseURL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have access to the Ada binding directly? If so, we might prefer that as it can't be affected by user mutations

Copy link
Member

Choose a reason for hiding this comment

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

Another option is to extract the static method as an internal function and use it.

Copy link
Member Author

Choose a reason for hiding this comment

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

if I export URLParse: URL.parse in internal/url.js would it make it immutable?

Copy link
Contributor

Choose a reason for hiding this comment

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

From what I can see in the implementation code, it seems we could simply pass kParseURLSymbol as a third argument, although URLParse is probably better for readability

Comment on lines +900 to +903
resolved = URL.parse(specifier, base);

if (resolved == null) {
throw new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about this one, it seems useful to have the error as cause (also if we're throwing an error anyway, I don't think we need to worry too much about performance)

Copy link
Member Author

Choose a reason for hiding this comment

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

Throwing an error, catching it, and later throwing it again has a really big performance impact. I'm not quite sure about the un-happy path probability of this code...

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 "passing invalid URL to import()" is a use-case we want to support.

Throwing an error, catching it, and later throwing it again has a really big performance impact

You say "really big", but I'm unconvinced we could even measure it.

Copy link
Member

Choose a reason for hiding this comment

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

While I'm generally happy with the revised approach (creating one error is better than creating two)... Unfortunately I think dropping the cause ends up making this a semver-major change. While unlikely, I can imagine code in the wild that looks at the ERR_UNSUPPORTED_RESOLVE_REQUEST error then looks for the cause to determine exactly why.

Comment on lines +353 to +361
const urlPath = URL.parse(url);

if (urlPath != null) {
try {
// We still need to read the FS to detect the exports.
source ??= readFileSync(urlPath, 'utf8');
} catch {
// Continue regardless of error.
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, also I don't think URL.parse(url) can ever be null at this point

Copy link
Member Author

Choose a reason for hiding this comment

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

Any downside to have this check either way?

Copy link
Contributor

Choose a reason for hiding this comment

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

Code readability, noise in the git blame.

lib/internal/url.js Outdated Show resolved Hide resolved
} catch {
// responseURLObj not defined will throw in next branch.
}
responseURLObj = URL.parse(responseURL);
Copy link
Member

Choose a reason for hiding this comment

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

This is mutable. I am pretty sure at least in the ESM loader we try not to look things up from mutable public APIs…

Copy link
Member Author

Choose a reason for hiding this comment

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

if I export URLParse: URL.parse in internal/url.js would it make it immutable?

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
resolved = URL.parse(specifier);

if (resolved == null && isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
throw new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
Copy link
Member

Choose a reason for hiding this comment

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

Likewise here... I'm afraid that dropping the cause is probably semver-major

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants