Skip to content

Commit d29908c

Browse files
authored
perf(vite): speed up inspect invalidation (#620)
1 parent f130e0e commit d29908c

2 files changed

Lines changed: 151 additions & 57 deletions

File tree

‎packages/vite/src/node/__tests__/inspect-context.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,42 @@ describe('vite inspect public module ids', () => {
13711371
expect(envCtx.getPublicModuleId('/src/main.ts')).toBe('/src/main.ts')
13721372
})
13731373
})
1374+
1375+
describe('vite inspect invalidation', () => {
1376+
const info = (name: string) => ({ name, result: `export const name = '${name}'`, start: 0, end: 1 })
1377+
1378+
it('clears modules linked only through a public id and keeps unrelated modules', async () => {
1379+
const { ctx, envCtx } = await createFixture()
1380+
const scope = envCtx.scope
1381+
ctx.store.recordTransform(scope, '/abs/a.js', 'pub-a', info('a'), 'const a = 1')
1382+
ctx.store.recordTransform(scope, '/abs/b.js', 'pub-b', info('b'), 'const b = 1')
1383+
ctx.store.recordTransform(scope, '/abs/c.js', 'pub-c', info('c'), 'const c = 1')
1384+
// This resolve result shares a's public id but points at b, so a and b are linked only through `pub-a`.
1385+
ctx.store.recordResolveId(scope, 'spec', 'spec', { name: 'resolver', result: '/abs/b.js', start: 0, end: 1 }, 'pub-a')
1386+
await ctx.store.flush()
1387+
1388+
ctx.store.invalidate(scope, '/abs/a.js', 'pub-a')
1389+
await ctx.store.flush()
1390+
1391+
expect(await ctx.store.getModuleIds(scope)).toEqual(['/abs/c.js'])
1392+
expect(await ctx.store.getResolveIdList(scope)).toEqual([])
1393+
})
1394+
1395+
it('does not reach removed modules through stale links', async () => {
1396+
const { ctx, envCtx } = await createFixture()
1397+
const scope = envCtx.scope
1398+
ctx.store.recordTransform(scope, '/abs/a.js', 'pub-shared', info('a'), 'const a = 1')
1399+
await ctx.store.flush()
1400+
ctx.store.invalidate(scope, '/abs/a.js', 'pub-shared')
1401+
await ctx.store.flush()
1402+
1403+
// A new module takes over the public id; invalidating it must only touch the current records.
1404+
ctx.store.recordTransform(scope, '/abs/d.js', 'pub-shared', info('d'), 'const d = 1')
1405+
ctx.store.recordTransform(scope, '/abs/e.js', 'pub-e', info('e'), 'const e = 1')
1406+
await ctx.store.flush()
1407+
ctx.store.invalidate(scope, '/abs/d.js', 'pub-shared')
1408+
await ctx.store.flush()
1409+
1410+
expect(await ctx.store.getModuleIds(scope)).toEqual(['/abs/e.js'])
1411+
})
1412+
})

‎packages/vite/src/node/inspect/store/index.ts‎

Lines changed: 112 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ interface StoredModuleTransforms {
4141
interface ScopeData {
4242
transforms: Map<string, StoredModuleTransforms>
4343
resolveIds: Map<string, ViteInspectResolveIdItem[]>
44+
// Indexes for invalidation; keep them in sync through setTransforms, deleteTransforms, addResolveId and deleteResolveId.
45+
transformIdsByPublicId: Map<string, Set<string>>
46+
resolveIdsById: Map<string, Set<ViteInspectResolveIdItem>>
47+
resolveIdsByPublicId: Map<string, Set<ViteInspectResolveIdItem>>
4448
declinedLoadMetrics: Map<string, Map<string, ViteInspectPluginMetricItem>>
4549
pendingLoadMetrics: Map<object | undefined, Map<string, Map<string, ViteInspectPluginMetricItem>>>
4650
}
@@ -554,13 +558,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
554558
addMetric(metrics, write.info.plugin_id, write.info.plugin_name, write.info.duration)
555559
}
556560
else if (write.operation === 'recordResolveId') {
557-
const data = this.getScope(write.scope)
558-
let resolveIds = data.resolveIds.get(write.sourceId)
559-
if (!resolveIds) {
560-
resolveIds = []
561-
data.resolveIds.set(write.sourceId, resolveIds)
562-
}
563-
resolveIds.push({
561+
addResolveId(this.getScope(write.scope), {
564562
sourceId: write.sourceId,
565563
sourcePublicId: write.sourcePublicId,
566564
result: write.info.result,
@@ -591,7 +589,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
591589
invokeCount: transforms?.invokeCount ?? 0,
592590
items: [],
593591
}
594-
data.transforms.set(write.moduleId, transforms)
592+
setTransforms(data, write.moduleId, transforms)
595593
}
596594

597595
if (!transforms.items.some(item => item.hasResult)) {
@@ -619,7 +617,7 @@ class PayloadViteInspectStore implements ViteInspectStore {
619617
const existing = data.transforms.get(write.moduleId)
620618
if (existing)
621619
this.payloads.reclaim(collectPayloadRanges(existing.items))
622-
data.transforms.set(write.moduleId, {
620+
setTransforms(data, write.moduleId, {
623621
transformRequest: write.transformRequest,
624622
publicModuleId: write.publicModuleId,
625623
invokeCount: (existing?.invokeCount ?? 0) + 1,
@@ -647,46 +645,59 @@ class PayloadViteInspectStore implements ViteInspectStore {
647645
return
648646
}
649647

650-
const invalidIds = new Set([moduleId])
651-
const invalidPublicIds = new Set([publicModuleId])
652-
let changed = true
653-
while (changed) {
654-
changed = false
655-
for (const [id, transforms] of data.transforms) {
656-
if (invalidIds.has(id) || invalidPublicIds.has(transforms.publicModuleId))
657-
changed = addInvalidId(invalidIds, invalidPublicIds, id, transforms.publicModuleId) || changed
648+
// Walk only the records linked to this module through the indexes, instead of rescanning the whole scope.
649+
const invalidIds = new Set<string>()
650+
const invalidPublicIds = new Set<string>()
651+
const pendingIds: string[] = []
652+
const pendingPublicIds: string[] = []
653+
const linkedResolveIds = new Set<ViteInspectResolveIdItem>()
654+
const addInvalid = (id: string, publicId: string) => {
655+
if (!invalidIds.has(id)) {
656+
invalidIds.add(id)
657+
pendingIds.push(id)
658658
}
659-
for (const resolveIds of data.resolveIds.values()) {
660-
for (const resolveId of resolveIds) {
661-
if (!matchesInvalid(resolveId.sourceId, resolveId.sourcePublicId, invalidIds, invalidPublicIds)
662-
&& !matchesInvalid(resolveId.result, resolveId.resultPublicId, invalidIds, invalidPublicIds)) {
663-
continue
664-
}
665-
changed = addInvalidId(invalidIds, invalidPublicIds, resolveId.sourceId, resolveId.sourcePublicId) || changed
666-
changed = addInvalidId(invalidIds, invalidPublicIds, resolveId.result, resolveId.resultPublicId) || changed
667-
}
659+
if (!invalidPublicIds.has(publicId)) {
660+
invalidPublicIds.add(publicId)
661+
pendingPublicIds.push(publicId)
662+
}
663+
}
664+
const linkResolveIds = (items: Set<ViteInspectResolveIdItem> | undefined) => {
665+
for (const item of items ?? []) {
666+
if (linkedResolveIds.has(item))
667+
continue
668+
linkedResolveIds.add(item)
669+
addInvalid(item.sourceId, item.sourcePublicId)
670+
addInvalid(item.result, item.resultPublicId)
668671
}
669672
}
670673

674+
addInvalid(moduleId, publicModuleId)
675+
while (pendingIds.length > 0 || pendingPublicIds.length > 0) {
676+
const id = pendingIds.pop()
677+
if (id !== undefined) {
678+
const transforms = data.transforms.get(id)
679+
if (transforms)
680+
addInvalid(id, transforms.publicModuleId)
681+
linkResolveIds(data.resolveIdsById.get(id))
682+
continue
683+
}
684+
const publicId = pendingPublicIds.pop()!
685+
for (const transformId of data.transformIdsByPublicId.get(publicId) ?? [])
686+
addInvalid(transformId, data.transforms.get(transformId)!.publicModuleId)
687+
linkResolveIds(data.resolveIdsByPublicId.get(publicId))
688+
}
689+
671690
const reclaimed: ViteInspectPayloadRange[] = []
672-
for (const [id, transforms] of data.transforms) {
673-
if (!matchesInvalid(id, transforms.publicModuleId, invalidIds, invalidPublicIds))
691+
for (const id of invalidIds) {
692+
const transforms = data.transforms.get(id)
693+
if (!transforms)
674694
continue
675695
reclaimed.push(...collectPayloadRanges(transforms.items))
676-
data.transforms.delete(id)
696+
deleteTransforms(data, id)
677697
}
678698
this.payloads.reclaim(reclaimed)
679699

680-
for (const [sourceId, resolveIds] of data.resolveIds) {
681-
const remaining = resolveIds.filter((resolveId) => {
682-
return !matchesInvalid(sourceId, resolveId.sourcePublicId, invalidIds, invalidPublicIds)
683-
&& !matchesInvalid(resolveId.result, resolveId.resultPublicId, invalidIds, invalidPublicIds)
684-
})
685-
if (remaining.length > 0)
686-
data.resolveIds.set(sourceId, remaining)
687-
else
688-
data.resolveIds.delete(sourceId)
689-
}
700+
deleteResolveIds(data, linkedResolveIds)
690701

691702
for (const id of invalidPublicIds) {
692703
data.declinedLoadMetrics.delete(id)
@@ -756,6 +767,9 @@ class PayloadViteInspectStore implements ViteInspectStore {
756767
data = {
757768
transforms: new Map(),
758769
resolveIds: new Map(),
770+
transformIdsByPublicId: new Map(),
771+
resolveIdsById: new Map(),
772+
resolveIdsByPublicId: new Map(),
759773
declinedLoadMetrics: new Map(),
760774
pendingLoadMetrics: new Map(),
761775
}
@@ -830,26 +844,67 @@ function addMetric(
830844
metric.totalTime += duration
831845
}
832846

833-
function addInvalidId(
834-
ids: Set<string>,
835-
publicIds: Set<string>,
836-
id: string,
837-
publicId: string,
838-
): boolean {
839-
const idSize = ids.size
840-
const publicIdSize = publicIds.size
841-
ids.add(id)
842-
publicIds.add(publicId)
843-
return ids.size !== idSize || publicIds.size !== publicIdSize
847+
function setTransforms(data: ScopeData, id: string, transforms: StoredModuleTransforms): void {
848+
deleteTransforms(data, id)
849+
data.transforms.set(id, transforms)
850+
addToIndex(data.transformIdsByPublicId, transforms.publicModuleId, id)
851+
}
852+
853+
function deleteTransforms(data: ScopeData, id: string): void {
854+
const existing = data.transforms.get(id)
855+
if (!existing)
856+
return
857+
data.transforms.delete(id)
858+
removeFromIndex(data.transformIdsByPublicId, existing.publicModuleId, id)
859+
}
860+
861+
function addResolveId(data: ScopeData, item: ViteInspectResolveIdItem): void {
862+
let items = data.resolveIds.get(item.sourceId)
863+
if (!items) {
864+
items = []
865+
data.resolveIds.set(item.sourceId, items)
866+
}
867+
items.push(item)
868+
addToIndex(data.resolveIdsById, item.sourceId, item)
869+
addToIndex(data.resolveIdsById, item.result, item)
870+
addToIndex(data.resolveIdsByPublicId, item.sourcePublicId, item)
871+
addToIndex(data.resolveIdsByPublicId, item.resultPublicId, item)
872+
}
873+
874+
function deleteResolveIds(data: ScopeData, items: Set<ViteInspectResolveIdItem>): void {
875+
const sourceIds = new Set<string>()
876+
for (const item of items) {
877+
sourceIds.add(item.sourceId)
878+
removeFromIndex(data.resolveIdsById, item.sourceId, item)
879+
removeFromIndex(data.resolveIdsById, item.result, item)
880+
removeFromIndex(data.resolveIdsByPublicId, item.sourcePublicId, item)
881+
removeFromIndex(data.resolveIdsByPublicId, item.resultPublicId, item)
882+
}
883+
for (const sourceId of sourceIds) {
884+
const remaining = data.resolveIds.get(sourceId)!.filter(item => !items.has(item))
885+
if (remaining.length > 0)
886+
data.resolveIds.set(sourceId, remaining)
887+
else
888+
data.resolveIds.delete(sourceId)
889+
}
890+
}
891+
892+
function addToIndex<T>(index: Map<string, Set<T>>, key: string, value: T): void {
893+
let values = index.get(key)
894+
if (!values) {
895+
values = new Set()
896+
index.set(key, values)
897+
}
898+
values.add(value)
844899
}
845900

846-
function matchesInvalid(
847-
id: string,
848-
publicId: string,
849-
ids: Set<string>,
850-
publicIds: Set<string>,
851-
): boolean {
852-
return ids.has(id) || publicIds.has(publicId)
901+
function removeFromIndex<T>(index: Map<string, Set<T>>, key: string, value: T): void {
902+
const values = index.get(key)
903+
if (!values)
904+
return
905+
values.delete(value)
906+
if (values.size === 0)
907+
index.delete(key)
853908
}
854909

855910
function collectPayloadRanges(items: StoredViteInspectTransformInfo[]): ViteInspectPayloadRange[] {

0 commit comments

Comments
 (0)