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): keep track of already traversed modules when propagating update #12658

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export function updateModules(
): void {
const updates: Update[] = []
const invalidatedModules = new Set<ModuleNode>()
const traversedModules = new Set<ModuleNode>()
let needFullReload = false

for (const mod of modules) {
Expand All @@ -148,18 +149,15 @@ export function updateModules(
continue
}

const boundaries = new Set<{
boundary: ModuleNode
acceptedVia: ModuleNode
}>()
const hasDeadEnd = propagateUpdate(mod, boundaries)
const boundaries: { boundary: ModuleNode; acceptedVia: ModuleNode }[] = []
const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries)
if (hasDeadEnd) {
needFullReload = true
continue
}

updates.push(
...[...boundaries].map(({ boundary, acceptedVia }) => ({
...boundaries.map(({ boundary, acceptedVia }) => ({
type: `${boundary.type}-update` as const,
timestamp,
path: normalizeHmrUrl(boundary.url),
Expand Down Expand Up @@ -231,12 +229,15 @@ function areAllImportsAccepted(

function propagateUpdate(
node: ModuleNode,
boundaries: Set<{
boundary: ModuleNode
acceptedVia: ModuleNode
}>,
traversedModules: Set<ModuleNode>,
boundaries: { boundary: ModuleNode; acceptedVia: ModuleNode }[],
currentChain: ModuleNode[] = [node],
): boolean /* hasDeadEnd */ {
if (traversedModules.has(node)) {
return false
}
traversedModules.add(node)

// #7561
// if the imports of `node` have not been analyzed, then `node` has not
// been loaded in the browser and we should stop propagation.
Expand All @@ -250,16 +251,18 @@ function propagateUpdate(
}

if (node.isSelfAccepting) {
boundaries.add({
boundary: node,
acceptedVia: node,
})
boundaries.push({ boundary: node, acceptedVia: node })

// additionally check for CSS importers, since a PostCSS plugin like
// Tailwind JIT may register any file as a dependency to a CSS file.
for (const importer of node.importers) {
if (isCSSRequest(importer.url) && !currentChain.includes(importer)) {
propagateUpdate(importer, boundaries, currentChain.concat(importer))
propagateUpdate(
importer,
traversedModules,
boundaries,
currentChain.concat(importer),
Copy link
Member

Choose a reason for hiding this comment

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

Looks great! Let's merge so we can test it during the beta.

Later on, maybe it is worth exploring using a data structure for currentChain/propagateUpdate that doesn't require the creation of new arrays on every call.

)
}
}

Expand All @@ -272,10 +275,7 @@ function propagateUpdate(
// Also, the imported module (this one) must be updated before the importers,
// so that they do get the fresh imported module when/if they are reloaded.
if (node.acceptedHmrExports) {
boundaries.add({
boundary: node,
acceptedVia: node,
})
boundaries.push({ boundary: node, acceptedVia: node })
} else {
if (!node.importers.size) {
return true
Expand All @@ -295,10 +295,7 @@ function propagateUpdate(
for (const importer of node.importers) {
const subChain = currentChain.concat(importer)
if (importer.acceptedHmrDeps.has(node)) {
boundaries.add({
boundary: importer,
acceptedVia: node,
})
boundaries.push({ boundary: importer, acceptedVia: node })
continue
}

Expand All @@ -317,7 +314,7 @@ function propagateUpdate(
return true
}

if (propagateUpdate(importer, boundaries, subChain)) {
if (propagateUpdate(importer, traversedModules, boundaries, subChain)) {
return true
}
}
Expand Down