-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split cutout and repair step in make (#6364)
- Loading branch information
1 parent
b46aeb1
commit 5b8dd1d
Showing
16 changed files
with
630 additions
and
642 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
crates/rspack_core/src/compiler/make/cutout/clean_isolated_module.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::collections::VecDeque; | ||
|
||
use rustc_hash::FxHashSet as HashSet; | ||
|
||
use super::super::MakeArtifact; | ||
use crate::ModuleIdentifier; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct CleanIsolatedModule { | ||
need_check_isolated_module_ids: HashSet<ModuleIdentifier>, | ||
} | ||
|
||
impl CleanIsolatedModule { | ||
pub fn analyze_force_build_module( | ||
&mut self, | ||
artifact: &MakeArtifact, | ||
module_identifier: &ModuleIdentifier, | ||
) { | ||
let module_graph = artifact.get_module_graph(); | ||
for connection in module_graph.get_outgoing_connections(module_identifier) { | ||
self | ||
.need_check_isolated_module_ids | ||
.insert(*connection.module_identifier()); | ||
} | ||
} | ||
|
||
pub fn fix_artifact(self, artifact: &mut MakeArtifact) { | ||
let mut module_graph = artifact.get_module_graph_mut(); | ||
let mut queue = VecDeque::from( | ||
self | ||
.need_check_isolated_module_ids | ||
.into_iter() | ||
.collect::<Vec<_>>(), | ||
); | ||
while let Some(module_identifier) = queue.pop_front() { | ||
let Some(mgm) = module_graph.module_graph_module_by_identifier(&module_identifier) else { | ||
tracing::trace!("Module is cleaned: {}", module_identifier); | ||
continue; | ||
}; | ||
if !mgm.incoming_connections().is_empty() { | ||
tracing::trace!("Module is used: {}", module_identifier); | ||
continue; | ||
} | ||
|
||
for connection in module_graph.get_outgoing_connections(&module_identifier) { | ||
// clean child module | ||
queue.push_back(*connection.module_identifier()); | ||
} | ||
module_graph.revoke_module(&module_identifier); | ||
tracing::trace!("Module is cleaned: {}", module_identifier); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
crates/rspack_core/src/compiler/make/cutout/fix_issuers.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use rustc_hash::FxHashMap as HashMap; | ||
|
||
use super::super::MakeArtifact; | ||
use crate::{ModuleIdentifier, ModuleIssuer}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct FixIssuers { | ||
origin_module_issuers: HashMap<ModuleIdentifier, ModuleIssuer>, | ||
} | ||
|
||
impl FixIssuers { | ||
pub fn analyze_force_build_module( | ||
&mut self, | ||
artifact: &MakeArtifact, | ||
module_identifier: &ModuleIdentifier, | ||
) { | ||
let module_graph = artifact.get_module_graph(); | ||
let mgm = module_graph | ||
.module_graph_module_by_identifier(module_identifier) | ||
.expect("should have module graph module"); | ||
self | ||
.origin_module_issuers | ||
.insert(*module_identifier, mgm.get_issuer().clone()); | ||
} | ||
|
||
pub fn fix_artifact(self, artifact: &mut MakeArtifact) { | ||
let mut module_graph = artifact.get_module_graph_mut(); | ||
for (id, issuer) in self.origin_module_issuers.into_iter() { | ||
if let Some(mgm) = module_graph.module_graph_module_by_identifier_mut(&id) { | ||
mgm.set_issuer(issuer); | ||
} | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; | ||
|
||
use super::super::MakeArtifact; | ||
use crate::{AsyncDependenciesBlockIdentifier, GroupOptions, ModuleGraph, ModuleIdentifier}; | ||
|
||
#[derive(Debug, Default, Eq, PartialEq)] | ||
struct ModuleDeps { | ||
// child module identifier of current module | ||
child_modules: HashSet<ModuleIdentifier>, | ||
// blocks in current module | ||
module_blocks: HashSet<(AsyncDependenciesBlockIdentifier, Option<GroupOptions>)>, | ||
} | ||
|
||
impl ModuleDeps { | ||
fn from_module(module_graph: &ModuleGraph, module_identifier: &ModuleIdentifier) -> Self { | ||
let mut res = Self::default(); | ||
for connection in module_graph.get_outgoing_connections(module_identifier) { | ||
res.child_modules.insert(*connection.module_identifier()); | ||
} | ||
let module = module_graph | ||
.module_by_identifier(module_identifier) | ||
.expect("should have module"); | ||
for block_id in module.get_blocks() { | ||
let block = module_graph | ||
.block_by_id(block_id) | ||
.expect("should have block"); | ||
res | ||
.module_blocks | ||
.insert((*block_id, block.get_group_options().cloned())); | ||
} | ||
|
||
res | ||
} | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct HasModuleGraphChange { | ||
origin_module_deps: HashMap<ModuleIdentifier, ModuleDeps>, | ||
} | ||
|
||
impl HasModuleGraphChange { | ||
pub fn analyze_force_build_module( | ||
&mut self, | ||
artifact: &MakeArtifact, | ||
module_identifier: &ModuleIdentifier, | ||
) { | ||
let module_graph = &artifact.get_module_graph(); | ||
self.origin_module_deps.insert( | ||
*module_identifier, | ||
ModuleDeps::from_module(module_graph, module_identifier), | ||
); | ||
} | ||
|
||
pub fn fix_artifact(self, artifact: &mut MakeArtifact) { | ||
let module_graph = &artifact.get_module_graph(); | ||
if self.origin_module_deps.is_empty() { | ||
// origin_module_deps empty means no force_build_module and no file changed | ||
// this only happens when build from entry | ||
artifact.has_module_graph_change = true; | ||
return; | ||
} | ||
// if artifact.has_module_graph_change is true, no need to recalculate | ||
if !artifact.has_module_graph_change { | ||
for (module_identifier, module_deps) in self.origin_module_deps { | ||
if module_graph | ||
.module_by_identifier(&module_identifier) | ||
.is_none() | ||
|| ModuleDeps::from_module(module_graph, &module_identifier) != module_deps | ||
{ | ||
artifact.has_module_graph_change = true; | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
mod clean_isolated_module; | ||
mod fix_issuers; | ||
mod has_module_graph_change; | ||
|
||
use rustc_hash::FxHashSet as HashSet; | ||
|
||
use self::{ | ||
clean_isolated_module::CleanIsolatedModule, fix_issuers::FixIssuers, | ||
has_module_graph_change::HasModuleGraphChange, | ||
}; | ||
use super::{MakeArtifact, MakeParam}; | ||
use crate::BuildDependency; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Cutout { | ||
fix_issuers: FixIssuers, | ||
clean_isolated_module: CleanIsolatedModule, | ||
has_module_graph_change: HasModuleGraphChange, | ||
} | ||
|
||
impl Cutout { | ||
pub fn cutout_artifact( | ||
&mut self, | ||
artifact: &mut MakeArtifact, | ||
params: Vec<MakeParam>, | ||
) -> HashSet<BuildDependency> { | ||
let module_graph = artifact.get_module_graph(); | ||
|
||
let mut force_build_modules = HashSet::default(); | ||
let mut force_build_deps = HashSet::default(); | ||
|
||
for item in params { | ||
match item { | ||
MakeParam::ModifiedFiles(files) => { | ||
force_build_modules.extend(module_graph.modules().values().filter_map(|module| { | ||
// check has dependencies modified | ||
if !module.is_available(&files) { | ||
Some(module.identifier()) | ||
} else { | ||
None | ||
} | ||
})) | ||
} | ||
MakeParam::DeletedFiles(files) => { | ||
force_build_modules.extend(module_graph.modules().values().flat_map(|module| { | ||
let mut res = vec![]; | ||
|
||
// check has dependencies modified | ||
if !module.is_available(&files) { | ||
// add module id | ||
res.push(module.identifier()); | ||
// add parent module id | ||
res.extend( | ||
module_graph | ||
.get_incoming_connections(&module.identifier()) | ||
.iter() | ||
.filter_map(|connect| connect.original_module_identifier), | ||
) | ||
} | ||
res | ||
})) | ||
} | ||
MakeParam::ForceBuildDeps(deps) => { | ||
for item in deps { | ||
let (dependency_id, _) = &item; | ||
// add deps bindings module to force_build_modules | ||
if let Some(mid) = module_graph.module_identifier_by_dependency_id(dependency_id) { | ||
force_build_modules.insert(*mid); | ||
} | ||
force_build_deps.insert(item); | ||
} | ||
} | ||
MakeParam::ForceBuildModules(modules) => { | ||
force_build_modules.extend(modules); | ||
} | ||
}; | ||
} | ||
|
||
for module_identifier in &force_build_modules { | ||
self | ||
.fix_issuers | ||
.analyze_force_build_module(artifact, module_identifier); | ||
self | ||
.clean_isolated_module | ||
.analyze_force_build_module(artifact, module_identifier); | ||
self | ||
.has_module_graph_change | ||
.analyze_force_build_module(artifact, module_identifier); | ||
} | ||
|
||
let mut module_graph = artifact.get_module_graph_mut(); | ||
// do revoke module and collect deps | ||
force_build_deps.extend( | ||
force_build_modules | ||
.iter() | ||
.flat_map(|id| module_graph.revoke_module(id)), | ||
); | ||
|
||
force_build_deps | ||
} | ||
|
||
pub fn fix_artifact(self, artifact: &mut MakeArtifact) { | ||
let Self { | ||
fix_issuers, | ||
clean_isolated_module, | ||
has_module_graph_change, | ||
} = self; | ||
fix_issuers.fix_artifact(artifact); | ||
clean_isolated_module.fix_artifact(artifact); | ||
has_module_graph_change.fix_artifact(artifact); | ||
} | ||
} |
Oops, something went wrong.
5b8dd1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open
5b8dd1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open