Skip to content

Commit 80c526e

Browse files
authored
perf: unresolvedUrlToModule promise cache (#12725)
1 parent 3ef8aaa commit 80c526e

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

packages/vite/src/node/server/moduleGraph.ts

+45-32
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ export class ModuleGraph {
6464
/**
6565
* @internal
6666
*/
67-
_unresolvedUrlToModuleMap = new Map<string, ModuleNode>()
67+
_unresolvedUrlToModuleMap = new Map<
68+
string,
69+
Promise<ModuleNode> | ModuleNode
70+
>()
6871
/**
6972
* @internal
7073
*/
71-
_ssrUnresolvedUrlToModuleMap = new Map<string, ModuleNode>()
74+
_ssrUnresolvedUrlToModuleMap = new Map<
75+
string,
76+
Promise<ModuleNode> | ModuleNode
77+
>()
7278

7379
constructor(
7480
private resolveId: (
@@ -255,37 +261,40 @@ export class ModuleGraph {
255261
if (mod) {
256262
return mod
257263
}
258-
const [url, resolvedId, meta] = await this._resolveUrl(
259-
rawUrl,
260-
ssr,
261-
resolved,
262-
)
263-
mod = this.idToModuleMap.get(resolvedId)
264-
if (!mod) {
265-
mod = new ModuleNode(url, setIsSelfAccepting)
266-
if (meta) mod.meta = meta
267-
this.urlToModuleMap.set(url, mod)
268-
mod.id = resolvedId
269-
this.idToModuleMap.set(resolvedId, mod)
270-
const file = (mod.file = cleanUrl(resolvedId))
271-
let fileMappedModules = this.fileToModulesMap.get(file)
272-
if (!fileMappedModules) {
273-
fileMappedModules = new Set()
274-
this.fileToModulesMap.set(file, fileMappedModules)
264+
const modPromise = (async () => {
265+
const [url, resolvedId, meta] = await this._resolveUrl(
266+
rawUrl,
267+
ssr,
268+
resolved,
269+
)
270+
mod = this.idToModuleMap.get(resolvedId)
271+
if (!mod) {
272+
mod = new ModuleNode(url, setIsSelfAccepting)
273+
if (meta) mod.meta = meta
274+
this.urlToModuleMap.set(url, mod)
275+
mod.id = resolvedId
276+
this.idToModuleMap.set(resolvedId, mod)
277+
const file = (mod.file = cleanUrl(resolvedId))
278+
let fileMappedModules = this.fileToModulesMap.get(file)
279+
if (!fileMappedModules) {
280+
fileMappedModules = new Set()
281+
this.fileToModulesMap.set(file, fileMappedModules)
282+
}
283+
fileMappedModules.add(mod)
275284
}
276-
fileMappedModules.add(mod)
277-
}
278-
// multiple urls can map to the same module and id, make sure we register
279-
// the url to the existing module in that case
280-
else if (!this.urlToModuleMap.has(url)) {
281-
this.urlToModuleMap.set(url, mod)
282-
}
285+
// multiple urls can map to the same module and id, make sure we register
286+
// the url to the existing module in that case
287+
else if (!this.urlToModuleMap.has(url)) {
288+
this.urlToModuleMap.set(url, mod)
289+
}
290+
this._setUnresolvedUrlToModule(rawUrl, mod, ssr)
291+
return mod
292+
})()
283293

284294
// Also register the clean url to the module, so that we can short-circuit
285295
// resolving the same url twice
286-
this._setUnresolvedUrlToModule(rawUrl, mod, ssr)
287-
288-
return mod
296+
this._setUnresolvedUrlToModule(rawUrl, modPromise, ssr)
297+
return modPromise
289298
}
290299

291300
// some deps, like a css file referenced via @import, don't have its own
@@ -319,7 +328,7 @@ export class ModuleGraph {
319328
// the same module
320329
async resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
321330
url = removeImportQuery(removeTimestampQuery(url))
322-
const mod = this._getUnresolvedUrlToModule(url, ssr)
331+
const mod = await this._getUnresolvedUrlToModule(url, ssr)
323332
if (mod?.id) {
324333
return [mod.url, mod.id, mod.meta]
325334
}
@@ -332,15 +341,19 @@ export class ModuleGraph {
332341
_getUnresolvedUrlToModule(
333342
url: string,
334343
ssr?: boolean,
335-
): ModuleNode | undefined {
344+
): Promise<ModuleNode> | ModuleNode | undefined {
336345
return (
337346
ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap
338347
).get(url)
339348
}
340349
/**
341350
* @internal
342351
*/
343-
_setUnresolvedUrlToModule(url: string, mod: ModuleNode, ssr?: boolean): void {
352+
_setUnresolvedUrlToModule(
353+
url: string,
354+
mod: Promise<ModuleNode> | ModuleNode,
355+
ssr?: boolean,
356+
): void {
344357
;(ssr
345358
? this._ssrUnresolvedUrlToModuleMap
346359
: this._unresolvedUrlToModuleMap

0 commit comments

Comments
 (0)