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

improve module graph building performance #14350

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
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
60 changes: 46 additions & 14 deletions lib/ModuleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ModuleGraphModule {
this.profile = undefined;
/** @type {boolean} */
this.async = false;
/** @type {ModuleGraphConnection[]} */
this._unassignedConnections = undefined;
}
}

Expand Down Expand Up @@ -169,14 +171,21 @@ class ModuleGraph {
dependency.weak,
dependency.getCondition(this)
);
this._dependencyMap.set(dependency, connection);
const connections = this._getModuleGraphModule(module).incomingConnections;
connections.add(connection);
const mgm = this._getModuleGraphModule(originModule);
if (mgm.outgoingConnections === undefined) {
mgm.outgoingConnections = new Set();
if (originModule) {
const mgm = this._getModuleGraphModule(originModule);
if (mgm._unassignedConnections === undefined) {
mgm._unassignedConnections = [];
}
mgm._unassignedConnections.push(connection);
if (mgm.outgoingConnections === undefined) {
mgm.outgoingConnections = new Set();
}
mgm.outgoingConnections.add(connection);
} else {
this._dependencyMap.set(dependency, connection);
}
mgm.outgoingConnections.add(connection);
}

/**
Expand All @@ -185,7 +194,7 @@ class ModuleGraph {
* @returns {void}
*/
updateModule(dependency, module) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
if (connection.module === module) return;
const newConnection = connection.clone();
newConnection.module = module;
Expand All @@ -202,12 +211,12 @@ class ModuleGraph {
* @returns {void}
*/
removeConnection(dependency) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
const targetMgm = this._getModuleGraphModule(connection.module);
targetMgm.incomingConnections.delete(connection);
const originMgm = this._getModuleGraphModule(connection.originModule);
originMgm.outgoingConnections.delete(connection);
this._dependencyMap.delete(dependency);
this._dependencyMap.set(dependency, null);
}

/**
Expand All @@ -216,7 +225,7 @@ class ModuleGraph {
* @returns {void}
*/
addExplanation(dependency, explanation) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
connection.addExplanation(explanation);
}

Expand Down Expand Up @@ -342,7 +351,7 @@ class ModuleGraph {
* @returns {Module} the referenced module
*/
getResolvedModule(dependency) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
return connection !== undefined ? connection.resolvedModule : null;
}

Expand All @@ -352,15 +361,38 @@ class ModuleGraph {
*/
getConnection(dependency) {
const connection = this._dependencyMap.get(dependency);
return connection;
if (connection === undefined) {
const module = this.getParentModule(dependency);
if (module !== undefined) {
const mgm = this._getModuleGraphModule(module);
if (
mgm._unassignedConnections &&
mgm._unassignedConnections.length !== 0
) {
let foundConnection;
for (const connection of mgm._unassignedConnections) {
this._dependencyMap.set(connection.dependency, connection);
if (connection.dependency === dependency)
foundConnection = connection;
}
mgm._unassignedConnections.length = 0;
if (foundConnection !== undefined) {
return foundConnection;
}
}
}
this._dependencyMap.set(dependency, null);
return undefined;
}
return connection === null ? undefined : connection;
}

/**
* @param {Dependency} dependency the dependency to look for a referenced module
* @returns {Module} the referenced module
*/
getModule(dependency) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
return connection !== undefined ? connection.module : null;
}

Expand All @@ -369,7 +401,7 @@ class ModuleGraph {
* @returns {Module} the referencing module
*/
getOrigin(dependency) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
return connection !== undefined ? connection.originModule : null;
}

Expand All @@ -378,7 +410,7 @@ class ModuleGraph {
* @returns {Module} the original referencing module
*/
getResolvedOrigin(dependency) {
const connection = this._dependencyMap.get(dependency);
const connection = this.getConnection(dependency);
return connection !== undefined ? connection.resolvedOriginModule : null;
}

Expand Down
20 changes: 11 additions & 9 deletions lib/dependencies/CommonJsExportRequireDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,17 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency
weak: dep.weak,
runtimeRequirements
});
const ids = dep.getIds(moduleGraph);
const usedImported = moduleGraph
.getExportsInfo(importedModule)
.getUsedName(ids, runtime);
if (usedImported) {
const comment = equals(usedImported, ids)
? ""
: Template.toNormalComment(propertyAccess(ids)) + " ";
requireExpr += `${comment}${propertyAccess(usedImported)}`;
if (importedModule) {
const ids = dep.getIds(moduleGraph);
const usedImported = moduleGraph
.getExportsInfo(importedModule)
.getUsedName(ids, runtime);
if (usedImported) {
const comment = equals(usedImported, ids)
? ""
: Template.toNormalComment(propertyAccess(ids)) + " ";
requireExpr += `${comment}${propertyAccess(usedImported)}`;
}
}

switch (type) {
Expand Down
20 changes: 11 additions & 9 deletions lib/dependencies/CommonJsFullRequireDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
weak: dep.weak,
runtimeRequirements
});
const ids = dep.names;
const usedImported = moduleGraph
.getExportsInfo(importedModule)
.getUsedName(ids, runtime);
if (usedImported) {
const comment = equals(usedImported, ids)
? ""
: Template.toNormalComment(propertyAccess(ids)) + " ";
requireExpr += `${comment}${propertyAccess(usedImported)}`;
if (importedModule) {
const ids = dep.names;
const usedImported = moduleGraph
.getExportsInfo(importedModule)
.getUsedName(ids, runtime);
if (usedImported) {
const comment = equals(usedImported, ids)
? ""
: Template.toNormalComment(propertyAccess(ids)) + " ";
requireExpr += `${comment}${propertyAccess(usedImported)}`;
}
}
source.replace(dep.range[0], dep.range[1] - 1, requireExpr);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/dependencies/HarmonyImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate extends
}

const importStatement = dep.getImportStatement(false, templateContext);
if (templateContext.moduleGraph.isAsync(referencedModule)) {
if (
referencedModule &&
templateContext.moduleGraph.isAsync(referencedModule)
) {
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[0],
Expand Down