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

perf(resolve): improve package.json resolve speed #12441

Merged
merged 7 commits into from
Mar 21, 2023
Merged

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Mar 16, 2023

Description

Should fix #8850, my benchmarks were based on on the issue's repro.

This PR bring 1.5-4x resolve speed for resolving modules that heavily rely on resolving package.json. We only use the resolve package to resolve dep package.json files, I've implemented a simpler logic for it specifically, extracted from vitefu, which has been used by many frameworks so far.

This simpler logic is more straight-forward and prevents the use of errors + try/catch. It also supports yarn pnp. (fun fact: yarn pnp patches the resolve package by default)

Additional context

It would be easier to review by each commit.

Before merging this, it might be a good idea to run ecosystem-ci.

Here are the cpuprofiles: https://gist.github.com/bluwy/19c6c3ec6fc3479ffa5e679b4c61344e

The resolve dep is not removed yet. There's one more place that needs to be fixed before doing so, the work is tracked at #11410. This shouldn't affect anything though.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added p4-important Violate documented behavior or significantly improves performance (priority) performance Performance related enhancement labels Mar 16, 2023
@stackblitz
Copy link

stackblitz bot commented Mar 16, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

* if importer was not resolved by vite's resolver previously
* (when esbuild resolved it)
* resolve importer's pkg and add to idToPkgMap
* Load closest `package.json` to `importer`
*/
function resolvePkg(importer: string, options: InternalResolveOptions) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to refactor this function, since it's incorrectly using resolvePackageData and looks unnecessarily complex 🤔 What happens is that this function gets the nearest package.json, and loads the PackageData. And the new changes does the same too.

In the old changes, it manually walks /User/foo/project, create a possiblePkgIds of ['/User/foo/project', '/User/foo', '/User'], and then try to resolve the package.json in those directories. So resolvePackageData would receive absolute paths instead of bare imports which doesn't work with this refactor.

@sun0day
Copy link
Member

sun0day commented Mar 16, 2023

Nice work!

patak-dev
patak-dev previously approved these changes Mar 16, 2023
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Incredible work 🔥

Changes look good to me, thanks for breaking them down into simpler commits.

It feels a bit big to include in a patch. But it is tempting to do so. If we don't, we could start the 4.3 beta soon after we give some time to 4.2 to stabilize.

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Mar 16, 2023

📝 Ran ecosystem CI: Open

suite result
astro ✅ success
histoire ❌ failure
iles ✅ success
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt ❌ failure
previewjs ✅ success
qwik ❌ failure
rakkas ✅ success
sveltekit ✅ success
vite-plugin-ssr ✅ success
vite-plugin-react ✅ success
vite-plugin-react-pages ✅ success
vite-plugin-react-swc ✅ success
vite-plugin-svelte ✅ success
vite-plugin-vue ✅ success
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ✅ success
windicss ✅ success

@patak-dev
Copy link
Member

The same projects are failing compared to main (same error in Nuxt). I think we should merge and release tomorrow as 4.2.1 so we can build on top of this PR without waiting for 4.3.

@dominikg
Copy link
Contributor

I like the simple approach this has 👍

@Liknox
Copy link

Liknox commented Mar 17, 2023

Looks amazing🔥. I think we should implement this performance improving!

@patak-dev patak-dev added this to the 4.3 milestone Mar 21, 2023
@patak-dev patak-dev merged commit 1fc8c65 into main Mar 21, 2023
@patak-dev patak-dev deleted the refactor-resolve-from branch March 21, 2023 06:42
@brillout
Copy link
Contributor

A little late to the party, but LGTM. (Also 👍 to get it out in the field sooner rather than lather.) Neat PR 🔥.

@bartlomieju
Copy link
Contributor

Commenting at the request of @bluwy - LGTM and shouldn't be a blocker in any way for Deno plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important Violate documented behavior or significantly improves performance (priority) performance Performance related enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pre-bundling is very slow when including ant-design-vue
7 participants