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(hmr): implement soft invalidation #14654

Merged
merged 12 commits into from
Oct 25, 2023
Merged

perf(hmr): implement soft invalidation #14654

merged 12 commits into from
Oct 25, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Oct 16, 2023

Description

When a module is invalidated, and its importers are invalidated recursively (to invalidate import with timestamp to retrieve new module), this PR implements a soft invalidation for those importers as they don't need a full load + transform. They only need to update the import timestamps which is done specifically as a fast path now.

However, some importers may still need to be hard invalidated, for example if they call addWatchFile on certain files. In the module graph, the representation for this is that importer would import those certain files, hence to differentiate files that are actually imported in the file, I created staticImportedUrls.

Soft-invalidation is optional, and is only an optimization for static imports in a file.

Additional context

For the record, I was thinking about this idea before I saw Evan's comment 😄

I had "soft invalidation" in my head, but happy to rename it to "partial invalidation" if anyone prefers it. For me, "soft" kinda refers to soft/hard caches and soft/hard deletes.


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 feat: hmr p3-significant High priority enhancement (priority) performance Performance related enhancement labels Oct 16, 2023
Copy link
Member

@ArnaudBarre ArnaudBarre left a comment

Choose a reason for hiding this comment

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

This is actually something I noticed while implementing my "own Vite" 1 and half year ago but completely forgot to backport this idea.

For sourcemap I think this should be fine because the length of query params will be stable after and before.

packages/vite/src/node/server/moduleGraph.ts Outdated Show resolved Hide resolved
packages/vite/src/node/server/moduleGraph.ts Outdated Show resolved Hide resolved
@bluwy bluwy marked this pull request as ready for review October 18, 2023 11:24
bluwy and others added 2 commits October 19, 2023 13:51
Co-authored-by: patak <matias.capeletto@gmail.com>
patak-dev
patak-dev previously approved these changes Oct 19, 2023
ArnaudBarre
ArnaudBarre previously approved these changes Oct 19, 2023
packages/vite/src/node/server/moduleGraph.ts Outdated Show resolved Hide resolved
packages/vite/src/node/server/moduleGraph.ts Outdated Show resolved Hide resolved
packages/vite/src/node/plugins/importAnalysis.ts Outdated Show resolved Hide resolved
@bluwy bluwy dismissed stale reviews from ArnaudBarre and patak-dev via 50dbb13 October 19, 2023 15:55
@bluwy
Copy link
Member Author

bluwy commented Oct 20, 2023

Thanks to Arnaud's suggestion to use HARD_INVALIDATED, it made clear of some bugs (that's now fixed):

  • If a module was hard-invalidated once before, it'll never soft-invalidate again in the lifetime of the server (Moved invalidation reset higher in handleModuleSoftInvalidation)
  • If a module is soft, then hard invalidated in a HMR chain, the second invalidation will propagate its hard invalidation state to its importers too (Updated seen.has check in invalidateModule)
  • The updated code doesn't have the right etag (Regenerated it)

@bluwy bluwy added this to the 5.0 milestone Oct 23, 2023
@bluwy
Copy link
Member Author

bluwy commented Oct 23, 2023

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 7fabbb4: Open

suite result latest scheduled
analogjs success success
astro success success
histoire success success
ladle success success
laravel failure failure
marko failure failure
nuxt success success
nx failure failure
previewjs success success
qwik success success
rakkas success success
sveltekit failure failure
unocss success success
vike failure failure
vite-plugin-pwa failure failure
vite-plugin-react success success
vite-plugin-react-pages success success
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success success
vite-setup-catalogue success success
vitepress success success
vitest failure failure

Copy link
Member

@ArnaudBarre ArnaudBarre left a comment

Choose a reason for hiding this comment

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

Not an easy change, but I think the complexity is quite low compare to the big benefit for HMR perf (this would not be visible in benchmarks that update just a file that self-update, but sure will be in real world usages)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: hmr p3-significant High priority enhancement (priority) performance Performance related enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants