move Module.index and index2 into ModuleGraph

This commit is contained in:
Tobias Koppers 2018-08-21 16:12:00 +02:00
parent 4dfe88edb0
commit 42167db4af
8 changed files with 170 additions and 33 deletions

View File

@ -1056,6 +1056,7 @@ class Compilation {
for (const module of this.modules) {
module.unseal();
}
this.moduleGraph.removeAllModuleAttributes();
}
/**
@ -1381,6 +1382,8 @@ class Compilation {
// eachother and Blocks with Chunks. It stops traversing when all modules
// for a chunk are already available. So it doesn't connect unneeded chunks.
const moduleGraph = this.moduleGraph;
/** @type {Map<ChunkGroup, {block: AsyncDependenciesBlock, chunkGroup: ChunkGroup}[]>} */
const chunkDependencies = new Map();
/** @type {Set<ChunkGroup>} */
@ -1467,8 +1470,8 @@ class Compilation {
chunkGroupCounters.set(chunkGroup, { index: 0, index2: 0 });
}
let nextFreeModuleIndex = 0;
let nextFreeModuleIndex2 = 0;
let nextFreeModulePreOrderIndex = 0;
let nextFreeModulePostOrderIndex = 0;
/** @type {Map<DependenciesBlock, ChunkGroup>} */
const blockChunkGroups = new Map();
@ -1605,8 +1608,13 @@ class Compilation {
}
}
if (module.index === null) {
module.index = nextFreeModuleIndex++;
if (
moduleGraph.setPreOrderIndexIfUnset(
module,
nextFreeModulePreOrderIndex
)
) {
nextFreeModulePreOrderIndex++;
}
queue.push({
@ -1659,8 +1667,13 @@ class Compilation {
}
}
if (module.index2 === null) {
module.index2 = nextFreeModuleIndex2++;
if (
moduleGraph.setPostOrderIndexIfUnset(
module,
nextFreeModulePostOrderIndex
)
) {
nextFreeModulePostOrderIndex++;
}
break;
}

View File

@ -103,10 +103,6 @@ class Module extends DependenciesBlock {
/** @type {number|string} */
this.id = null;
/** @type {number} */
this.index = null;
/** @type {number} */
this.index2 = null;
/** @type {number} */
this.depth = null;
/** @type {undefined | object} */
this.profile = undefined;
@ -119,6 +115,14 @@ class Module extends DependenciesBlock {
this.useSourceMap = false;
}
get index() {
throw new Error();
}
get index2() {
throw new Error();
}
/**
* @deprecated moved to .buildInfo.exportsArgument
* @returns {string} name of the exports argument
@ -144,8 +148,6 @@ class Module extends DependenciesBlock {
this.renderedHash = undefined;
this.id = null;
this.index = null;
this.index2 = null;
this.depth = null;
this.profile = undefined;
this.prefetched = false;
@ -159,8 +161,6 @@ class Module extends DependenciesBlock {
*/
unseal() {
this.id = null;
this.index = null;
this.index2 = null;
this.depth = null;
super.unseal();
}

View File

@ -27,6 +27,10 @@ class ModuleGraphModule {
this.optimizationBailout = [];
/** @type {false | true | SortableSet<string> | null} */
this.usedExports = null;
/** @type {number} */
this.preOrderIndex = null;
/** @type {number} */
this.postOrderIndex = null;
}
}
@ -155,16 +159,51 @@ class ModuleGraph {
connection.addExplanation(explanation);
}
/**
* @param {Module} oldModule the old module
* @param {Module} newModule the new module
* @returns {void}
*/
moveModuleAttributes(oldModule, newModule) {
const oldMgm = this._getModuleGraphModule(oldModule);
const newMgm = this._getModuleGraphModule(newModule);
newMgm.postOrderIndex = oldMgm.postOrderIndex;
newMgm.preOrderIndex = oldMgm.preOrderIndex;
oldMgm.postOrderIndex = null;
oldMgm.preOrderIndex = null;
}
/**
* @param {Module} module the module
* @returns {void}
*/
removeModuleAttributes(module) {
const mgm = this._getModuleGraphModule(module);
mgm.postOrderIndex = null;
mgm.preOrderIndex = null;
}
/**
* @returns {void}
*/
removeAllModuleAttributes() {
for (const mgm of this._moduleMap.values()) {
mgm.postOrderIndex = null;
mgm.preOrderIndex = null;
}
}
/**
* @param {Module} oldModule the old referencing module
* @param {Module} newModule the new referencing module
* @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
* @returns {void}
*/
replaceModule(oldModule, newModule, filterConnection) {
moveModuleConnections(oldModule, newModule, filterConnection) {
if (oldModule === newModule) return;
const oldMgm = this._getModuleGraphModule(oldModule);
const newMgm = this._getModuleGraphModule(newModule);
// Outgoing connections
const oldConnections = oldMgm.outgoingConnections;
const newConnections = newMgm.outgoingConnections;
for (const connection of oldConnections) {
@ -174,6 +213,7 @@ class ModuleGraph {
oldConnections.delete(connection);
}
}
// Incoming connections
const oldConnections2 = oldMgm.incomingConnections;
const newConnections2 = newMgm.incomingConnections;
for (const connection of oldConnections2) {
@ -310,6 +350,72 @@ class ModuleGraph {
mgm.usedExports = usedExports;
}
/**
* @param {Module} module the module
* @returns {number} the index of the module
*/
getPreOrderIndex(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.preOrderIndex;
}
/**
* @param {Module} module the module
* @returns {number} the index of the module
*/
getPostOrderIndex(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.postOrderIndex;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {void}
*/
setPreOrderIndex(module, index) {
const mgm = this._getModuleGraphModule(module);
mgm.preOrderIndex = index;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {boolean} true, if the index was set
*/
setPreOrderIndexIfUnset(module, index) {
const mgm = this._getModuleGraphModule(module);
if (mgm.preOrderIndex === null) {
mgm.preOrderIndex = index;
return true;
}
return false;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {void}
*/
setPostOrderIndex(module, index) {
const mgm = this._getModuleGraphModule(module);
mgm.postOrderIndex = index;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {boolean} true, if the index was set
*/
setPostOrderIndexIfUnset(module, index) {
const mgm = this._getModuleGraphModule(module);
if (mgm.postOrderIndex === null) {
mgm.postOrderIndex = index;
return true;
}
return false;
}
/**
* @param {any} thing any thing
* @returns {Object} metadata

View File

@ -518,8 +518,10 @@ class Stats {
id: module.id,
identifier: module.identifier(),
name: module.readableIdentifier(requestShortener),
index: module.index,
index2: module.index2,
index: moduleGraph.getPreOrderIndex(module),
preOrderIndex: moduleGraph.getPreOrderIndex(module),
index2: moduleGraph.getPostOrderIndex(module),
postOrderIndex: moduleGraph.getPostOrderIndex(module),
size: module.size(),
cacheable: module.buildInfo.cacheable,
built: !!module.built,

View File

@ -321,8 +321,6 @@ class ConcatenatedModule extends Module {
this.factoryMeta = rootModule.factoryMeta;
// Info from Compilation
this.index = rootModule.index;
this.index2 = rootModule.index2;
this.depth = rootModule.depth;
// Info from Optimization

View File

@ -336,12 +336,15 @@ class ModuleConcatenationPlugin {
.getOptimizationBailout(newModule)
.push(formatBailoutWarning(warning[0], warning[1]));
}
moduleGraph.moveModuleAttributes(rootModule, newModule);
for (const m of modules) {
usedModules.add(m);
// remove attributes from module
moduleGraph.removeModuleAttributes(m);
// remove module from chunk
chunkGraph.replaceModule(m, newModule);
// replace module references with the concatenated module
moduleGraph.replaceModule(m, newModule, c => {
moduleGraph.moveModuleConnections(m, newModule, c => {
return !(
c.dependency instanceof HarmonyImportDependency &&
modules.has(c.originModule) &&

View File

@ -81,7 +81,10 @@ const compareNumbers = (a, b) => {
* @returns {-1|0|1} compare result
*/
const compareModulesByIndex = (moduleGraph, a, b) => {
return compareNumbers(a.index, b.index);
return compareNumbers(
moduleGraph.getPreOrderIndex(a),
moduleGraph.getPreOrderIndex(b)
);
};
/** @type {ParamizedComparator<ModuleGraph, Module>} */
exports.compareModulesByIndex = createCachedParamizedComparator(
@ -95,7 +98,10 @@ exports.compareModulesByIndex = createCachedParamizedComparator(
* @returns {-1|0|1} compare result
*/
const compareModulesByIndex2 = (moduleGraph, a, b) => {
return compareNumbers(a.index2, b.index2);
return compareNumbers(
moduleGraph.getPostOrderIndex(a),
moduleGraph.getPostOrderIndex(b)
);
};
/** @type {ParamizedComparator<ModuleGraph, Module>} */
exports.compareModulesByIndex2 = createCachedParamizedComparator(
@ -109,13 +115,13 @@ exports.compareModulesByIndex2 = createCachedParamizedComparator(
* @returns {-1|0|1} compare result
*/
const compareModulesByIndexOrIdentifier = (moduleGraph, a, b) => {
if (a.index < b.index) return -1;
if (a.index > b.index) return 1;
const identA = a.identifier();
const identB = b.identifier();
if (identA < identB) return -1;
if (identA > identB) return 1;
return 0;
const cmp1 = compareNumbers(
moduleGraph.getPreOrderIndex(a),
moduleGraph.getPreOrderIndex(b)
);
if (cmp1 !== 0) return cmp1;
const cmp2 = compareIds(a.identifier(), b.identifier());
return cmp2;
};
/** @type {ParamizedComparator<ModuleGraph, Module>} */
exports.compareModulesByIndexOrIdentifier = createCachedParamizedComparator(

View File

@ -16,6 +16,7 @@ module.exports = {
* @returns {void}
*/
const handler = compilation => {
const moduleGraph = compilation.moduleGraph;
compilation.hooks.afterSeal.tap("testcase", () => {
const data = {};
for (const [name, group] of compilation.namedChunkGroups) {
@ -69,20 +70,28 @@ module.exports = {
});
const indicies = compilation.modules
.slice()
.sort((a, b) => a.index - b.index)
.sort(
(a, b) =>
moduleGraph.getPreOrderIndex(a) -
moduleGraph.getPreOrderIndex(b)
)
.map(
m =>
`${m.index}: ${m.readableIdentifier(
`${moduleGraph.getPreOrderIndex(m)}: ${m.readableIdentifier(
compilation.requestShortener
)}`
)
.join(", ");
const indicies2 = compilation.modules
.slice()
.sort((a, b) => a.index2 - b.index2)
.sort(
(a, b) =>
moduleGraph.getPostOrderIndex(a) -
moduleGraph.getPostOrderIndex(b)
)
.map(
m =>
`${m.index2}: ${m.readableIdentifier(
`${moduleGraph.getPostOrderIndex(m)}: ${m.readableIdentifier(
compilation.requestShortener
)}`
)