Skip to content

Commit

Permalink
refactor: module diagnostic action move to Diagnosable trait (#9264)
Browse files Browse the repository at this point in the history
* refactor: Diagnosable trait use mutable self

* refactor: update Diagnosable trait
  • Loading branch information
jerrykingxyz authored Feb 12, 2025
1 parent 519c54c commit 7fca533
Show file tree
Hide file tree
Showing 22 changed files with 85 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod t {

use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip};
use rspack_collections::Identifiable;
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_macros::impl_source_map_config;
use rspack_sources::Source;
use rspack_util::{atom::Atom, source_map::SourceMapKind};
Expand Down Expand Up @@ -285,10 +285,6 @@ mod t {
todo!()
}

fn get_diagnostics(&self) -> Vec<Diagnostic> {
todo!()
}

fn original_source(&self) -> Option<&dyn Source> {
todo!()
}
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_core/src/compiler/make/repair/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ impl Task<MakeTaskContext> for BuildTask {

let build_result = result.map(|t| {
let diagnostics = module
.clone_diagnostics()
.into_iter()
.diagnostics()
.iter()
.cloned()
.map(|d| d.with_module_identifier(Some(module.identifier())))
.collect();
t.with_diagnostic(diagnostics)
Expand Down
62 changes: 14 additions & 48 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::hash_map::Entry,
fmt::Debug,
hash::{BuildHasherDefault, Hasher},
sync::{Arc, LazyLock, Mutex},
sync::{Arc, LazyLock},
};

use dashmap::DashMap;
Expand Down Expand Up @@ -372,7 +372,7 @@ pub struct ConcatenatedModule {
#[cacheable(with=AsMap)]
cached_source_sizes: DashMap<SourceType, f64, BuildHasherDefault<FxHasher>>,
#[cacheable(with=Skip)]
diagnostics: Mutex<Vec<Diagnostic>>,
diagnostics: Vec<Diagnostic>,
build_info: BuildInfo,
}

Expand All @@ -394,7 +394,7 @@ impl ConcatenatedModule {
dependencies: vec![],
blocks: vec![],
cached_source_sizes: DashMap::default(),
diagnostics: Mutex::new(vec![]),
diagnostics: vec![],
build_info: BuildInfo {
cacheable: true,
hash: None,
Expand Down Expand Up @@ -490,11 +490,6 @@ impl Module for ConcatenatedModule {
&ModuleType::JsEsm
}

fn get_diagnostics(&self) -> Vec<Diagnostic> {
let guard = self.diagnostics.lock().expect("should have diagnostics");
guard.clone()
}

fn factory_meta(&self) -> Option<&FactoryMeta> {
self.root_module_ctxt.factory_meta.as_ref()
}
Expand Down Expand Up @@ -556,9 +551,6 @@ impl Module for ConcatenatedModule {
compilation: Option<&Compilation>,
) -> Result<BuildResult> {
let compilation = compilation.expect("should pass compilation");
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L774-L784
// Some fields does not exists in rspack
self.clear_diagnostics();

let module_graph = compilation.get_module_graph();
let modules = self
Expand Down Expand Up @@ -592,12 +584,8 @@ impl Module for ConcatenatedModule {
for b in module.get_blocks() {
self.blocks.push(*b);
}
let mut diagnostics_guard = self.diagnostics.lock().expect("should have diagnostics");
// populate diagnostic
diagnostics_guard.extend(module.get_diagnostics());

// release guard ASAP
drop(diagnostics_guard);
self.diagnostics.extend(module.diagnostics().into_owned());

// populate topLevelDeclarations
let module_build_info = module.build_info();
Expand Down Expand Up @@ -1411,42 +1399,20 @@ impl Module for ConcatenatedModule {
}

impl Diagnosable for ConcatenatedModule {
fn add_diagnostic(&self, diagnostic: Diagnostic) {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.push(diagnostic);
}

fn add_diagnostics(&self, mut diagnostics: Vec<Diagnostic>) {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.append(&mut diagnostics);
}

fn clone_diagnostics(&self) -> Vec<Diagnostic> {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.iter()
.cloned()
.collect()
fn add_diagnostic(&mut self, diagnostic: Diagnostic) {
self.diagnostics.push(diagnostic);
}
}

impl ConcatenatedModule {
fn clear_diagnostics(&mut self) {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.clear()
fn add_diagnostics(&mut self, mut diagnostics: Vec<Diagnostic>) {
self.diagnostics.append(&mut diagnostics);
}

fn diagnostics(&self) -> Cow<[Diagnostic]> {
Cow::Borrowed(&self.diagnostics)
}
}

impl ConcatenatedModule {
// TODO: replace self.modules with indexmap or linkedhashset
fn get_modules_with_info(
&self,
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rspack_cacheable::{
with::{AsOption, AsPreset, AsVec, Unsupported},
};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_macros::impl_source_map_config;
use rspack_paths::{ArcPath, Utf8PathBuf};
use rspack_regex::RspackRegex;
Expand Down Expand Up @@ -847,10 +847,6 @@ impl Module for ContextModule {
&[SourceType::JavaScript]
}

fn get_diagnostics(&self) -> Vec<Diagnostic> {
vec![]
}

fn original_source(&self) -> Option<&dyn rspack_sources::Source> {
None
}
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{borrow::Cow, hash::Hash, iter};

use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{error, impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_error::{error, impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_macros::impl_source_map_config;
use rspack_util::{ext::DynHash, json_stringify, source_map::SourceMapKind};
Expand Down Expand Up @@ -490,10 +490,6 @@ impl Module for ExternalModule {
&ModuleType::JsAuto
}

fn get_diagnostics(&self) -> Vec<Diagnostic> {
vec![]
}

fn source_types(&self) -> &[SourceType] {
if self.external_type == "css-import" {
EXTERNAL_MODULE_CSS_SOURCE_TYPES
Expand Down
12 changes: 4 additions & 8 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rspack_cacheable::{
with::{AsOption, AsVec},
};
use rspack_collections::{Identifiable, Identifier, IdentifierSet};
use rspack_error::{Diagnosable, Diagnostic, Result};
use rspack_error::{Diagnosable, Result};
use rspack_fs::ReadableFileSystem;
use rspack_hash::RspackHashDigest;
use rspack_paths::ArcPath;
Expand Down Expand Up @@ -218,7 +218,7 @@ pub trait Module:

/// Defines what kind of code generation results this module can generate.
fn source_types(&self) -> &[SourceType];
fn get_diagnostics(&self) -> Vec<Diagnostic>;

/// The original source of the module. This could be optional, modules like the `NormalModule` can have the corresponding original source.
/// However, modules that is created from "nowhere" (e.g. `ExternalModule` and `MissingModule`) does not have its original source.
fn original_source(&self) -> Option<&dyn Source>;
Expand Down Expand Up @@ -606,7 +606,7 @@ mod test {

use rspack_cacheable::cacheable;
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{Diagnosable, Diagnostic, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_sources::Source;
use rspack_util::source_map::{ModuleSourceMapConfig, SourceMapKind};

Expand All @@ -633,7 +633,7 @@ mod test {
}
}

impl Diagnosable for $ident {}
impl_empty_diagnosable_trait!($ident);

impl DependenciesBlock for $ident {
fn add_block_id(&mut self, _: AsyncDependenciesBlockIdentifier) {
Expand Down Expand Up @@ -692,10 +692,6 @@ mod test {
unreachable!()
}

fn get_diagnostics(&self) -> Vec<Diagnostic> {
vec![]
}

fn update_hash(
&self,
_hasher: &mut dyn std::hash::Hasher,
Expand Down
51 changes: 11 additions & 40 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
ptr::NonNull,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
Arc,
},
};

Expand Down Expand Up @@ -143,7 +143,7 @@ pub struct NormalModule {
#[cacheable(with=AsMap)]
cached_source_sizes: DashMap<SourceType, f64, BuildHasherDefault<FxHasher>>,
#[cacheable(with=Skip)]
diagnostics: Mutex<Vec<Diagnostic>>,
diagnostics: Vec<Diagnostic>,

code_generation_dependencies: Option<Vec<Box<dyn ModuleDependency>>>,
presentational_dependencies: Option<Vec<Box<dyn DependencyTemplate>>>,
Expand Down Expand Up @@ -231,7 +231,7 @@ impl NormalModule {
debug_id: DEBUG_ID.fetch_add(1, Ordering::Relaxed),

cached_source_sizes: DashMap::default(),
diagnostics: Mutex::new(Default::default()),
diagnostics: Default::default(),
code_generation_dependencies: None,
presentational_dependencies: None,
factory_meta: None,
Expand Down Expand Up @@ -371,11 +371,6 @@ impl Module for NormalModule {
&self.module_type
}

fn get_diagnostics(&self) -> Vec<Diagnostic> {
let guard = self.diagnostics.lock().expect("should have diagnostics");
guard.clone()
}

fn source_types(&self) -> &[SourceType] {
self.parser_and_generator.source_types()
}
Expand Down Expand Up @@ -408,8 +403,6 @@ impl Module for NormalModule {
build_context: BuildContext,
_compilation: Option<&Compilation>,
) -> Result<BuildResult> {
self.clear_diagnostics();

// so does webpack
self.parsed = true;

Expand Down Expand Up @@ -545,7 +538,7 @@ impl Module for NormalModule {
if no_parse {
self.parsed = false;
self.original_source = Some(original_source.clone());
self.source = NormalModuleSource::new_built(original_source, self.clone_diagnostics());
self.source = NormalModuleSource::new_built(original_source, self.diagnostics.clone());
self.code_generation_dependencies = Some(Vec::new());
self.presentational_dependencies = Some(Vec::new());

Expand Down Expand Up @@ -607,7 +600,7 @@ impl Module for NormalModule {
// Only side effects used in code_generate can stay here
// Other side effects should be set outside use_cache
self.original_source = Some(source.clone());
self.source = NormalModuleSource::new_built(source, self.clone_diagnostics());
self.source = NormalModuleSource::new_built(source, self.diagnostics.clone());
self.code_generation_dependencies = Some(code_generation_dependencies);
self.presentational_dependencies = Some(presentational_dependencies);

Expand Down Expand Up @@ -798,30 +791,16 @@ impl Module for NormalModule {
}

impl Diagnosable for NormalModule {
fn add_diagnostic(&self, diagnostic: Diagnostic) {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.push(diagnostic);
fn add_diagnostic(&mut self, diagnostic: Diagnostic) {
self.diagnostics.push(diagnostic);
}

fn add_diagnostics(&self, mut diagnostics: Vec<Diagnostic>) {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.append(&mut diagnostics);
fn add_diagnostics(&mut self, mut diagnostics: Vec<Diagnostic>) {
self.diagnostics.append(&mut diagnostics);
}

fn clone_diagnostics(&self) -> Vec<Diagnostic> {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.iter()
.cloned()
.collect()
fn diagnostics(&self) -> Cow<[Diagnostic]> {
Cow::Borrowed(&self.diagnostics)
}
}

Expand Down Expand Up @@ -851,12 +830,4 @@ impl NormalModule {
}
Ok(RawStringSource::from(content.into_string_lossy()).boxed())
}

fn clear_diagnostics(&mut self) {
self
.diagnostics
.lock()
.expect("should be able to lock diagnostics")
.clear()
}
}
6 changes: 1 addition & 5 deletions crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;

use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset};
use rspack_collections::Identifiable;
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_macros::impl_source_map_config;
use rspack_sources::{BoxSource, RawStringSource, Source, SourceExt};
use rspack_util::source_map::SourceMapKind;
Expand Down Expand Up @@ -92,10 +92,6 @@ impl DependenciesBlock for RawModule {
impl Module for RawModule {
impl_module_meta_info!();

fn get_diagnostics(&self) -> Vec<Diagnostic> {
vec![]
}

fn module_type(&self) -> &ModuleType {
&ModuleType::JsAuto
}
Expand Down
6 changes: 1 addition & 5 deletions crates/rspack_core/src/self_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use async_trait::async_trait;
use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_macros::impl_source_map_config;
use rspack_sources::Source;
use rspack_util::source_map::SourceMapKind;
Expand Down Expand Up @@ -79,10 +79,6 @@ impl DependenciesBlock for SelfModule {
impl Module for SelfModule {
impl_module_meta_info!();

fn get_diagnostics(&self) -> Vec<Diagnostic> {
vec![]
}

fn size(&self, _source_type: Option<&SourceType>, _compilation: Option<&Compilation>) -> f64 {
self.identifier.len() as f64
}
Expand Down
Loading

2 comments on commit 7fca533

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 7fca533 Feb 12, 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 ✅ success
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 7fca533 Feb 12, 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-12 03cc6cf) Current Change
10000_big_production-mode_disable-minimize + exec 38.8 s ± 1.01 s 39.1 s ± 1.07 s +0.75 %
10000_development-mode + exec 1.88 s ± 100 ms 1.85 s ± 36 ms -1.89 %
10000_development-mode_hmr + exec 685 ms ± 6.4 ms 685 ms ± 7.6 ms -0.06 %
10000_production-mode + exec 2.34 s ± 90 ms 2.29 s ± 37 ms -1.93 %
10000_production-mode_persistent-cold + exec 2.45 s ± 62 ms 2.5 s ± 262 ms +2.05 %
10000_production-mode_persistent-hot + exec 1.68 s ± 167 ms 1.67 s ± 62 ms -0.13 %
arco-pro_development-mode + exec 1.82 s ± 103 ms 1.78 s ± 78 ms -1.88 %
arco-pro_development-mode_hmr + exec 389 ms ± 1.9 ms 388 ms ± 0.91 ms -0.41 %
arco-pro_production-mode + exec 3.63 s ± 161 ms 3.65 s ± 255 ms +0.72 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.71 s ± 33 ms 3.72 s ± 62 ms +0.23 %
arco-pro_production-mode_persistent-cold + exec 3.81 s ± 223 ms 3.76 s ± 86 ms -1.48 %
arco-pro_production-mode_persistent-hot + exec 2.32 s ± 52 ms 2.41 s ± 77 ms +3.81 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.7 s ± 122 ms 3.66 s ± 192 ms -1.19 %
large-dyn-imports_development-mode + exec 2.16 s ± 38 ms 2.13 s ± 101 ms -1.47 %
large-dyn-imports_production-mode + exec 2.16 s ± 27 ms 2.17 s ± 61 ms +0.58 %
threejs_development-mode_10x + exec 1.57 s ± 49 ms 1.54 s ± 30 ms -1.81 %
threejs_development-mode_10x_hmr + exec 788 ms ± 7.2 ms 775 ms ± 12 ms -1.71 %
threejs_production-mode_10x + exec 5.24 s ± 88 ms 5.31 s ± 320 ms +1.30 %
threejs_production-mode_10x_persistent-cold + exec 5.3 s ± 81 ms 5.35 s ± 387 ms +0.89 %
threejs_production-mode_10x_persistent-hot + exec 4.54 s ± 144 ms 4.53 s ± 276 ms -0.24 %
10000_big_production-mode_disable-minimize + rss memory 8691 MiB ± 24.9 MiB 8722 MiB ± 94.9 MiB +0.35 %
10000_development-mode + rss memory 656 MiB ± 28.5 MiB 659 MiB ± 28.1 MiB +0.43 %
10000_development-mode_hmr + rss memory 1286 MiB ± 174 MiB 1297 MiB ± 112 MiB +0.85 %
10000_production-mode + rss memory 627 MiB ± 20.1 MiB 622 MiB ± 25.1 MiB -0.77 %
10000_production-mode_persistent-cold + rss memory 740 MiB ± 25.8 MiB 730 MiB ± 27.1 MiB -1.30 %
10000_production-mode_persistent-hot + rss memory 717 MiB ± 25.4 MiB 709 MiB ± 15.8 MiB -1.15 %
arco-pro_development-mode + rss memory 560 MiB ± 17.8 MiB 570 MiB ± 29.4 MiB +1.85 %
arco-pro_development-mode_hmr + rss memory 637 MiB ± 83 MiB 627 MiB ± 70.5 MiB -1.68 %
arco-pro_production-mode + rss memory 716 MiB ± 24.8 MiB 711 MiB ± 29.5 MiB -0.74 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 727 MiB ± 15.4 MiB 726 MiB ± 33.6 MiB -0.17 %
arco-pro_production-mode_persistent-cold + rss memory 837 MiB ± 63.8 MiB 834 MiB ± 17 MiB -0.34 %
arco-pro_production-mode_persistent-hot + rss memory 706 MiB ± 19.6 MiB 697 MiB ± 29.6 MiB -1.28 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 735 MiB ± 23.3 MiB 716 MiB ± 20.1 MiB -2.59 %
large-dyn-imports_development-mode + rss memory 640 MiB ± 5.5 MiB 638 MiB ± 2.96 MiB -0.39 %
large-dyn-imports_production-mode + rss memory 527 MiB ± 6.62 MiB 524 MiB ± 4.7 MiB -0.66 %
threejs_development-mode_10x + rss memory 546 MiB ± 15.1 MiB 548 MiB ± 16.8 MiB +0.51 %
threejs_development-mode_10x_hmr + rss memory 1183 MiB ± 111 MiB 1116 MiB ± 72.4 MiB -5.65 %
threejs_production-mode_10x + rss memory 834 MiB ± 45 MiB 834 MiB ± 50.7 MiB +0.10 %
threejs_production-mode_10x_persistent-cold + rss memory 962 MiB ± 46.9 MiB 952 MiB ± 32.7 MiB -1.03 %
threejs_production-mode_10x_persistent-hot + rss memory 878 MiB ± 35 MiB 863 MiB ± 31.6 MiB -1.65 %

Please sign in to comment.