optimize SideEffectsFlagPlugin performance

This commit is contained in:
Tobias Koppers 2020-01-30 11:59:34 +01:00
parent 8445f06fa9
commit e6113e9787
3 changed files with 26 additions and 24 deletions

View File

@ -15,7 +15,7 @@ class ModuleGraphConnection {
* @param {Module} module the referenced module * @param {Module} module the referenced module
* @param {string=} explanation some extra detail * @param {string=} explanation some extra detail
* @param {boolean=} weak the reference is weak * @param {boolean=} weak the reference is weak
* @param {function(): boolean=} condition condition for the connection * @param {function(ModuleGraphConnection): boolean=} condition condition for the connection
*/ */
constructor( constructor(
originModule, originModule,
@ -33,7 +33,7 @@ class ModuleGraphConnection {
this.weak = weak; this.weak = weak;
this.conditional = !!condition; this.conditional = !!condition;
this._active = true; this._active = true;
/** @type {function(): boolean} */ /** @type {function(ModuleGraphConnection): boolean} */
this.condition = condition; this.condition = condition;
/** @type {Set<string>} */ /** @type {Set<string>} */
this.explanations = undefined; this.explanations = undefined;
@ -44,13 +44,13 @@ class ModuleGraphConnection {
} }
/** /**
* @param {function(): boolean} condition condition for the connection * @param {function(ModuleGraphConnection): boolean} condition condition for the connection
* @returns {void} * @returns {void}
*/ */
addCondition(condition) { addCondition(condition) {
if (this.conditional) { if (this.conditional) {
const old = this.condition; const old = this.condition;
this.condition = () => old() && condition(); this.condition = c => old(c) && condition(c);
} else if (this._active) { } else if (this._active) {
this.conditional = true; this.conditional = true;
this.condition = condition; this.condition = condition;
@ -75,7 +75,7 @@ class ModuleGraphConnection {
get active() { get active() {
if (!this.conditional) return this._active; if (!this.conditional) return this._active;
return this.condition(); return this.condition(this);
} }
set active(value) { set active(value) {

View File

@ -23,6 +23,21 @@ class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
get type() { get type() {
return "harmony side effect evaluation"; return "harmony side effect evaluation";
} }
/**
* @param {ModuleGraph} moduleGraph module graph
* @returns {function(): boolean} function to determine if the connection is active
*/
getCondition(moduleGraph) {
return connection => {
const refModule = connection.resolvedModule;
return (
!refModule ||
refModule.factoryMeta === undefined ||
!refModule.factoryMeta.sideEffectFree
);
};
}
} }
makeSerializable( makeSerializable(

View File

@ -8,7 +8,6 @@
const glob2regexp = require("glob-to-regexp"); const glob2regexp = require("glob-to-regexp");
const { STAGE_DEFAULT } = require("../OptimizationStages"); const { STAGE_DEFAULT } = require("../OptimizationStages");
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency"); const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency"); const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
/** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Compiler")} Compiler */
@ -104,12 +103,12 @@ class SideEffectsFlagPlugin {
// Capture reexports of sideEffectFree modules // Capture reexports of sideEffectFree modules
for (const module of modules) { for (const module of modules) {
for (const dep of module.dependencies) {
if (dep instanceof HarmonyExportImportedSpecifierDependency) {
if ( if (
module.factoryMeta !== undefined && module.factoryMeta !== undefined &&
module.factoryMeta.sideEffectFree module.factoryMeta.sideEffectFree
) { ) {
for (const dep of module.dependencies) {
if (dep instanceof HarmonyExportImportedSpecifierDependency) {
const mode = dep.getMode(moduleGraph, true); const mode = dep.getMode(moduleGraph, true);
if (mode.type === "normal-reexport") { if (mode.type === "normal-reexport") {
let map = reexportMaps.get(module); let map = reexportMaps.get(module);
@ -127,18 +126,6 @@ class SideEffectsFlagPlugin {
} }
} }
} }
} else if (dep instanceof HarmonyImportSideEffectDependency) {
const connection = moduleGraph.getConnection(dep);
if (connection) {
const refModule = connection.resolvedModule;
if (
refModule &&
refModule.factoryMeta !== undefined &&
refModule.factoryMeta.sideEffectFree
) {
connection.active = false;
}
}
} }
} }
} }