Skip to content

Commit 36b0c2f

Browse files
committed
perf: reuse dependency between compilation
1 parent b41f13d commit 36b0c2f

File tree

8 files changed

+50
-17
lines changed

8 files changed

+50
-17
lines changed

crates/node_binding/src/compilation/entries.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ impl JsEntryData {
228228
.map(|dependency_id| {
229229
#[allow(clippy::unwrap_used)]
230230
let dep = module_graph.dependency_by_id(dependency_id).unwrap();
231-
JsDependencyWrapper::new(dep.as_ref(), compilation.id(), Some(compilation))
231+
JsDependencyWrapper::new(
232+
dep.as_ref(),
233+
compilation.compiler_id(),
234+
compilation.id(),
235+
Some(compilation),
236+
)
232237
})
233238
.collect::<Vec<_>>(),
234239
)
@@ -258,7 +263,12 @@ impl JsEntryData {
258263
.map(|dependency_id| {
259264
#[allow(clippy::unwrap_used)]
260265
let dep = module_graph.dependency_by_id(dependency_id).unwrap();
261-
JsDependencyWrapper::new(dep.as_ref(), compilation.id(), Some(compilation))
266+
JsDependencyWrapper::new(
267+
dep.as_ref(),
268+
compilation.compiler_id(),
269+
compilation.id(),
270+
Some(compilation),
271+
)
262272
})
263273
.collect::<Vec<_>>(),
264274
)

crates/node_binding/src/compilation/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ impl JsCompilation {
807807
let dependency = module_graph.dependency_by_id(&dependency_id).unwrap();
808808
let js_dependency = JsDependencyWrapper::new(
809809
dependency.as_ref(),
810+
compilation.compiler_id(),
810811
compilation.id(),
811812
Some(&compilation),
812813
);

crates/node_binding/src/context_module_factory.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ impl JsContextModuleFactoryAfterResolveData {
175175
.0
176176
.dependencies
177177
.iter()
178-
.map(|dep| JsDependencyWrapper::new(dep.as_ref(), self.0.compilation_id, None))
178+
.map(|dep| {
179+
JsDependencyWrapper::new(
180+
dep.as_ref(),
181+
self.0.compiler_id,
182+
self.0.compilation_id,
183+
None,
184+
)
185+
})
179186
.collect::<Vec<_>>()
180187
}
181188
}

crates/node_binding/src/dependency.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{cell::RefCell, ptr::NonNull};
22

33
use napi::{bindgen_prelude::ToNapiValue, Either, Env, JsString};
44
use napi_derive::napi;
5-
use rspack_core::{Compilation, CompilationId, Dependency, DependencyId};
5+
use rspack_core::{Compilation, CompilationId, CompilerId, Dependency, DependencyId};
66
use rspack_napi::OneShotInstanceRef;
77
use rspack_plugin_javascript::dependency::{
88
CommonJsExportRequireDependency, ESMExportImportedSpecifierDependency,
@@ -142,14 +142,14 @@ impl JsDependency {
142142

143143
type DependencyInstanceRefs = HashMap<DependencyId, OneShotInstanceRef<JsDependency>>;
144144

145-
type DependencyInstanceRefsByCompilationId =
146-
RefCell<HashMap<CompilationId, DependencyInstanceRefs>>;
145+
type DependencyInstanceRefsByCompilerId = RefCell<HashMap<CompilerId, DependencyInstanceRefs>>;
147146

148147
thread_local! {
149-
static DEPENDENCY_INSTANCE_REFS: DependencyInstanceRefsByCompilationId = Default::default();
148+
static DEPENDENCY_INSTANCE_REFS: DependencyInstanceRefsByCompilerId = Default::default();
150149
}
151150

152151
pub struct JsDependencyWrapper {
152+
compiler_id: CompilerId,
153153
dependency_id: DependencyId,
154154
dependency: NonNull<dyn Dependency>,
155155
compilation_id: CompilationId,
@@ -159,6 +159,7 @@ pub struct JsDependencyWrapper {
159159
impl JsDependencyWrapper {
160160
pub fn new(
161161
dependency: &dyn Dependency,
162+
compiler_id: CompilerId,
162163
compilation_id: CompilationId,
163164
compilation: Option<&Compilation>,
164165
) -> Self {
@@ -168,17 +169,18 @@ impl JsDependencyWrapper {
168169
Self {
169170
dependency_id,
170171
dependency: NonNull::new(dependency as *const dyn Dependency as *mut dyn Dependency).unwrap(),
172+
compiler_id,
171173
compilation_id,
172174
compilation: compilation
173175
.map(|c| NonNull::new(c as *const Compilation as *mut Compilation).unwrap()),
174176
}
175177
}
176178

177179
pub fn cleanup_last_compilation(compilation_id: CompilationId) {
178-
DEPENDENCY_INSTANCE_REFS.with(|refs| {
179-
let mut refs_by_compilation_id = refs.borrow_mut();
180-
refs_by_compilation_id.remove(&compilation_id)
181-
});
180+
// DEPENDENCY_INSTANCE_REFS.with(|refs| {
181+
// let mut refs_by_compilation_id = refs.borrow_mut();
182+
// refs_by_compilation_id.remove(&compilation_id)
183+
// });
182184
}
183185
}
184186

@@ -189,7 +191,7 @@ impl ToNapiValue for JsDependencyWrapper {
189191
) -> napi::Result<napi::sys::napi_value> {
190192
DEPENDENCY_INSTANCE_REFS.with(|refs| {
191193
let mut refs_by_compilation_id = refs.borrow_mut();
192-
let entry = refs_by_compilation_id.entry(val.compilation_id);
194+
let entry = refs_by_compilation_id.entry(val.compiler_id);
193195
let refs = match entry {
194196
std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
195197
std::collections::hash_map::Entry::Vacant(entry) => {

crates/node_binding/src/dependency_block.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ impl JsDependenciesBlock {
2727
.get_dependencies()
2828
.iter()
2929
.filter_map(|dependency_id| {
30-
module_graph
31-
.dependency_by_id(dependency_id)
32-
.map(|dep| JsDependencyWrapper::new(dep.as_ref(), compilation.id(), Some(compilation)))
30+
module_graph.dependency_by_id(dependency_id).map(|dep| {
31+
JsDependencyWrapper::new(
32+
dep.as_ref(),
33+
compilation.compiler_id(),
34+
compilation.id(),
35+
Some(compilation),
36+
)
37+
})
3338
})
3439
.collect::<Vec<_>>()
3540
} else {

crates/node_binding/src/module.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,12 @@ impl JsModule {
239239
.filter_map(|dependency_id| {
240240
module_graph.dependency_by_id(dependency_id).map(|dep| {
241241
let compilation = unsafe { self.compilation.map(|c| c.as_ref()) };
242-
JsDependencyWrapper::new(dep.as_ref(), self.compilation_id, compilation)
242+
JsDependencyWrapper::new(
243+
dep.as_ref(),
244+
self.compiler_id,
245+
self.compilation_id,
246+
compilation,
247+
)
243248
})
244249
})
245250
.collect::<Vec<_>>()

crates/node_binding/src/module_graph_connection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl JsModuleGraphConnection {
4040
if let Some(dependency) = module_graph.dependency_by_id(&self.dependency_id) {
4141
Ok(JsDependencyWrapper::new(
4242
dependency.as_ref(),
43+
compilation.compiler_id(),
4344
compilation.id(),
4445
Some(compilation),
4546
))

crates/rspack_core/src/context_module_factory.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use swc_core::common::util::take::Take;
1010
use tracing::instrument;
1111

1212
use crate::{
13-
resolve, BoxDependency, CompilationId, ContextElementDependency, ContextModule,
13+
resolve, BoxDependency, CompilationId, CompilerId, ContextElementDependency, ContextModule,
1414
ContextModuleOptions, DependencyCategory, DependencyId, DependencyType, ErrorSpan, ModuleExt,
1515
ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, RawModule,
1616
ResolveArgs, ResolveContextModuleDependencies, ResolveInnerOptions,
@@ -49,6 +49,7 @@ pub enum AfterResolveResult {
4949

5050
#[derive(Debug, Clone)]
5151
pub struct AfterResolveData {
52+
pub compiler_id: CompilerId,
5253
pub compilation_id: CompilationId,
5354
pub resource: Utf8PathBuf,
5455
pub context: String,
@@ -341,6 +342,7 @@ impl ContextModuleFactory {
341342
) -> Result<Option<ModuleFactoryResult>> {
342343
let context_options = &context_module_options.context_options;
343344
let after_resolve_data = AfterResolveData {
345+
compiler_id: data.compiler_id,
344346
compilation_id: data.compilation_id,
345347
resource: context_module_options.resource.clone(),
346348
context: context_options.context.clone(),

0 commit comments

Comments
 (0)