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

Resolver perf: Use directory existence to cut down candidate list (3/n) #1333

Closed
wants to merge 1 commit into from

Conversation

robhogan
Copy link
Contributor

Summary:
Further optimise resolution with some work avoidance by using the fact that, when resolving require('foo'), we can eliminate any candidates of the form <prefix>/node_modules/foo<suffix> unless <prefix>/node_modules exists and is a directory. This saves a lot of lookups from deeply-nested origin modules.

Further, when resolving require('foo/bar'), we can eliminate candidates of the form <prefix>/node_modules/foo/<suffix> unless <prefix>/node_modules/foo exists as a directory.

This is a bit more subtle than it might seem, and can't be extended beyond these cases, due to redirection. For require('foo/bar/baz'), foo/bar need not exist anywhere, because some foo/package.json could redirect it. However, when looking up a package.json for possible redirections, we stop at node_modules (we do not check node_modules/package.json), so it's safe to treat anything up to and including node_modules (or node_modules/foo in the case node_modules/foo.js may not be a candidate) as a real file path that must be an extant directory.

This fix reduces the existence checks we need to perform in RNTester such that total resolution speed is improved ~3x, or ~12x relative to the start of this stack / the previous Metro release.

- **[Performance]**: Use directory existence to cut down resolution candidates

Reviewed By: huntie

Differential Revision: D58594993

Summary:
Further optimise resolution with some work avoidance by using the fact that, when resolving `require('foo')`, we can eliminate any candidates of the form `<prefix>/node_modules/foo<suffix>` unless `<prefix>/node_modules` exists and is a directory. This saves a lot of lookups from deeply-nested origin modules.

Further, when resolving `require('foo/bar')`, we can eliminate candidates of the form `<prefix>/node_modules/foo/<suffix>` unless `<prefix>/node_modules/foo` exists as a directory.

This is a bit more subtle than it might seem, and can't be extended beyond these cases, due to redirection. For `require('foo/bar/baz')`, `foo/bar` need not exist anywhere, because some `foo/package.json` could redirect it. However, when looking up a `package.json` for possible redirections, we stop at `node_modules` (we do not check `node_modules/package.json`), so  it's safe to treat anything up to and including `node_modules` (or `node_modules/foo` in the case `node_modules/foo.js` may not be a candidate) as a real file path that must be an extant directory.

This fix reduces the existence checks we need to perform in RNTester such that total resolution speed is improved ~3x, or ~12x relative to the start of this stack / the previous Metro release.

```
- **[Performance]**: Use directory existence to cut down resolution candidates
```

Reviewed By: huntie

Differential Revision: D58594993
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 27, 2024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D58594993

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 6f3af82.

@robhogan robhogan deleted the export-D58594993 branch August 27, 2024 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants