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: module graph url shortcuts #12635

Merged
merged 17 commits into from Mar 30, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 31 additions & 3 deletions packages/vite/src/node/server/moduleGraph.ts
Expand Up @@ -61,6 +61,8 @@ export class ModuleGraph {
fileToModulesMap = new Map<string, Set<ModuleNode>>()
safeModulesPath = new Set<string>()

#rawUrlToModuleMap = new Map<string, ModuleNode>()

constructor(
private resolveId: (
url: string,
Expand All @@ -72,6 +74,13 @@ export class ModuleGraph {
rawUrl: string,
ssr?: boolean,
): Promise<ModuleNode | undefined> {
// Quick path, if we already have a module for this rawUrl (even without extension)
const cleanedUrl = removeImportQuery(removeTimestampQuery(rawUrl))
const mod = this.#rawUrlToModuleMap.get(cleanedUrl)
if (mod) {
return mod
}

const [url] = await this.resolveUrl(rawUrl, ssr)
return this.urlToModuleMap.get(url)
}
Expand Down Expand Up @@ -189,14 +198,25 @@ export class ModuleGraph {
ssr?: boolean,
setIsSelfAccepting = true,
): Promise<ModuleNode> {
// Quick path, if we already have a module for this rawUrl (even without extension)
const cleanedUrl = removeImportQuery(removeTimestampQuery(rawUrl))
let mod = this.#rawUrlToModuleMap.get(cleanedUrl)
if (mod) {
return mod
}

const [url, resolvedId, meta] = await this.resolveUrl(rawUrl, ssr)
let mod = this.idToModuleMap.get(resolvedId)
mod = this.idToModuleMap.get(resolvedId)
if (!mod) {
mod = new ModuleNode(url, setIsSelfAccepting)
if (meta) mod.meta = meta

this.urlToModuleMap.set(url, mod)
this.#rawUrlToModuleMap.set(cleanedUrl, mod)

mod.id = resolvedId
this.idToModuleMap.set(resolvedId, mod)

const file = (mod.file = cleanUrl(resolvedId))
let fileMappedModules = this.fileToModulesMap.get(file)
if (!fileMappedModules) {
Expand All @@ -207,9 +227,17 @@ export class ModuleGraph {
}
// multiple urls can map to the same module and id, make sure we register
// the url to the existing module in that case
else if (!this.urlToModuleMap.has(url)) {
this.urlToModuleMap.set(url, mod)
else {
if (!this.urlToModuleMap.has(url)) {
this.urlToModuleMap.set(url, mod)
}
// Also register the clean url to the module, so that we can short-circuit
// resolving the same url twice
if (!this.#rawUrlToModuleMap.has(cleanedUrl)) {
this.#rawUrlToModuleMap.set(cleanedUrl, mod)
}
}

return mod
}

Expand Down