Skip to content

Commit

Permalink
lazy assign connections to dependencies
Browse files Browse the repository at this point in the history
in many cases we don't need the assignment
  • Loading branch information
sokra committed Sep 28, 2021
1 parent 449f7ef commit daa2c38
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions lib/ModuleGraph.js
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) {

This comment has been minimized.

Copy link
@liuwenzhuang

liuwenzhuang Dec 13, 2021

Before this commit, the root node of module tree could be extracted by moduleGraph._moduleMap.get(null), so I wonder how to get the module tree's root node after this commit?

This comment has been minimized.

Copy link
@sokra

sokra Dec 13, 2021

Author Member

_moduleMap is private, you shouldn't access that anyway.

There could be multiple roots.

Best you look up the entrypoints from compilation.entries

This comment has been minimized.

Copy link
@liuwenzhuang

liuwenzhuang Dec 15, 2021

Appreciate your reply. 👍

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

0 comments on commit daa2c38

Please sign in to comment.