Skip to content

Commit 22f6cd0

Browse files
authored
perf(vite): reduce inspect write batch overhead (#622)
1 parent f035a20 commit 22f6cd0

2 files changed

Lines changed: 60 additions & 30 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,3 +1487,25 @@ describe('vite inspect payload archive free space', () => {
14871487
}
14881488
})
14891489
})
1490+
1491+
describe('vite inspect batched writes', () => {
1492+
it('keeps the same module id separate across scopes in one batch', async () => {
1493+
const { ctx } = await createFixture()
1494+
const info = (result: string) => ({ name: 'plugin-a', result, start: 0, end: 1 })
1495+
// Both scopes are queued before the flush, so they land in the same write batch.
1496+
ctx.store.recordTransform('client', '/src/shared.ts', '/src/shared.ts', info('export const shared = 1'), 'const client = 1')
1497+
ctx.store.recordTransform('ssr', '/src/shared.ts', '/src/shared.ts', info('export const shared = 1'), 'const ssr = 1')
1498+
ctx.store.recordTransform('ssr', '/src/shared.ts', '/src/shared.ts', { ...info('export const shared = 1'), name: 'plugin-b' }, 'export const shared = 1')
1499+
await ctx.store.flush()
1500+
1501+
expect(await ctx.store.getModuleTransforms('client', '/src/shared.ts')).toMatchObject([
1502+
{ name: '__load__', result: 'const client = 1' },
1503+
{ name: 'plugin-a', result: 'export const shared = 1' },
1504+
])
1505+
expect(await ctx.store.getModuleTransforms('ssr', '/src/shared.ts')).toMatchObject([
1506+
{ name: '__load__', result: 'const ssr = 1' },
1507+
{ name: 'plugin-a', result: 'export const shared = 1' },
1508+
{ name: 'plugin-b', result: 'export const shared = 1' },
1509+
])
1510+
})
1511+
})

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ interface PreparedWrite {
5555
sourceIndex?: number
5656
}
5757

58+
interface ModuleBatchState {
59+
source?: { hasSource: boolean, transformRequest?: object }
60+
latest?: { value: string, index: number }
61+
}
62+
5863
export type {
5964
ViteInspectPluginMetricItem,
6065
ViteInspectResolveIdItem,
@@ -459,31 +464,38 @@ class PayloadViteInspectStore implements ViteInspectStore {
459464
const values: Array<string | null | undefined> = []
460465
const prepared: PreparedWrite[] = []
461466
const pluginCallWrites: InspectPluginCallWrite[] = []
462-
const sourceState = new Map<string, { hasSource: boolean, transformRequest?: object }>()
463-
// Latest code queued for each module in this batch, so a step that returns its input unchanged reuses that payload.
464-
const latestCode = new Map<string, { value: string, index: number }>()
465-
const pushCode = (key: string, value: string | null | undefined): number => {
466-
const latest = latestCode.get(key)
467-
if (value != null && latest?.value === value)
468-
return latest.index
467+
// Keyed by scope, then module id, to avoid building a composite key per write.
468+
const moduleStates = new Map<string, Map<string, ModuleBatchState>>()
469+
const getModuleState = (scope: string, moduleId: string): ModuleBatchState => {
470+
let scoped = moduleStates.get(scope)
471+
if (!scoped) {
472+
scoped = new Map()
473+
moduleStates.set(scope, scoped)
474+
}
475+
let state = scoped.get(moduleId)
476+
if (!state) {
477+
state = {}
478+
scoped.set(moduleId, state)
479+
}
480+
return state
481+
}
482+
const pushCode = (state: ModuleBatchState, value: string | null | undefined): number => {
483+
if (value != null && state.latest?.value === value)
484+
return state.latest.index
469485
const index = values.push(value) - 1
470486
if (value != null)
471-
latestCode.set(key, { value, index })
487+
state.latest = { value, index }
472488
return index
473489
}
474-
475-
const hasSource = (scope: string, moduleId: string, transformRequest?: object): boolean => {
476-
const key = scopeKey(scope, moduleId)
477-
let state = sourceState.get(key)
478-
if (!state) {
490+
const hasSource = (state: ModuleBatchState, scope: string, moduleId: string, transformRequest?: object): boolean => {
491+
if (!state.source) {
479492
const stored = this.scopes.get(scope)?.transforms.get(moduleId)
480-
state = {
493+
state.source = {
481494
hasSource: stored?.items.some(item => item.hasResult) ?? false,
482495
transformRequest: stored?.transformRequest,
483496
}
484-
sourceState.set(key, state)
485497
}
486-
return state.hasSource && (!transformRequest || state.transformRequest === transformRequest)
498+
return state.source.hasSource && (!transformRequest || state.source.transformRequest === transformRequest)
487499
}
488500

489501
for (const write of writes) {
@@ -499,19 +511,19 @@ class PayloadViteInspectStore implements ViteInspectStore {
499511

500512
const item: PreparedWrite = { write }
501513
if (write.operation === 'recordTransform') {
502-
const key = scopeKey(write.scope, write.moduleId)
503-
if (!hasSource(write.scope, write.moduleId, write.transformRequest)) {
504-
latestCode.delete(key)
505-
item.sourceIndex = pushCode(key, write.preTransformCode)
506-
sourceState.set(key, { hasSource: true, transformRequest: write.transformRequest })
514+
const state = getModuleState(write.scope, write.moduleId)
515+
if (!hasSource(state, write.scope, write.moduleId, write.transformRequest)) {
516+
state.latest = undefined
517+
item.sourceIndex = pushCode(state, write.preTransformCode)
518+
state.source = { hasSource: true, transformRequest: write.transformRequest }
507519
}
508-
item.resultIndex = pushCode(key, write.info.result)
520+
item.resultIndex = pushCode(state, write.info.result)
509521
}
510522
else if (write.operation === 'recordLoad') {
511-
const key = scopeKey(write.scope, write.moduleId)
512-
latestCode.delete(key)
513-
item.resultIndex = pushCode(key, write.info.result)
514-
sourceState.set(scopeKey(write.scope, write.moduleId), { hasSource: write.info.result != null, transformRequest: write.transformRequest })
523+
const state = getModuleState(write.scope, write.moduleId)
524+
state.latest = undefined
525+
item.resultIndex = pushCode(state, write.info.result)
526+
state.source = { hasSource: write.info.result != null, transformRequest: write.transformRequest }
515527
}
516528
prepared.push(item)
517529
}
@@ -940,10 +952,6 @@ function rangeAt(
940952
return index == null ? undefined : ranges[index]
941953
}
942954

943-
function scopeKey(scope: string, moduleId: string): string {
944-
return `${scope}\0${moduleId}`
945-
}
946-
947955
function positiveInteger(value: number | undefined, fallback: number): number {
948956
return Number.isFinite(value) && value! > 0
949957
? Math.max(1, Math.floor(value!))

0 commit comments

Comments
 (0)