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: improve package cache usage #12512

Merged
merged 16 commits into from Mar 21, 2023
Merged

perf: improve package cache usage #12512

merged 16 commits into from Mar 21, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Mar 21, 2023

Description

This PR implements a smarter package caching algorithm.

Why and how

When we're given /User/foo/bar/node_modules/my-lib/deep/deep/deep/file.js, and file.js wants to get my-dep/package.json. When we found a match, we use /User/.../file.js as the cache key for the PackageData.

However, we also know that if /User/foo/bar/node_modules/my-lib/deep/another-file.js happens to want to get my-dep/package.json too, it would get the same PackageData too, because we had traversed the same directories to get there.

This PR implements this, where the directories traversed while handling /User/foo/bar/node_modules/my-lib/deep/deep/deep/file.js are also cached, so that another-file can fast-path to the PackageData.

Additional context

  1. This is a huge change. I've broken up to multiple commits so it's easier to follow along, though it seems to be too many commits. Or you can review the diff directly too 💪

  2. It looks like packageCache is used for two cases before, this PR preserves this oddness since it's easier to work with it, for now.

  3. More logs and notes on perf at https://gist.github.com/bluwy/53d11b29394c7e9e399f777fae099efc


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 21, 2023
@stackblitz
Copy link

stackblitz bot commented Mar 21, 2023

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

Comment on lines 60 to 62
// use separate package cache for optimizer as it caches paths around node_modules
// and it's unlikely for the core Vite process to traverse into node_modules again
const packageCache: PackageCache = new Map()
Copy link
Member

Choose a reason for hiding this comment

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

Good idea! It would still traverse for excluded deps but that is completely orthogonal to the paths cached here.

and split cache for optimizer esm and cjs resolvers
@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Mar 21, 2023

📝 Ran ecosystem CI: Open

suite result
astro ❌ failure
histoire ❌ failure
iles ❌ failure
ladle ❌ failure
laravel ✅ success
marko ✅ success
nuxt ❌ failure
previewjs ✅ success
qwik ✅ success
rakkas ✅ success
sveltekit ✅ success
vite-plugin-ssr ✅ success
vite-plugin-react ❌ failure
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

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.

Awesome PR 🔥

The same failures in vite-ecosystem-ci are in main

@patak-dev patak-dev merged commit abc2b9c into main Mar 21, 2023
13 checks passed
@patak-dev patak-dev deleted the improve-pkg-cache branch March 21, 2023 17:07
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.

None yet

2 participants