Skip to content

Commit

Permalink
refactor: get plugin hooks by compilation id (#9222)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Feb 10, 2025
1 parent 3c86a19 commit 2ac097f
Show file tree
Hide file tree
Showing 20 changed files with 41 additions and 42 deletions.
8 changes: 4 additions & 4 deletions crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ async fn js_hooks_adapter_compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> rspack_error::Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks
.chunk_hash
.intercept(self.register_javascript_modules_chunk_hash_taps.clone());
Expand All @@ -457,7 +457,7 @@ async fn html_hooks_adapter_compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> rspack_error::Result<()> {
let mut hooks = HtmlRspackPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = HtmlRspackPlugin::get_compilation_hooks_mut(compilation.id());
hooks.before_asset_tag_generation.intercept(
self
.register_html_plugin_before_asset_tag_generation_taps
Expand Down Expand Up @@ -492,7 +492,7 @@ async fn runtime_hooks_adapter_compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> rspack_error::Result<()> {
let mut hooks = RuntimePlugin::get_compilation_hooks_mut(compilation);
let mut hooks = RuntimePlugin::get_compilation_hooks_mut(compilation.id());
hooks
.create_script
.intercept(self.register_runtime_plugin_create_script_taps.clone());
Expand All @@ -511,7 +511,7 @@ async fn rsdoctor_hooks_adapter_compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> rspack_error::Result<()> {
let mut hooks = RsdoctorPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = RsdoctorPlugin::get_compilation_hooks_mut(compilation.id());
hooks
.module_graph
.intercept(self.register_rsdoctor_plugin_module_graph_taps.clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn eval_devtool_plugin_compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks
.render_module_content
.tap(eval_devtool_plugin_render_module_content::new(self));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn eval_source_map_devtool_plugin_compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks
.render_module_content
.tap(eval_source_map_devtool_plugin_render_module_content::new(
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_html/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ impl HtmlRspackPlugin {
}

pub fn get_compilation_hooks_mut(
compilation: &Compilation,
) -> dashmap::mapref::one::RefMut<'_, CompilationId, Box<HtmlPluginHooks>> {
COMPILATION_HOOKS_MAP.entry(compilation.id()).or_default()
id: CompilationId,
) -> dashmap::mapref::one::RefMut<'static, CompilationId, Box<HtmlPluginHooks>> {
COMPILATION_HOOKS_MAP.entry(id).or_default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/src/plugin/api_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks
.render_module_content
.tap(render_module_content::new(self));
Expand Down
19 changes: 9 additions & 10 deletions crates/rspack_plugin_javascript/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ pub struct JsPlugin {

impl JsPlugin {
pub fn get_compilation_hooks(
compilation: &Compilation,
) -> dashmap::mapref::one::Ref<'_, CompilationId, Box<JavascriptModulesPluginHooks>> {
let id = compilation.id();
id: CompilationId,
) -> dashmap::mapref::one::Ref<'static, CompilationId, Box<JavascriptModulesPluginHooks>> {
if !COMPILATION_HOOKS_MAP.contains_key(&id) {
COMPILATION_HOOKS_MAP.insert(id, Default::default());
}
Expand All @@ -117,9 +116,9 @@ impl JsPlugin {
}

pub fn get_compilation_hooks_mut(
compilation: &Compilation,
) -> dashmap::mapref::one::RefMut<'_, CompilationId, Box<JavascriptModulesPluginHooks>> {
COMPILATION_HOOKS_MAP.entry(compilation.id()).or_default()
id: CompilationId,
) -> dashmap::mapref::one::RefMut<'static, CompilationId, Box<JavascriptModulesPluginHooks>> {
COMPILATION_HOOKS_MAP.entry(id).or_default()
}

pub fn render_require(&self, chunk_ukey: &ChunkUkey, compilation: &Compilation) -> Vec<Cow<str>> {
Expand Down Expand Up @@ -353,7 +352,7 @@ impl JsPlugin {
buf2.push("// This entry module doesn't tell about it's top-level declarations so it can't be inlined".into());
allow_inline_startup = false;
}
let hooks = JsPlugin::get_compilation_hooks(compilation);
let hooks = JsPlugin::get_compilation_hooks(compilation.id());
let bailout = hooks.inline_in_runtime_bailout.call(compilation)?;
if allow_inline_startup && let Some(bailout) = bailout {
buf2.push(format!("// This entry module can't be inlined because {bailout}").into());
Expand Down Expand Up @@ -515,7 +514,7 @@ impl JsPlugin {
chunk_ukey: &ChunkUkey,
output_path: &str,
) -> Result<BoxSource> {
let hooks = Self::get_compilation_hooks(compilation);
let hooks = Self::get_compilation_hooks(compilation.id());
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let supports_arrow_function = compilation
.options
Expand Down Expand Up @@ -1107,7 +1106,7 @@ impl JsPlugin {
chunk_ukey: &ChunkUkey,
output_path: &str,
) -> Result<BoxSource> {
let hooks = Self::get_compilation_hooks(compilation);
let hooks = Self::get_compilation_hooks(compilation.id());
let module_graph = &compilation.get_module_graph();
let is_module = compilation.options.output.module;
let mut all_strict = compilation.options.output.module;
Expand Down Expand Up @@ -1166,7 +1165,7 @@ impl JsPlugin {
compilation: &Compilation,
hasher: &mut RspackHash,
) -> Result<()> {
let hooks = Self::get_compilation_hooks(compilation);
let hooks = Self::get_compilation_hooks(compilation.id());
hooks.chunk_hash.call(compilation, chunk_ukey, hasher).await
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn render_module(
return Ok(None);
};

let hooks = JsPlugin::get_compilation_hooks(compilation);
let hooks = JsPlugin::get_compilation_hooks(compilation.id());
let mut module_chunk_init_fragments = match code_gen_result.data.get::<ChunkInitFragments>() {
Some(fragments) => fragments.clone(),
None => ChunkInitFragments::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_library/src/amd_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render.tap(render::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_library/src/assign_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render.tap(render::new(self));
hooks.render_startup.tap(render_startup::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render_startup.tap(render_startup::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render_startup.tap(render_startup::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_library/src/module_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render_startup.tap(render_startup::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_library/src/system_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render.tap(render::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_library/src/umd_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render.tap(render::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_rsdoctor/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ impl RsdoctorPlugin {
}

pub fn get_compilation_hooks_mut(
compilation: &Compilation,
) -> dashmap::mapref::one::RefMut<'_, CompilationId, Box<RsdoctorPluginHooks>> {
COMPILATION_HOOKS_MAP.entry(compilation.id()).or_default()
id: CompilationId,
) -> dashmap::mapref::one::RefMut<'static, CompilationId, Box<RsdoctorPluginHooks>> {
COMPILATION_HOOKS_MAP.entry(id).or_default()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.chunk_hash.tap(js_chunk_hash::new(self));
hooks.render_chunk.tap(render_chunk::new(self));
Ok(())
Expand Down Expand Up @@ -98,7 +98,7 @@ fn render_chunk(
chunk_ukey: &ChunkUkey,
render_source: &mut RenderSource,
) -> Result<()> {
let hooks = JsPlugin::get_compilation_hooks(compilation);
let hooks = JsPlugin::get_compilation_hooks(compilation.id());
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let has_runtime_modules = compilation
.chunk_graph
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_runtime/src/common_js_chunk_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.chunk_hash.tap(js_chunk_hash::new(self));
hooks.render_chunk.tap(render_chunk::new(self));
Ok(())
Expand Down Expand Up @@ -96,7 +96,7 @@ fn render_chunk(
chunk_ukey: &ChunkUkey,
render_source: &mut RenderSource,
) -> Result<()> {
let hooks = JsPlugin::get_compilation_hooks(compilation);
let hooks = JsPlugin::get_compilation_hooks(compilation.id());
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let base_chunk_output_name = get_chunk_output_name(chunk, compilation)?;
let mut sources = ConcatSource::default();
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_runtime/src/module_chunk_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.render_chunk.tap(render_chunk::new(self));
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
Expand Down Expand Up @@ -99,7 +99,7 @@ fn render_chunk(
chunk_ukey: &ChunkUkey,
render_source: &mut RenderSource,
) -> Result<()> {
let hooks = JsPlugin::get_compilation_hooks(compilation);
let hooks = JsPlugin::get_compilation_hooks(compilation.id());
let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey);
let base_chunk_output_name = get_chunk_output_name(chunk, compilation)?;
if matches!(chunk.kind(), ChunkKind::HotUpdate) {
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_plugin_runtime/src/runtime_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ impl RuntimePlugin {
}

pub fn get_compilation_hooks_mut(
compilation: &Compilation,
) -> dashmap::mapref::one::RefMut<'_, CompilationId, Box<RuntimePluginHooks>> {
COMPILATION_HOOKS_MAP.entry(compilation.id()).or_default()
id: CompilationId,
) -> dashmap::mapref::one::RefMut<'static, CompilationId, Box<RuntimePluginHooks>> {
COMPILATION_HOOKS_MAP.entry(id).or_default()
}
}

Expand All @@ -183,7 +183,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async fn compilation(
compilation: &mut Compilation,
_params: &mut CompilationParams,
) -> Result<()> {
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation);
let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id());
hooks.chunk_hash.tap(js_chunk_hash::new(self));
Ok(())
}
Expand Down

2 comments on commit 2ac097f

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 2ac097f Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 2ac097f Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2025-02-10 09af1d8) Current Change
10000_big_production-mode_disable-minimize + exec 37.7 s ± 622 ms 38.8 s ± 790 ms +2.99 %
10000_development-mode + exec 1.87 s ± 37 ms 1.87 s ± 180 ms -0.32 %
10000_development-mode_hmr + exec 684 ms ± 18 ms 702 ms ± 31 ms +2.67 %
10000_production-mode + exec 2.31 s ± 63 ms 2.29 s ± 99 ms -0.62 %
10000_production-mode_persistent-cold + exec 2.45 s ± 74 ms 2.41 s ± 36 ms -1.66 %
10000_production-mode_persistent-hot + exec 1.64 s ± 26 ms 1.67 s ± 69 ms +2.03 %
arco-pro_development-mode + exec 1.72 s ± 90 ms 1.75 s ± 117 ms +1.92 %
arco-pro_development-mode_hmr + exec 389 ms ± 2.1 ms 388 ms ± 2.4 ms -0.29 %
arco-pro_production-mode + exec 3.62 s ± 212 ms 3.68 s ± 124 ms +1.60 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.67 s ± 262 ms 3.72 s ± 39 ms +1.38 %
arco-pro_production-mode_persistent-cold + exec 3.83 s ± 285 ms 3.76 s ± 72 ms -1.74 %
arco-pro_production-mode_persistent-hot + exec 2.36 s ± 60 ms 2.46 s ± 107 ms +4.12 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.63 s ± 197 ms 3.68 s ± 148 ms +1.35 %
large-dyn-imports_development-mode + exec 2.12 s ± 39 ms 2.12 s ± 122 ms +0.22 %
large-dyn-imports_production-mode + exec 2.14 s ± 20 ms 2.14 s ± 50 ms -0.12 %
threejs_development-mode_10x + exec 1.53 s ± 28 ms 1.53 s ± 15 ms +0.46 %
threejs_development-mode_10x_hmr + exec 783 ms ± 13 ms 804 ms ± 23 ms +2.74 %
threejs_production-mode_10x + exec 5.22 s ± 186 ms 5.25 s ± 248 ms +0.51 %
threejs_production-mode_10x_persistent-cold + exec 5.31 s ± 155 ms 5.29 s ± 101 ms -0.35 %
threejs_production-mode_10x_persistent-hot + exec 4.53 s ± 315 ms 4.58 s ± 396 ms +1.17 %
10000_big_production-mode_disable-minimize + rss memory 8773 MiB ± 74.2 MiB 8722 MiB ± 80.6 MiB -0.59 %
10000_development-mode + rss memory 653 MiB ± 15.5 MiB 661 MiB ± 29 MiB +1.31 %
10000_development-mode_hmr + rss memory 1264 MiB ± 261 MiB 1253 MiB ± 235 MiB -0.88 %
10000_production-mode + rss memory 623 MiB ± 16.1 MiB 637 MiB ± 41.2 MiB +2.25 %
10000_production-mode_persistent-cold + rss memory 743 MiB ± 22.3 MiB 737 MiB ± 16.4 MiB -0.81 %
10000_production-mode_persistent-hot + rss memory 714 MiB ± 18.8 MiB 737 MiB ± 23.2 MiB +3.14 %
arco-pro_development-mode + rss memory 561 MiB ± 21.6 MiB 557 MiB ± 35.6 MiB -0.64 %
arco-pro_development-mode_hmr + rss memory 638 MiB ± 51.3 MiB 639 MiB ± 60.4 MiB +0.22 %
arco-pro_production-mode + rss memory 735 MiB ± 26.9 MiB 703 MiB ± 26 MiB -4.35 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 741 MiB ± 13.1 MiB 740 MiB ± 77.2 MiB -0.07 %
arco-pro_production-mode_persistent-cold + rss memory 857 MiB ± 36.2 MiB 848 MiB ± 26.7 MiB -1.04 %
arco-pro_production-mode_persistent-hot + rss memory 730 MiB ± 6.4 MiB 718 MiB ± 25.1 MiB -1.61 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 737 MiB ± 60.8 MiB 708 MiB ± 34.3 MiB -3.93 %
large-dyn-imports_development-mode + rss memory 647 MiB ± 9.25 MiB 648 MiB ± 6.07 MiB +0.18 %
large-dyn-imports_production-mode + rss memory 530 MiB ± 6.21 MiB 530 MiB ± 9.69 MiB -0.10 %
threejs_development-mode_10x + rss memory 551 MiB ± 25.1 MiB 552 MiB ± 15.8 MiB +0.29 %
threejs_development-mode_10x_hmr + rss memory 1124 MiB ± 189 MiB 1153 MiB ± 223 MiB +2.62 %
threejs_production-mode_10x + rss memory 829 MiB ± 36.8 MiB 831 MiB ± 8.89 MiB +0.25 %
threejs_production-mode_10x_persistent-cold + rss memory 967 MiB ± 35.8 MiB 939 MiB ± 43.9 MiB -2.89 %
threejs_production-mode_10x_persistent-hot + rss memory 868 MiB ± 38.6 MiB 870 MiB ± 66.9 MiB +0.24 %

Please sign in to comment.