Skip to content

Commit b41f13d

Browse files
committed
perf: reuse js module between compilation
1 parent af88281 commit b41f13d

File tree

22 files changed

+200
-54
lines changed

22 files changed

+200
-54
lines changed

crates/node_binding/src/chunk_graph.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ impl JsChunkGraph {
4141
Ok(
4242
modules
4343
.iter()
44-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)))
44+
.map(|module| {
45+
JsModuleWrapper::new(
46+
module.as_ref(),
47+
compilation.compiler_id(),
48+
compilation.id(),
49+
Some(compilation),
50+
)
51+
})
4552
.collect::<Vec<_>>(),
4653
)
4754
}
@@ -58,7 +65,14 @@ impl JsChunkGraph {
5865
modules
5966
.iter()
6067
.filter_map(|module| module_graph.module_by_identifier(module))
61-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)))
68+
.map(|module| {
69+
JsModuleWrapper::new(
70+
module.as_ref(),
71+
compilation.compiler_id(),
72+
compilation.id(),
73+
Some(compilation),
74+
)
75+
})
6276
.collect::<Vec<_>>(),
6377
)
6478
}
@@ -102,7 +116,14 @@ impl JsChunkGraph {
102116
SourceType::from(source_type.as_str()),
103117
&compilation.get_module_graph(),
104118
)
105-
.map(|module| JsModuleWrapper::new(module, compilation.id(), Some(compilation)))
119+
.map(|module| {
120+
JsModuleWrapper::new(
121+
module,
122+
compilation.compiler_id(),
123+
compilation.id(),
124+
Some(compilation),
125+
)
126+
})
106127
.collect(),
107128
)
108129
}

crates/node_binding/src/chunk_group.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ impl JsChunkGroup {
7171
js_origins.push(JsChunkGroupOrigin {
7272
module: origin.module.and_then(|module_id| {
7373
compilation.module_by_identifier(&module_id).map(|module| {
74-
JsModuleWrapper::new(module.as_ref(), self.compilation_id, Some(compilation))
74+
JsModuleWrapper::new(
75+
module.as_ref(),
76+
compilation.compiler_id(),
77+
self.compilation_id,
78+
Some(compilation),
79+
)
7580
})
7681
}),
7782
request: match &origin.request {

crates/node_binding/src/compilation/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ impl JsCompilation {
189189
.keys()
190190
.filter_map(|module_id| {
191191
compilation.module_by_identifier(module_id).map(|module| {
192-
JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))
192+
JsModuleWrapper::new(
193+
module.as_ref(),
194+
compilation.compiler_id(),
195+
compilation.id(),
196+
Some(compilation),
197+
)
193198
})
194199
})
195200
.collect::<Vec<_>>(),
@@ -206,7 +211,12 @@ impl JsCompilation {
206211
.iter()
207212
.filter_map(|module_id| {
208213
compilation.module_by_identifier(module_id).map(|module| {
209-
JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))
214+
JsModuleWrapper::new(
215+
module.as_ref(),
216+
compilation.compiler_id(),
217+
compilation.id(),
218+
Some(compilation),
219+
)
210220
})
211221
})
212222
.collect::<Vec<_>>(),
@@ -617,7 +627,9 @@ impl JsCompilation {
617627
|modules| {
618628
modules
619629
.into_iter()
620-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation_id, None))
630+
.map(|module| {
631+
JsModuleWrapper::new(module.as_ref(), compiler_id, compilation_id, None)
632+
})
621633
.collect::<Vec<_>>()
622634
},
623635
)
@@ -798,8 +810,12 @@ impl JsCompilation {
798810
compilation.id(),
799811
Some(&compilation),
800812
);
801-
let js_module =
802-
JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation));
813+
let js_module = JsModuleWrapper::new(
814+
module.as_ref(),
815+
compilation.compiler_id(),
816+
compilation.id(),
817+
Some(compilation),
818+
);
803819
Either::B((js_dependency, js_module))
804820
}
805821
None => Either::A(format!(

crates/node_binding/src/module.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct JsFactoryMeta {
3636
pub struct JsModule {
3737
pub(crate) identifier: ModuleIdentifier,
3838
module: NonNull<dyn Module>,
39+
compiler_id: CompilerId,
3940
compilation_id: CompilationId,
4041
compilation: Option<NonNull<Compilation>>,
4142
}
@@ -274,7 +275,12 @@ impl JsModule {
274275
compilation
275276
.module_by_identifier(&inner_module_info.id)
276277
.map(|module| {
277-
JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))
278+
JsModuleWrapper::new(
279+
module.as_ref(),
280+
compilation.compiler_id(),
281+
compilation.id(),
282+
Some(compilation),
283+
)
278284
})
279285
})
280286
.collect::<Vec<_>>();
@@ -349,10 +355,10 @@ impl JsModule {
349355

350356
type ModuleInstanceRefs = IdentifierMap<OneShotInstanceRef<JsModule>>;
351357

352-
type ModuleInstanceRefsByCompilationId = RefCell<HashMap<CompilationId, ModuleInstanceRefs>>;
358+
type ModuleInstanceRefsByCompilerId = RefCell<HashMap<CompilerId, ModuleInstanceRefs>>;
353359

354360
thread_local! {
355-
static MODULE_INSTANCE_REFS: ModuleInstanceRefsByCompilationId = Default::default();
361+
static MODULE_INSTANCE_REFS: ModuleInstanceRefsByCompilerId = Default::default();
356362
}
357363

358364
// The difference between JsModuleWrapper and JsModule is:
@@ -362,6 +368,7 @@ thread_local! {
362368
pub struct JsModuleWrapper {
363369
identifier: ModuleIdentifier,
364370
module: NonNull<dyn Module>,
371+
compiler_id: CompilerId,
365372
compilation_id: CompilationId,
366373
compilation: Option<NonNull<Compilation>>,
367374
}
@@ -371,6 +378,7 @@ unsafe impl Send for JsModuleWrapper {}
371378
impl JsModuleWrapper {
372379
pub fn new(
373380
module: &dyn Module,
381+
compiler_id: CompilerId,
374382
compilation_id: CompilationId,
375383
compilation: Option<&Compilation>,
376384
) -> Self {
@@ -381,17 +389,18 @@ impl JsModuleWrapper {
381389
Self {
382390
identifier,
383391
module: NonNull::new(module as *const dyn Module as *mut dyn Module).unwrap(),
392+
compiler_id,
384393
compilation_id,
385394
compilation: compilation
386395
.map(|c| NonNull::new(c as *const Compilation as *mut Compilation).unwrap()),
387396
}
388397
}
389398

390399
pub fn cleanup_last_compilation(compilation_id: CompilationId) {
391-
MODULE_INSTANCE_REFS.with(|refs| {
392-
let mut refs_by_compilation_id = refs.borrow_mut();
393-
refs_by_compilation_id.remove(&compilation_id)
394-
});
400+
// MODULE_INSTANCE_REFS.with(|refs| {
401+
// let mut refs_by_compilation_id = refs.borrow_mut();
402+
// refs_by_compilation_id.remove(&compilation_id)
403+
// });
395404
}
396405

397406
pub fn attach(&mut self, compilation: *const Compilation) {
@@ -409,8 +418,8 @@ impl ToNapiValue for JsModuleWrapper {
409418
let module = unsafe { val.module.as_ref() };
410419

411420
MODULE_INSTANCE_REFS.with(|refs| {
412-
let mut refs_by_compilation_id = refs.borrow_mut();
413-
let entry = refs_by_compilation_id.entry(val.compilation_id);
421+
let mut refs_by_compiler_id = refs.borrow_mut();
422+
let entry = refs_by_compiler_id.entry(val.compiler_id);
414423
let refs = match entry {
415424
std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
416425
std::collections::hash_map::Entry::Vacant(entry) => {
@@ -431,6 +440,7 @@ impl ToNapiValue for JsModuleWrapper {
431440
let js_module = JsModule {
432441
identifier: val.identifier,
433442
module: val.module,
443+
compiler_id: val.compiler_id,
434444
compilation_id: val.compilation_id,
435445
compilation: val.compilation,
436446
};
@@ -450,6 +460,7 @@ impl FromNapiValue for JsModuleWrapper {
450460
identifier: instance.identifier,
451461
#[allow(clippy::unwrap_used)]
452462
module: instance.module,
463+
compiler_id: instance.compiler_id,
453464
compilation_id: instance.compilation_id,
454465
compilation: instance.compilation,
455466
})

crates/node_binding/src/module_graph.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ impl JsModuleGraph {
3636
pub fn get_module(&self, js_dependency: &JsDependency) -> napi::Result<Option<JsModuleWrapper>> {
3737
let (compilation, module_graph) = self.as_ref()?;
3838
let module = module_graph.get_module_by_dependency_id(&js_dependency.dependency_id);
39-
let js_module = module
40-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)));
39+
let js_module = module.map(|module| {
40+
JsModuleWrapper::new(
41+
module.as_ref(),
42+
compilation.compiler_id(),
43+
compilation.id(),
44+
Some(compilation),
45+
)
46+
});
4147
Ok(js_module)
4248
}
4349

@@ -51,7 +57,14 @@ impl JsModuleGraph {
5157
match module_graph.connection_by_dependency_id(&js_dependency.dependency_id) {
5258
Some(connection) => module_graph
5359
.module_by_identifier(&connection.resolved_module)
54-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))),
60+
.map(|module| {
61+
JsModuleWrapper::new(
62+
module.as_ref(),
63+
compilation.compiler_id(),
64+
compilation.id(),
65+
Some(compilation),
66+
)
67+
}),
5568
None => None,
5669
},
5770
)
@@ -93,10 +106,14 @@ impl JsModuleGraph {
93106
pub fn get_issuer(&self, module: &JsModule) -> napi::Result<Option<JsModuleWrapper>> {
94107
let (compilation, module_graph) = self.as_ref()?;
95108
let issuer = module_graph.get_issuer(&module.identifier);
96-
Ok(
97-
issuer
98-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))),
99-
)
109+
Ok(issuer.map(|module| {
110+
JsModuleWrapper::new(
111+
module.as_ref(),
112+
compilation.compiler_id(),
113+
compilation.id(),
114+
Some(compilation),
115+
)
116+
}))
100117
}
101118

102119
#[napi]
@@ -161,9 +178,14 @@ impl JsModuleGraph {
161178
let (compilation, module_graph) = self.as_ref()?;
162179
Ok(
163180
match module_graph.get_parent_module(&js_dependency.dependency_id) {
164-
Some(identifier) => compilation
165-
.module_by_identifier(identifier)
166-
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))),
181+
Some(identifier) => compilation.module_by_identifier(identifier).map(|module| {
182+
JsModuleWrapper::new(
183+
module.as_ref(),
184+
compilation.compiler_id(),
185+
compilation.id(),
186+
Some(compilation),
187+
)
188+
}),
167189
None => None,
168190
},
169191
)

crates/node_binding/src/module_graph_connection.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ impl JsModuleGraphConnection {
5656
let (compilation, module_graph) = self.as_ref()?;
5757
if let Some(connection) = module_graph.connection_by_dependency_id(&self.dependency_id) {
5858
let module = module_graph.module_by_identifier(connection.module_identifier());
59-
Ok(module.map(|m| JsModuleWrapper::new(m.as_ref(), compilation.id(), Some(compilation))))
59+
Ok(module.map(|m| {
60+
JsModuleWrapper::new(
61+
m.as_ref(),
62+
compilation.compiler_id(),
63+
compilation.id(),
64+
Some(compilation),
65+
)
66+
}))
6067
} else {
6168
Err(napi::Error::from_reason(format!(
6269
"Unable to access ModuleGraphConnection with id = {:#?} now. The ModuleGraphConnection have been removed on the Rust side.",
@@ -70,7 +77,14 @@ impl JsModuleGraphConnection {
7077
let (compilation, module_graph) = self.as_ref()?;
7178
if let Some(connection) = module_graph.connection_by_dependency_id(&self.dependency_id) {
7279
let module = module_graph.module_by_identifier(&connection.resolved_module);
73-
Ok(module.map(|m| JsModuleWrapper::new(m.as_ref(), compilation.id(), Some(compilation))))
80+
Ok(module.map(|m| {
81+
JsModuleWrapper::new(
82+
m.as_ref(),
83+
compilation.compiler_id(),
84+
compilation.id(),
85+
Some(compilation),
86+
)
87+
}))
7488
} else {
7589
Err(napi::Error::from_reason(format!(
7690
"Unable to access ModuleGraphConnection with id = {:#?} now. The ModuleGraphConnection have been removed on the Rust side.",
@@ -86,7 +100,14 @@ impl JsModuleGraphConnection {
86100
Ok(match connection.original_module_identifier {
87101
Some(original_module_identifier) => module_graph
88102
.module_by_identifier(&original_module_identifier)
89-
.map(|m| JsModuleWrapper::new(m.as_ref(), compilation.id(), Some(compilation))),
103+
.map(|m| {
104+
JsModuleWrapper::new(
105+
m.as_ref(),
106+
compilation.compiler_id(),
107+
compilation.id(),
108+
Some(compilation),
109+
)
110+
}),
90111
None => None,
91112
})
92113
} else {

crates/node_binding/src/plugins/interceptor.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,12 +1055,18 @@ impl CompilerAssetEmitted for CompilerAssetEmittedTap {
10551055
impl CompilationBuildModule for CompilationBuildModuleTap {
10561056
async fn run(
10571057
&self,
1058+
compiler_id: CompilerId,
10581059
compilation_id: CompilationId,
10591060
module: &mut BoxModule,
10601061
) -> rspack_error::Result<()> {
10611062
self
10621063
.function
1063-
.call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None))
1064+
.call_with_sync(JsModuleWrapper::new(
1065+
module.as_ref(),
1066+
compiler_id,
1067+
compilation_id,
1068+
None,
1069+
))
10641070
.await
10651071
}
10661072

@@ -1073,12 +1079,18 @@ impl CompilationBuildModule for CompilationBuildModuleTap {
10731079
impl CompilationStillValidModule for CompilationStillValidModuleTap {
10741080
async fn run(
10751081
&self,
1082+
compiler_id: CompilerId,
10761083
compilation_id: CompilationId,
10771084
module: &mut BoxModule,
10781085
) -> rspack_error::Result<()> {
10791086
self
10801087
.function
1081-
.call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None))
1088+
.call_with_sync(JsModuleWrapper::new(
1089+
module.as_ref(),
1090+
compiler_id,
1091+
compilation_id,
1092+
None,
1093+
))
10821094
.await
10831095
}
10841096

@@ -1091,12 +1103,18 @@ impl CompilationStillValidModule for CompilationStillValidModuleTap {
10911103
impl CompilationSucceedModule for CompilationSucceedModuleTap {
10921104
async fn run(
10931105
&self,
1106+
compiler_id: CompilerId,
10941107
compilation_id: CompilationId,
10951108
module: &mut BoxModule,
10961109
) -> rspack_error::Result<()> {
10971110
self
10981111
.function
1099-
.call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None))
1112+
.call_with_sync(JsModuleWrapper::new(
1113+
module.as_ref(),
1114+
compiler_id,
1115+
compilation_id,
1116+
None,
1117+
))
11001118
.await
11011119
}
11021120

0 commit comments

Comments
 (0)