Skip to content

Commit bce8a78

Browse files
committed
Change ModuleGraph API to iterator
1 parent 22f282a commit bce8a78

File tree

31 files changed

+97
-86
lines changed

31 files changed

+97
-86
lines changed

crates/rspack_binding_api/src/compilation/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ impl JsCompilation {
187187
let compilation = self.as_ref()?;
188188
let module_graph = compilation.get_module_graph();
189189
let modules = module_graph.modules();
190-
let mut arr = env.create_array(modules.len() as u32)?;
191-
for (i, identifier) in modules.keys().enumerate() {
190+
let mut arr = env.create_array(module_graph.module_count() as u32)?;
191+
for (i, (identifier, _)) in modules.enumerate() {
192192
arr.set(
193193
i as u32,
194194
compilation
195-
.module_by_identifier(identifier)
195+
.module_by_identifier(&identifier)
196196
.map(|module| ModuleObject::with_ref(module.as_ref(), compilation.compiler_id())),
197197
)?;
198198
}

crates/rspack_core/src/cache/persistent/occasion/make/module_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ pub async fn recovery_module_graph(
200200
mg.cache_recovery_connection(connection);
201201
}
202202

203-
tracing::debug!("recovery {} module", mg.modules().len());
203+
tracing::debug!("recovery {} module", mg.module_count());
204204
Ok((mg, module_to_lazy_make, entry_dependencies))
205205
}

crates/rspack_core/src/compilation/build_chunk_graph/incremental.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ impl CodeSplitter {
504504
// Thanks!
505505
let module_graph = compilation.get_module_graph();
506506
let ordinal_by_module = &mut self.ordinal_by_module;
507-
for m in module_graph.modules().keys() {
508-
if !ordinal_by_module.contains_key(m) {
509-
ordinal_by_module.insert(*m, ordinal_by_module.len() as u64 + 1);
507+
for (m, _) in module_graph.modules() {
508+
if !ordinal_by_module.contains_key(&m) {
509+
ordinal_by_module.insert(m, ordinal_by_module.len() as u64 + 1);
510510
}
511511
}
512512
for chunk in compilation.build_chunk_graph_artifact.chunk_by_ukey.keys() {
@@ -551,8 +551,7 @@ impl CodeSplitter {
551551
compilation
552552
.get_module_graph()
553553
.modules()
554-
.keys()
555-
.copied()
554+
.map(|(module_identifier, _)| module_identifier)
556555
.collect(),
557556
Default::default(),
558557
)

crates/rspack_core/src/compilation/build_chunk_graph/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ pub fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<
2525
let all_modules = compilation
2626
.get_module_graph()
2727
.modules()
28-
.keys()
29-
.copied()
28+
.map(|(module_identifier, _)| module_identifier)
3029
.collect::<Vec<_>>();
3130

3231
splitter.prepare(&all_modules, compilation)?;

crates/rspack_core/src/compilation/build_module_graph/graph_updater/cutout/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Cutout {
5050
clean_entry_dependencies = true;
5151
}
5252
UpdateParam::CheckNeedBuild => {
53-
force_build_modules.extend(module_graph.modules().values().filter_map(|module| {
53+
force_build_modules.extend(module_graph.modules().filter_map(|(_, module)| {
5454
if module.need_build(&compilation.value_cache_versions) {
5555
Some(module.identifier())
5656
} else {

crates/rspack_core/src/compilation/code_generation/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,15 @@ async fn code_generation_pass_impl(compilation: &mut Compilation) -> Result<()>
5555
logger.log(format!(
5656
"{} modules are affected, {} in total",
5757
modules.len(),
58-
compilation.get_module_graph().modules().len()
58+
compilation.get_module_graph().module_count()
5959
));
6060
modules
6161
} else {
6262
*compilation.code_generation_results = Default::default();
6363
compilation
6464
.get_module_graph()
6565
.modules()
66-
.keys()
67-
.copied()
66+
.map(|(module_identifier, _)| module_identifier)
6867
.collect()
6968
};
7069
compilation.code_generation(code_generation_modules).await?;

crates/rspack_core/src/compilation/create_module_hashes/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ async fn create_module_hashes_pass_impl(compilation: &mut Compilation) -> Result
4949

5050
// check if module runtime changes
5151
let mg = compilation.get_module_graph();
52-
for mi in mg.modules().keys() {
52+
for (mi, _) in mg.modules() {
5353
let module_runtimes = compilation
5454
.build_chunk_graph_artifact
5555
.chunk_graph
56-
.get_module_runtimes(*mi, &compilation.build_chunk_graph_artifact.chunk_by_ukey);
56+
.get_module_runtimes(mi, &compilation.build_chunk_graph_artifact.chunk_by_ukey);
5757
let module_runtime_keys = module_runtimes
5858
.values()
5959
.map(get_runtime_key)
6060
.collect::<HashSet<_>>();
6161

62-
if let Some(runtime_map) = compilation.cgm_hash_artifact.get_runtime_map(mi) {
62+
if let Some(runtime_map) = compilation.cgm_hash_artifact.get_runtime_map(&mi) {
6363
if module_runtimes.is_empty() {
6464
// module has no runtime, skip
6565
continue;
@@ -76,23 +76,23 @@ async fn create_module_hashes_pass_impl(compilation: &mut Compilation) -> Result
7676
.next()
7777
.expect("should have at least one runtime")
7878
{
79-
modules.insert(*mi);
79+
modules.insert(mi);
8080
}
8181
} else {
8282
// multiple runtimes
8383
if matches!(runtime_map.mode, RuntimeMode::SingleEntry) {
84-
modules.insert(*mi);
84+
modules.insert(mi);
8585
continue;
8686
}
8787

8888
if runtime_map.map.len() != module_runtimes.len() {
89-
modules.insert(*mi);
89+
modules.insert(mi);
9090
continue;
9191
}
9292

9393
for runtime_key in runtime_map.map.keys() {
9494
if !module_runtime_keys.contains(runtime_key) {
95-
modules.insert(*mi);
95+
modules.insert(mi);
9696
break;
9797
}
9898
}
@@ -105,16 +105,15 @@ async fn create_module_hashes_pass_impl(compilation: &mut Compilation) -> Result
105105
logger.log(format!(
106106
"{} modules are affected, {} in total",
107107
modules.len(),
108-
mg.modules().len()
108+
mg.module_count()
109109
));
110110

111111
modules
112112
} else {
113113
compilation
114114
.get_module_graph()
115115
.modules()
116-
.keys()
117-
.copied()
116+
.map(|(module_identifier, _)| module_identifier)
118117
.collect()
119118
};
120119
compilation

crates/rspack_core/src/compilation/finish_modules/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,26 @@ impl Compilation {
120120
logger.log(format!(
121121
"{} modules are affected, {} in total",
122122
modules.len(),
123-
self.get_module_graph().modules().len()
123+
self.get_module_graph().module_count()
124124
));
125125
(modules, true)
126126
} else {
127127
(
128-
self.get_module_graph().modules().keys().copied().collect(),
128+
self
129+
.get_module_graph()
130+
.modules()
131+
.map(|(module_identifier, _)| module_identifier)
132+
.collect(),
129133
true,
130134
)
131135
}
132136
} else {
133137
(
134-
self.get_module_graph().modules().keys().copied().collect(),
138+
self
139+
.get_module_graph()
140+
.modules()
141+
.map(|(module_identifier, _)| module_identifier)
142+
.collect(),
135143
false,
136144
)
137145
}

crates/rspack_core/src/compilation/module_ids/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn get_modules_needing_ids(
1616
compilation
1717
.get_module_graph()
1818
.modules()
19-
.values()
19+
.map(|(_, module)| module)
2020
.filter(|m| {
2121
m.need_id()
2222
&& ChunkGraph::get_module_id(module_ids_artifact, m.identifier()).is_none()

crates/rspack_core/src/compilation/runtime_requirements/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async fn runtime_requirements_pass_impl(compilation: &mut Compilation) -> Result
5353
logger.log(format!(
5454
"{} modules are affected, {} in total",
5555
modules.len(),
56-
compilation.get_module_graph().modules().len()
56+
compilation.get_module_graph().module_count()
5757
));
5858
modules
5959
} else {
@@ -62,8 +62,7 @@ async fn runtime_requirements_pass_impl(compilation: &mut Compilation) -> Result
6262
compilation
6363
.get_module_graph()
6464
.modules()
65-
.keys()
66-
.copied()
65+
.map(|(module_identifier, _)| module_identifier)
6766
.collect()
6867
};
6968
compilation

0 commit comments

Comments
 (0)