Skip to content

Commit 711cb55

Browse files
authored
refactor: split module_graph into ModuleGraphRef and ModuleGraphMut (#12199)
* refactor: split module_graph into ModuleGraphRef and ModuleGraphMut * chore: fix check error
1 parent 7d4a1a3 commit 711cb55

File tree

12 files changed

+94
-60
lines changed

12 files changed

+94
-60
lines changed

crates/rspack_binding_api/src/exports_info.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::ptr::NonNull;
33
use napi::Either;
44
use napi_derive::napi;
55
use rspack_core::{
6-
Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraph, PrefetchExportsInfoMode, RuntimeSpec,
6+
Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraphMut, ModuleGraphRef,
7+
PrefetchExportsInfoMode, RuntimeSpec,
78
};
89
use rspack_util::atom::Atom;
910

@@ -24,13 +25,13 @@ impl JsExportsInfo {
2425
}
2526
}
2627

27-
fn as_ref(&self) -> napi::Result<ModuleGraph<'static>> {
28+
fn as_ref(&self) -> napi::Result<ModuleGraphRef<'static>> {
2829
let compilation = unsafe { self.compilation.as_ref() };
2930
let module_graph = compilation.get_module_graph();
3031
Ok(module_graph)
3132
}
3233

33-
fn as_mut(&mut self) -> napi::Result<ModuleGraph<'static>> {
34+
fn as_mut(&mut self) -> napi::Result<ModuleGraphMut<'static>> {
3435
let compilation = unsafe { self.compilation.as_mut() };
3536
let module_graph = compilation.get_module_graph_mut();
3637
Ok(module_graph)

crates/rspack_binding_api/src/module_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ptr::NonNull;
22

33
use napi::{Either, Env, JsString, bindgen_prelude::Array};
44
use napi_derive::napi;
5-
use rspack_core::{Compilation, ModuleGraph, PrefetchExportsInfoMode, RuntimeSpec};
5+
use rspack_core::{Compilation, ModuleGraph, ModuleGraphRef, PrefetchExportsInfoMode, RuntimeSpec};
66

77
use crate::{
88
dependencies::DependencyObject,
@@ -26,7 +26,7 @@ impl JsModuleGraph {
2626
}
2727
}
2828

29-
fn as_ref(&self) -> napi::Result<(&'static Compilation, ModuleGraph<'static>)> {
29+
fn as_ref(&self) -> napi::Result<(&'static Compilation, ModuleGraphRef<'static>)> {
3030
let compilation = unsafe { self.compilation.as_ref() };
3131
let module_graph = compilation.get_module_graph();
3232

crates/rspack_binding_api/src/module_graph_connection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cell::RefCell, ptr::NonNull};
33
use napi::bindgen_prelude::ToNapiValue;
44
use napi_derive::napi;
55
use rspack_collections::UkeyMap;
6-
use rspack_core::{Compilation, CompilationId, DependencyId, ModuleGraph};
6+
use rspack_core::{Compilation, CompilationId, DependencyId, ModuleGraphRef};
77
use rspack_napi::OneShotRef;
88

99
use crate::{dependency::DependencyWrapper, module::ModuleObject};
@@ -15,7 +15,7 @@ pub struct ModuleGraphConnection {
1515
}
1616

1717
impl ModuleGraphConnection {
18-
fn as_ref(&self) -> napi::Result<(&'static Compilation, ModuleGraph<'static>)> {
18+
fn as_ref(&self) -> napi::Result<(&'static Compilation, ModuleGraphRef<'static>)> {
1919
let compilation = unsafe { self.compilation.as_ref() };
2020
let module_graph = compilation.get_module_graph();
2121

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl MakeOccasion {
5151
need_update_modules.extend(affected_modules.active());
5252

5353
// The updated dependencies should be synced to persistent cache.
54-
let mg = ModuleGraph::new([Some(module_graph_partial), None], None);
54+
let mg = ModuleGraph::new_ref([Some(module_graph_partial), None]);
5555
for dep_id in affected_dependencies.updated() {
5656
if let Some(m) = mg.get_parent_module(dep_id) {
5757
need_update_modules.insert(*m);
@@ -74,7 +74,7 @@ impl MakeOccasion {
7474
module_graph::recovery_module_graph(&self.storage, &self.context).await?;
7575

7676
// regenerate statistical data
77-
let mg = ModuleGraph::new([Some(&partial), None], None);
77+
let mg = ModuleGraph::new_ref([Some(&partial), None]);
7878
// recovery make_failed_module
7979
let mut make_failed_module = IdentifierSet::default();
8080
// recovery *_dep

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn save_module_graph(
4343
storage: &Arc<dyn Storage>,
4444
context: &CacheableContext,
4545
) {
46-
let mg = ModuleGraph::new([Some(partial), None], None);
46+
let mg = ModuleGraph::new_ref([Some(partial), None]);
4747
for identifier in removed_modules {
4848
storage.remove(SCOPE, identifier.as_bytes());
4949
}
@@ -135,7 +135,7 @@ pub async fn recovery_module_graph(
135135
let mut need_check_dep = vec![];
136136
let mut partial = ModuleGraphPartial::default();
137137
let mut module_to_lazy_make = ModuleToLazyMake::default();
138-
let mut mg = ModuleGraph::new([None, None], Some(&mut partial));
138+
let mut mg = ModuleGraph::new_mut([None, None], &mut partial);
139139
storage
140140
.load(SCOPE)
141141
.await?

crates/rspack_core/src/compilation/build_module_graph/artifact.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use rspack_error::Diagnostic;
55
use rustc_hash::FxHashSet as HashSet;
66

77
use crate::{
8-
BuildDependency, DependencyId, FactorizeInfo, ModuleGraph, ModuleGraphPartial, ModuleIdentifier,
8+
BuildDependency, DependencyId, FactorizeInfo, ModuleGraph, ModuleGraphMut, ModuleGraphPartial,
9+
ModuleGraphRef, ModuleIdentifier,
910
compilation::build_module_graph::ModuleToLazyMake,
1011
incremental_info::IncrementalInfo,
1112
utils::{FileCounter, ResourceId},
@@ -68,11 +69,11 @@ pub struct BuildModuleGraphArtifact {
6869
}
6970

7071
impl BuildModuleGraphArtifact {
71-
pub fn get_module_graph(&self) -> ModuleGraph<'_> {
72-
ModuleGraph::new([Some(&self.module_graph_partial), None], None)
72+
pub fn get_module_graph(&self) -> ModuleGraphRef<'_> {
73+
ModuleGraph::new_ref([Some(&self.module_graph_partial), None])
7374
}
74-
pub fn get_module_graph_mut(&mut self) -> ModuleGraph<'_> {
75-
ModuleGraph::new([None, None], Some(&mut self.module_graph_partial))
75+
pub fn get_module_graph_mut(&mut self) -> ModuleGraphMut<'_> {
76+
ModuleGraph::new_mut([None, None], &mut self.module_graph_partial)
7677
}
7778
// TODO remove it
7879
pub fn get_module_graph_partial(&self) -> &ModuleGraphPartial {
@@ -87,7 +88,7 @@ impl BuildModuleGraphArtifact {
8788
///
8889
/// This function will update index on MakeArtifact.
8990
pub fn revoke_module(&mut self, module_identifier: &ModuleIdentifier) -> Vec<BuildDependency> {
90-
let mut mg = ModuleGraph::new([None, None], Some(&mut self.module_graph_partial));
91+
let mut mg = ModuleGraph::new_mut([None, None], &mut self.module_graph_partial);
9192
let module = mg
9293
.module_by_identifier(module_identifier)
9394
.expect("should have module");
@@ -150,7 +151,7 @@ impl BuildModuleGraphArtifact {
150151
pub fn revoke_dependency(&mut self, dep_id: &DependencyId, force: bool) -> Vec<BuildDependency> {
151152
self.make_failed_dependencies.remove(dep_id);
152153

153-
let mut mg = ModuleGraph::new([None, None], Some(&mut self.module_graph_partial));
154+
let mut mg = ModuleGraph::new_mut([None, None], &mut self.module_graph_partial);
154155
let revoke_dep_ids = if let Some(factorize_info) = mg
155156
.dependency_by_id_mut(dep_id)
156157
.and_then(FactorizeInfo::revoke)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl FixIssuers {
211211
mut force_build_module_issuers,
212212
need_check_modules,
213213
} = self;
214-
let mut module_graph = ModuleGraph::new([None, None], Some(&mut artifact.module_graph_partial));
214+
let mut module_graph = ModuleGraph::new_mut([None, None], &mut artifact.module_graph_partial);
215215
need_check_modules
216216
.into_iter()
217217
.filter_map(|mid| {
@@ -261,7 +261,7 @@ impl FixIssuers {
261261
queue.push_back((mid, parents));
262262
}
263263

264-
let mut module_graph = ModuleGraph::new([None, None], Some(&mut artifact.module_graph_partial));
264+
let mut module_graph = ModuleGraph::new_mut([None, None], &mut artifact.module_graph_partial);
265265
let mut revoke_module = IdentifierSet::default();
266266
let mut need_check_available_modules = IdentifierMap::default();
267267
loop {
@@ -353,7 +353,7 @@ impl FixIssuers {
353353
helper: &mut IssuerHelper,
354354
need_check_available_modules: IdentifierMap<Vec<Option<ModuleIdentifier>>>,
355355
) -> IdentifierMap<IdentifierSet> {
356-
let mut module_graph = ModuleGraph::new([None, None], Some(&mut artifact.module_graph_partial));
356+
let mut module_graph = ModuleGraph::new_mut([None, None], &mut artifact.module_graph_partial);
357357
let mut need_clean_cycle_modules: IdentifierMap<IdentifierMap<IdentifierSet>> =
358358
IdentifierMap::default();
359359
for (mid, parents) in need_check_available_modules {
@@ -414,7 +414,7 @@ impl FixIssuers {
414414
clean_modules: IdentifierMap<IdentifierSet>,
415415
) -> IdentifierMap<Vec<Option<ModuleIdentifier>>> {
416416
let mut revoke_module = IdentifierSet::default();
417-
let mut module_graph = ModuleGraph::new([None, None], Some(&mut artifact.module_graph_partial));
417+
let mut module_graph = ModuleGraph::new_mut([None, None], &mut artifact.module_graph_partial);
418418
for (mid, paths) in clean_modules {
419419
if revoke_module.contains(&mid) {
420420
continue;

crates/rspack_core/src/compilation/build_module_graph/graph_updater/repair/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
Compilation, CompilationId, CompilerId, CompilerOptions, DependencyTemplate,
1010
DependencyTemplateType, DependencyType, ModuleFactory, ResolverFactory, SharedPluginDriver,
1111
incremental::Incremental,
12-
module_graph::{ModuleGraph, ModuleGraphPartial},
12+
module_graph::{ModuleGraph, ModuleGraphMut, ModuleGraphPartial},
1313
old_cache::Cache as OldCache,
1414
};
1515

@@ -54,8 +54,8 @@ impl TaskContext {
5454
}
5555

5656
// TODO use module graph with make artifact
57-
pub fn get_module_graph_mut(partial: &mut ModuleGraphPartial) -> ModuleGraph<'_> {
58-
ModuleGraph::new([None, None], Some(partial))
57+
pub fn get_module_graph_mut(partial: &mut ModuleGraphPartial) -> ModuleGraphMut<'_> {
58+
ModuleGraph::new_mut([None, None], partial)
5959
}
6060

6161
// TODO remove it after incremental rebuild cover all stage

crates/rspack_core/src/compilation/mod.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ use crate::{
4848
DependencyCodeGeneration, DependencyTemplate, DependencyTemplateType, DependencyType, Entry,
4949
EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId, Filename, ImportPhase,
5050
ImportVarMap, ImportedByDeferModulesArtifact, Logger, MemoryGCStorage, ModuleFactory,
51-
ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphPartial, ModuleIdentifier, ModuleIdsArtifact,
52-
ModuleStaticCacheArtifact, PathData, ResolverFactory, RuntimeGlobals, RuntimeKeyMap, RuntimeMode,
53-
RuntimeModule, RuntimeSpec, RuntimeSpecMap, RuntimeTemplate, SharedPluginDriver,
54-
SideEffectsOptimizeArtifact, SourceType, Stats, ValueCacheVersions,
51+
ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphMut, ModuleGraphPartial, ModuleGraphRef,
52+
ModuleIdentifier, ModuleIdsArtifact, ModuleStaticCacheArtifact, PathData, ResolverFactory,
53+
RuntimeGlobals, RuntimeKeyMap, RuntimeMode, RuntimeModule, RuntimeSpec, RuntimeSpecMap,
54+
RuntimeTemplate, SharedPluginDriver, SideEffectsOptimizeArtifact, SourceType, Stats,
55+
ValueCacheVersions,
5556
build_chunk_graph::artifact::BuildChunkGraphArtifact,
5657
compilation::build_module_graph::{
5758
BuildModuleGraphArtifact, ModuleExecutor, UpdateParam, build_module_graph,
@@ -452,23 +453,17 @@ impl Compilation {
452453
std::mem::swap(&mut self.build_module_graph_artifact, make_artifact);
453454
}
454455

455-
pub fn get_module_graph(&self) -> ModuleGraph<'_> {
456+
pub fn get_module_graph(&self) -> ModuleGraphRef<'_> {
456457
if let Some(other_module_graph) = &self.other_module_graph {
457-
ModuleGraph::new(
458-
[
459-
Some(self.build_module_graph_artifact.get_module_graph_partial()),
460-
Some(other_module_graph),
461-
],
462-
None,
463-
)
458+
ModuleGraph::new_ref([
459+
Some(self.build_module_graph_artifact.get_module_graph_partial()),
460+
Some(other_module_graph),
461+
])
464462
} else {
465-
ModuleGraph::new(
466-
[
467-
Some(self.build_module_graph_artifact.get_module_graph_partial()),
468-
None,
469-
],
463+
ModuleGraph::new_ref([
464+
Some(self.build_module_graph_artifact.get_module_graph_partial()),
470465
None,
471-
)
466+
])
472467
}
473468
}
474469

@@ -492,23 +487,21 @@ impl Compilation {
492487
None
493488
}
494489

495-
pub fn get_module_graph_mut(&mut self) -> ModuleGraph<'_> {
490+
pub fn get_module_graph_mut(&mut self) -> ModuleGraphMut<'_> {
496491
if let Some(other) = &mut self.other_module_graph {
497-
ModuleGraph::new(
492+
ModuleGraph::new_mut(
498493
[
499494
Some(self.build_module_graph_artifact.get_module_graph_partial()),
500495
None,
501496
],
502-
Some(other),
497+
other,
503498
)
504499
} else {
505-
ModuleGraph::new(
500+
ModuleGraph::new_mut(
506501
[None, None],
507-
Some(
508-
self
509-
.build_module_graph_artifact
510-
.get_module_graph_partial_mut(),
511-
),
502+
self
503+
.build_module_graph_artifact
504+
.get_module_graph_partial_mut(),
512505
)
513506
}
514507
}

crates/rspack_core/src/module_graph/mod.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::collections::hash_map::Entry;
1+
use std::{
2+
collections::hash_map::Entry,
3+
ops::{Deref, DerefMut},
4+
};
25

36
use rayon::prelude::*;
47
use rspack_collections::{IdentifierMap, UkeyMap};
@@ -97,12 +100,48 @@ pub struct ModuleGraph<'a> {
97100
active: Option<&'a mut ModuleGraphPartial>,
98101
}
99102

103+
#[derive(Debug)]
104+
pub struct ModuleGraphRef<'a>(ModuleGraph<'a>);
105+
106+
#[derive(Debug)]
107+
pub struct ModuleGraphMut<'a>(ModuleGraph<'a>);
108+
109+
impl<'a> Deref for ModuleGraphRef<'a> {
110+
type Target = ModuleGraph<'a>;
111+
fn deref(&self) -> &Self::Target {
112+
&self.0
113+
}
114+
}
115+
116+
impl<'a> Deref for ModuleGraphMut<'a> {
117+
type Target = ModuleGraph<'a>;
118+
fn deref(&self) -> &Self::Target {
119+
&self.0
120+
}
121+
}
122+
123+
impl<'a> DerefMut for ModuleGraphMut<'a> {
124+
fn deref_mut(&mut self) -> &mut Self::Target {
125+
&mut self.0
126+
}
127+
}
128+
100129
impl<'a> ModuleGraph<'a> {
101-
pub fn new(
130+
pub fn new_ref(partials: [Option<&'a ModuleGraphPartial>; 2]) -> ModuleGraphRef<'a> {
131+
ModuleGraphRef(Self {
132+
partials,
133+
active: None,
134+
})
135+
}
136+
137+
pub fn new_mut(
102138
partials: [Option<&'a ModuleGraphPartial>; 2],
103-
active: Option<&'a mut ModuleGraphPartial>,
104-
) -> Self {
105-
Self { partials, active }
139+
active: &'a mut ModuleGraphPartial,
140+
) -> ModuleGraphMut<'a> {
141+
ModuleGraphMut(Self {
142+
partials,
143+
active: Some(active),
144+
})
106145
}
107146

108147
fn loop_partials<T>(&self, f: impl Fn(&ModuleGraphPartial) -> Option<&T>) -> Option<&T> {

0 commit comments

Comments
 (0)