From 79afa6eb061383262aa296db1b747b48bd39883f Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Thu, 5 Dec 2024 18:37:01 +0800 Subject: [PATCH 1/2] perf: avoid lock for logger --- crates/rspack_binding_values/src/stats.rs | 34 ++-- .../src/build_chunk_graph/code_splitter.rs | 9 +- .../rspack_core/src/compiler/compilation.rs | 67 ++++--- crates/rspack_core/src/compiler/mod.rs | 6 +- crates/rspack_core/src/logger.rs | 176 +++++++++++------- crates/rspack_core/src/stats/mod.rs | 9 +- .../rspack_ids/src/named_module_ids_plugin.rs | 3 +- crates/rspack_plugin_banner/src/lib.rs | 3 +- crates/rspack_plugin_copy/src/lib.rs | 16 +- .../src/source_map_dev_tool_plugin.rs | 3 +- .../src/lib.rs | 3 +- .../plugin/flag_dependency_exports_plugin.rs | 3 +- .../src/plugin/infer_async_modules_plugin.rs | 3 +- .../src/plugin/module_concatenation_plugin.rs | 4 +- .../src/lib.rs | 3 +- .../src/lib.rs | 3 +- .../src/plugin/mod.rs | 3 +- .../src/lib.rs | 3 +- 18 files changed, 217 insertions(+), 134 deletions(-) diff --git a/crates/rspack_binding_values/src/stats.rs b/crates/rspack_binding_values/src/stats.rs index b1ac4a3c6e5..70f8e403a63 100644 --- a/crates/rspack_binding_values/src/stats.rs +++ b/crates/rspack_binding_values/src/stats.rs @@ -195,76 +195,76 @@ pub struct JsStatsLogging { pub trace: Option>, } -impl From<(String, rspack_core::LogType)> for JsStatsLogging { - fn from(value: (String, rspack_core::LogType)) -> Self { +impl From<(String, rspack_core::Log)> for JsStatsLogging { + fn from(value: (String, rspack_core::Log)) -> Self { match value.1 { - rspack_core::LogType::Error { message, trace } => Self { + rspack_core::Log::Error { message, trace } => Self { name: value.0, r#type: "error".to_string(), args: Some(vec![message]), trace: Some(trace), }, - rspack_core::LogType::Warn { message, trace } => Self { + rspack_core::Log::Warn { message, trace } => Self { name: value.0, r#type: "warn".to_string(), args: Some(vec![message]), trace: Some(trace), }, - rspack_core::LogType::Info { message } => Self { + rspack_core::Log::Info { message } => Self { name: value.0, r#type: "info".to_string(), args: Some(vec![message]), trace: None, }, - rspack_core::LogType::Log { message } => Self { + rspack_core::Log::Log { message } => Self { name: value.0, r#type: "log".to_string(), args: Some(vec![message]), trace: None, }, - rspack_core::LogType::Debug { message } => Self { + rspack_core::Log::Debug { message } => Self { name: value.0, r#type: "debug".to_string(), args: Some(vec![message]), trace: None, }, - rspack_core::LogType::Trace { message, trace } => Self { + rspack_core::Log::Trace { message, trace } => Self { name: value.0, r#type: "trace".to_string(), args: Some(vec![message]), trace: Some(trace), }, - rspack_core::LogType::Group { message } => Self { + rspack_core::Log::Group { message } => Self { name: value.0, r#type: "group".to_string(), args: Some(vec![message]), trace: None, }, - rspack_core::LogType::GroupCollapsed { message } => Self { + rspack_core::Log::GroupCollapsed { message } => Self { name: value.0, r#type: "groupCollapsed".to_string(), args: Some(vec![message]), trace: None, }, - rspack_core::LogType::GroupEnd => Self { + rspack_core::Log::GroupEnd => Self { name: value.0, r#type: "groupEnd".to_string(), args: None, trace: None, }, - rspack_core::LogType::Profile { label } => Self { + rspack_core::Log::Profile { label } => Self { name: value.0, r#type: "profile".to_string(), args: Some(vec![label.to_string()]), trace: None, }, - rspack_core::LogType::ProfileEnd { label } => Self { + rspack_core::Log::ProfileEnd { label } => Self { name: value.0, r#type: "profileEnd".to_string(), args: Some(vec![label.to_string()]), trace: None, }, - rspack_core::LogType::Time { + rspack_core::Log::Time { label, secs, subsec_nanos, @@ -278,19 +278,19 @@ impl From<(String, rspack_core::LogType)> for JsStatsLogging { )]), trace: None, }, - rspack_core::LogType::Clear => Self { + rspack_core::Log::Clear => Self { name: value.0, r#type: "clear".to_string(), args: None, trace: None, }, - rspack_core::LogType::Status { message } => Self { + rspack_core::Log::Status { message } => Self { name: value.0, r#type: "status".to_string(), args: Some(vec![message]), trace: None, }, - rspack_core::LogType::Cache { label, hit, total } => Self { + rspack_core::Log::Cache { label, hit, total } => Self { name: value.0, r#type: "cache".to_string(), args: Some(vec![format!( diff --git a/crates/rspack_core/src/build_chunk_graph/code_splitter.rs b/crates/rspack_core/src/build_chunk_graph/code_splitter.rs index 10167f0cef8..ccf0411a36a 100644 --- a/crates/rspack_core/src/build_chunk_graph/code_splitter.rs +++ b/crates/rspack_core/src/build_chunk_graph/code_splitter.rs @@ -681,7 +681,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on input_entrypoints_and_modules: UkeyIndexMap>, compilation: &mut Compilation, ) -> Result<()> { - let logger = compilation.get_logger("rspack.buildChunkGraph"); + let mut logger = compilation.get_logger("rspack.buildChunkGraph"); let start = logger.time("prepare entrypoints"); logger.time_end(start); @@ -744,12 +744,13 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on } } + compilation.collect_logger(logger); Ok(()) } #[tracing::instrument(skip_all)] pub fn split(&mut self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger("rspack.buildChunkGraph"); + let mut logger = compilation.get_logger("rspack.buildChunkGraph"); // pop() is used to read from the queue // so it need to be reversed to be iterated in @@ -876,7 +877,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on .incremental .can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH) { - let logger = compilation.get_logger("rspack.incremental.buildChunkGraph"); + let mut logger = compilation.get_logger("rspack.incremental.buildChunkGraph"); logger.log(format!( "{} chunk group created", self.stat_chunk_group_created, @@ -901,9 +902,11 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on "{} cache missed by incorrect available modules", self.stat_cache_miss_by_available_modules, )); + compilation.collect_logger(logger); self.update_cache(compilation); } + compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 9d594c5cc05..119ee82197c 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -798,7 +798,7 @@ impl Compilation { #[instrument(name = "compilation:code_generation", skip_all)] fn code_generation(&mut self, modules: IdentifierSet) -> Result<()> { - let logger = self.get_logger("rspack.Compilation"); + let mut logger = self.get_logger("rspack.Compilation"); let mut codegen_cache_counter = match self.options.cache { CacheOptions::Disabled => None, _ => Some(logger.cache("module code generation cache")), @@ -825,6 +825,7 @@ impl Compilation { logger.cache_end(counter); } + self.collect_logger(logger); Ok(()) } @@ -964,10 +965,10 @@ impl Compilation { #[instrument(skip_all)] async fn create_chunk_assets(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> { - let mutations = self + let chunks = if let Some(mutations) = self .incremental - .mutations_read(IncrementalPasses::CHUNKS_RENDER); - let chunks = if let Some(mutations) = mutations { + .mutations_read(IncrementalPasses::CHUNKS_RENDER) + { let removed_chunks = mutations.iter().filter_map(|mutation| match mutation { Mutation::ChunkRemove { chunk } => Some(*chunk), _ => None, @@ -980,12 +981,13 @@ impl Compilation { .retain(|chunk, _| self.chunk_by_ukey.contains(chunk)); let mut chunks = mutations.get_affected_chunks_with_chunk_graph(self); chunks.extend(self.get_chunk_graph_entries()); - let logger = self.get_logger("rspack.incremental.chunksRender"); + let mut logger = self.get_logger("rspack.incremental.chunksRender"); logger.log(format!( "{} chunks are affected, {} in total", chunks.len(), self.chunk_by_ukey.len() )); + self.collect_logger(logger); chunks } else { self.chunk_by_ukey.keys().copied().collect() @@ -1015,7 +1017,10 @@ impl Compilation { .into_iter() .collect::>>()?; - let chunk_ukey_and_manifest = if mutations.is_some() { + let chunk_ukey_and_manifest = if self + .incremental + .can_read_mutations(IncrementalPasses::CHUNKS_RENDER) + { self.chunk_render_results.extend(chunk_render_results); self.chunk_render_results.clone() } else { @@ -1116,7 +1121,7 @@ impl Compilation { #[instrument(name = "compilation:finish", skip_all)] pub async fn finish(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> { - let logger = self.get_logger("rspack.Compilation"); + let mut logger = self.get_logger("rspack.Compilation"); // Recheck entry and clean useless entry // This should before finish_modules hook is called, ensure providedExports effects on new added modules @@ -1177,15 +1182,16 @@ impl Compilation { let diagnostics = self.make_artifact.take_diagnostics(); self.extend_diagnostics(diagnostics); + self.collect_logger(logger); Ok(()) } #[tracing::instrument(skip_all)] fn collect_dependencies_diagnostics(&mut self) { - let mutations = self + let modules = if let Some(mutations) = self .incremental - .mutations_read(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS); - let modules = if let Some(mutations) = mutations { + .mutations_read(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS) + { let revoked_modules = mutations.iter().filter_map(|mutation| match mutation { Mutation::ModuleRemove { module } => Some(*module), _ => None, @@ -1194,12 +1200,13 @@ impl Compilation { self.dependencies_diagnostics.remove(&revoked_module); } let modules = mutations.get_affected_modules_with_module_graph(&self.get_module_graph()); - let logger = self.get_logger("rspack.incremental.dependenciesDiagnostics"); + let mut logger = self.get_logger("rspack.incremental.dependenciesDiagnostics"); logger.log(format!( "{} modules are affected, {} in total", modules.len(), self.get_module_graph().modules().len() )); + self.collect_logger(logger); modules } else { self.get_module_graph().modules().keys().copied().collect() @@ -1221,7 +1228,10 @@ impl Compilation { (*module_identifier, diagnostics) }) .collect(); - let all_modules_diagnostics = if mutations.is_some() { + let all_modules_diagnostics = if self + .incremental + .can_read_mutations(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS) + { self .dependencies_diagnostics .extend(dependencies_diagnostics); @@ -1235,7 +1245,7 @@ impl Compilation { #[instrument(name = "compilation:seal", skip_all)] pub async fn seal(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> { self.other_module_graph = Some(ModuleGraphPartial::default()); - let logger = self.get_logger("rspack.Compilation"); + let mut logger = self.get_logger("rspack.Compilation"); // https://github.com/webpack/webpack/blob/main/lib/Compilation.js#L2809 plugin_driver.compilation_hooks.seal.call(self).await?; @@ -1321,12 +1331,13 @@ impl Compilation { self.cgm_hash_results.remove(&revoked_module); } let modules = mutations.get_affected_modules_with_chunk_graph(self); - let logger = self.get_logger("rspack.incremental.modulesHashes"); + let mut logger = self.get_logger("rspack.incremental.modulesHashes"); logger.log(format!( "{} modules are affected, {} in total", modules.len(), self.get_module_graph().modules().len() )); + self.collect_logger(logger); modules } else { self.get_module_graph().modules().keys().copied().collect() @@ -1353,12 +1364,13 @@ impl Compilation { self.code_generation_results.remove(&revoked_module); } let modules = mutations.get_affected_modules_with_chunk_graph(self); - let logger = self.get_logger("rspack.incremental.modulesCodegen"); + let mut logger = self.get_logger("rspack.incremental.modulesCodegen"); logger.log(format!( "{} modules are affected, {} in total", modules.len(), self.get_module_graph().modules().len() )); + self.collect_logger(logger); modules } else { self.get_module_graph().modules().keys().copied().collect() @@ -1381,12 +1393,13 @@ impl Compilation { .remove(&revoked_module); } let modules = mutations.get_affected_modules_with_chunk_graph(self); - let logger = self.get_logger("rspack.incremental.modulesRuntimeRequirements"); + let mut logger = self.get_logger("rspack.incremental.modulesRuntimeRequirements"); logger.log(format!( "{} modules are affected, {} in total", modules.len(), self.get_module_graph().modules().len() )); + self.collect_logger(logger); modules } else { self.get_module_graph().modules().keys().copied().collect() @@ -1419,12 +1432,13 @@ impl Compilation { self .cgc_runtime_requirements_results .retain(|chunk, _| self.chunk_by_ukey.contains(chunk)); - let logger = self.get_logger("rspack.incremental.chunksRuntimeRequirements"); + let mut logger = self.get_logger("rspack.incremental.chunksRuntimeRequirements"); logger.log(format!( "{} chunks are affected, {} in total", affected_chunks.len(), self.chunk_by_ukey.len() )); + self.collect_logger(logger); affected_chunks } else { self.chunk_by_ukey.keys().copied().collect() @@ -1454,12 +1468,13 @@ impl Compilation { .chunk_hashes_results .retain(|chunk, _| self.chunk_by_ukey.contains(chunk)); let chunks = mutations.get_affected_chunks_with_chunk_graph(self); - let logger = self.get_logger("rspack.incremental.chunksHashes"); + let mut logger = self.get_logger("rspack.incremental.chunksHashes"); logger.log(format!( "{} chunks are affected, {} in total", chunks.len(), self.chunk_by_ukey.len(), )); + self.collect_logger(logger); chunks } else { self.chunk_by_ukey.keys().copied().collect() @@ -1494,6 +1509,7 @@ impl Compilation { self.after_seal(plugin_driver).await?; logger.time_end(start); + self.collect_logger(logger); Ok(()) } @@ -1556,7 +1572,7 @@ impl Compilation { modules: IdentifierSet, plugin_driver: SharedPluginDriver, ) -> Result<()> { - let logger = self.get_logger("rspack.Compilation"); + let mut logger = self.get_logger("rspack.Compilation"); let start = logger.time("runtime requirements.modules"); let results: Vec<(ModuleIdentifier, RuntimeSpecMap)> = modules .into_par_iter() @@ -1607,6 +1623,7 @@ impl Compilation { ChunkGraph::set_module_runtime_requirements(self, module, map); } logger.time_end(start); + self.collect_logger(logger); Ok(()) } @@ -1617,7 +1634,7 @@ impl Compilation { entries: UkeySet, plugin_driver: SharedPluginDriver, ) -> Result<()> { - let logger = self.get_logger("rspack.Compilation"); + let mut logger = self.get_logger("rspack.Compilation"); let start = logger.time("runtime requirements.chunks"); let mut chunk_requirements = HashMap::default(); for chunk_ukey in chunks.iter().chain(entries.iter()) { @@ -1719,6 +1736,7 @@ impl Compilation { } logger.time_end(start); + self.collect_logger(logger); Ok(()) } @@ -1728,7 +1746,7 @@ impl Compilation { create_hash_chunks: UkeySet, plugin_driver: SharedPluginDriver, ) -> Result<()> { - let logger = self.get_logger("rspack.Compilation"); + let mut logger = self.get_logger("rspack.Compilation"); let mut compilation_hasher = RspackHash::from(&self.options.output); fn try_process_chunk_hash_results( @@ -1960,6 +1978,7 @@ impl Compilation { ); } logger.time_end(start); + self.collect_logger(logger); Ok(()) } @@ -2123,7 +2142,11 @@ impl Compilation { } pub fn get_logger(&self, name: impl Into) -> CompilationLogger { - CompilationLogger::new(name.into(), self.logging.clone()) + CompilationLogger::new(name.into()) + } + + pub fn collect_logger(&mut self, logger: CompilationLogger) { + self.logging.collect_logger(logger); } pub fn set_dependency_factory( diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index 447f69b8c4b..47ff8c6ca50 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -197,7 +197,7 @@ impl Compiler { .call(&mut self.compilation, &mut compilation_params) .await?; - let logger = self.compilation.get_logger("rspack.Compiler"); + let mut logger = self.compilation.get_logger("rspack.Compiler"); let make_start = logger.time("make"); let make_hook_start = logger.time("make hook"); if let Some(e) = self @@ -241,7 +241,7 @@ impl Compiler { #[instrument(name = "compile_done", skip_all)] async fn compile_done(&mut self) -> Result<()> { - let logger = self.compilation.get_logger("rspack.Compiler"); + let mut logger = self.compilation.get_logger("rspack.Compiler"); if matches!( self @@ -258,7 +258,7 @@ impl Compiler { let start = logger.time("emitAssets"); self.emit_assets().await?; logger.time_end(start); - + self.compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_core/src/logger.rs b/crates/rspack_core/src/logger.rs index 0ceeeff5c7f..89ba85d2984 100644 --- a/crates/rspack_core/src/logger.rs +++ b/crates/rspack_core/src/logger.rs @@ -1,15 +1,12 @@ use std::{ backtrace::Backtrace, - hash::BuildHasherDefault, - sync::Arc, time::{Duration, Instant}, }; -use dashmap::DashMap; -use rustc_hash::FxHasher; +use rustc_hash::FxHashMap; #[derive(Debug, Clone)] -pub enum LogType { +pub enum Log { Error { message: String, trace: Vec, @@ -60,24 +57,24 @@ pub enum LogType { }, } -impl LogType { +impl Log { pub fn to_bit_flag(&self) -> u32 { match self { - LogType::Error { .. } => 1 << 0, - LogType::Warn { .. } => 1 << 1, - LogType::Info { .. } => 1 << 2, - LogType::Log { .. } => 1 << 3, - LogType::Debug { .. } => 1 << 4, - LogType::Trace { .. } => 1 << 5, - LogType::Group { .. } => 1 << 6, - LogType::GroupCollapsed { .. } => 1 << 7, - LogType::GroupEnd => 1 << 8, - LogType::Profile { .. } => 1 << 9, - LogType::ProfileEnd { .. } => 1 << 10, - LogType::Time { .. } => 1 << 11, - LogType::Clear => 1 << 12, - LogType::Status { .. } => 1 << 13, - LogType::Cache { .. } => 1 << 14, + Log::Error { .. } => 1 << 0, + Log::Warn { .. } => 1 << 1, + Log::Info { .. } => 1 << 2, + Log::Log { .. } => 1 << 3, + Log::Debug { .. } => 1 << 4, + Log::Trace { .. } => 1 << 5, + Log::Group { .. } => 1 << 6, + Log::GroupCollapsed { .. } => 1 << 7, + Log::GroupEnd => 1 << 8, + Log::Profile { .. } => 1 << 9, + Log::ProfileEnd { .. } => 1 << 10, + Log::Time { .. } => 1 << 11, + Log::Clear => 1 << 12, + Log::Status { .. } => 1 << 13, + Log::Cache { .. } => 1 << 14, } } } @@ -95,85 +92,85 @@ fn capture_trace() -> Vec { } pub trait Logger { - fn raw(&self, log_type: LogType); + fn raw(&mut self, log_type: Log); - fn error(&self, message: impl Into) { - self.raw(LogType::Error { + fn error(&mut self, message: impl Into) { + self.raw(Log::Error { message: message.into(), trace: capture_trace(), }) } - fn warn(&self, message: impl Into) { - self.raw(LogType::Warn { + fn warn(&mut self, message: impl Into) { + self.raw(Log::Warn { message: message.into(), trace: capture_trace(), }) } - fn info(&self, message: impl Into) { - self.raw(LogType::Info { + fn info(&mut self, message: impl Into) { + self.raw(Log::Info { message: message.into(), }) } - fn log(&self, message: impl Into) { - self.raw(LogType::Log { + fn log(&mut self, message: impl Into) { + self.raw(Log::Log { message: message.into(), }) } - fn debug(&self, message: impl Into) { - self.raw(LogType::Debug { + fn debug(&mut self, message: impl Into) { + self.raw(Log::Debug { message: message.into(), }) } - fn assert(&self, assertion: bool, message: impl Into) { + fn assert(&mut self, assertion: bool, message: impl Into) { if !assertion { self.error(message); } } - fn trace(&self) { - self.raw(LogType::Trace { + fn trace(&mut self) { + self.raw(Log::Trace { message: "Trace".to_string(), trace: capture_trace(), }) } - fn clear(&self) { - self.raw(LogType::Clear) + fn clear(&mut self) { + self.raw(Log::Clear) } - fn status(&self, message: impl Into) { - self.raw(LogType::Status { + fn status(&mut self, message: impl Into) { + self.raw(Log::Status { message: message.into(), }) } - fn profile(&self, label: &'static str) { - self.raw(LogType::Profile { label }) + fn profile(&mut self, label: &'static str) { + self.raw(Log::Profile { label }) } - fn profile_end(&self, label: &'static str) { - self.raw(LogType::ProfileEnd { label }) + fn profile_end(&mut self, label: &'static str) { + self.raw(Log::ProfileEnd { label }) } - fn group(&self, message: impl Into) { - self.raw(LogType::Group { + fn group(&mut self, message: impl Into) { + self.raw(Log::Group { message: message.into(), }) } - fn group_collapsed(&self, message: impl Into) { - self.raw(LogType::GroupCollapsed { + fn group_collapsed(&mut self, message: impl Into) { + self.raw(Log::GroupCollapsed { message: message.into(), }) } - fn group_end(&self) { - self.raw(LogType::GroupEnd) + fn group_end(&mut self) { + self.raw(Log::GroupEnd) } fn time(&self, label: &'static str) -> StartTime { @@ -183,18 +180,18 @@ pub trait Logger { } } - fn time_log(&self, start: &StartTime) { + fn time_log(&mut self, start: &StartTime) { let elapsed = start.elapsed(); let secs = elapsed.as_secs(); let subsec_nanos = elapsed.subsec_nanos(); - self.raw(LogType::Time { + self.raw(Log::Time { label: start.label, secs, subsec_nanos, }) } - fn time_end(&self, start: StartTime) { + fn time_end(&mut self, start: StartTime) { self.time_log(&start) } @@ -205,10 +202,10 @@ pub trait Logger { } } - fn time_aggregate_end(&self, start: StartTimeAggregate) { + fn time_aggregate_end(&mut self, start: StartTimeAggregate) { let secs = start.duration.as_secs(); let subsec_nanos = start.duration.subsec_nanos(); - self.raw(LogType::Time { + self.raw(Log::Time { label: start.label, secs, subsec_nanos, @@ -223,9 +220,9 @@ pub trait Logger { } } - fn cache_end(&self, count: CacheCount) { + fn cache_end(&mut self, count: CacheCount) { if count.total != 0 { - self.raw(LogType::Cache { + self.raw(Log::Cache { label: count.label, hit: count.hit, total: count.total, @@ -288,25 +285,72 @@ impl CacheCount { } } -pub type CompilationLogging = Arc, BuildHasherDefault>>; +#[derive(Debug, Default)] +pub struct CompilationLogging { + loggers: FxHashMap, +} + +impl CompilationLogging { + pub fn collect_logger(&mut self, logger: CompilationLogger) { + self + .loggers + .entry(logger.name) + .or_default() + .extend(logger.logs); + } + + pub fn iter(&self) -> impl Iterator { + self.loggers.iter() + } +} +#[derive(Debug)] pub struct CompilationLogger { - logging: CompilationLogging, + logs: Logs, name: String, } impl CompilationLogger { - pub fn new(name: String, logging: CompilationLogging) -> Self { - Self { logging, name } + pub fn new(name: String) -> Self { + Self { + logs: Default::default(), + name, + } + } + + pub fn collect_logs(&mut self, logs: Logs) { + self.logs.extend(logs); } } impl Logger for CompilationLogger { - fn raw(&self, log_type: LogType) { - if let Some(mut value) = self.logging.get_mut(&self.name) { - value.push(log_type); - } else { - self.logging.insert(self.name.clone(), vec![log_type]); - } + fn raw(&mut self, log_type: Log) { + self.logs.raw(log_type); + } +} + +#[derive(Debug, Default, Clone)] +pub struct Logs { + inner: Vec, +} + +impl Logger for Logs { + fn raw(&mut self, log_type: Log) { + self.inner.push(log_type); + } +} + +impl IntoIterator for Logs { + type Item = Log; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.inner.into_iter() + } +} + +impl Extend for Logs { + fn extend>(&mut self, iter: T) { + self.inner.extend(iter) } } diff --git a/crates/rspack_core/src/stats/mod.rs b/crates/rspack_core/src/stats/mod.rs index a0adc311b90..1e92ac41885 100644 --- a/crates/rspack_core/src/stats/mod.rs +++ b/crates/rspack_core/src/stats/mod.rs @@ -18,7 +18,7 @@ pub use r#struct::*; use crate::{ BoxModule, BoxRuntimeModule, Chunk, ChunkGraph, ChunkGroupOrderKey, ChunkGroupUkey, ChunkUkey, - Compilation, ExecutedRuntimeModule, LogType, ModuleGraph, ModuleIdentifier, ProvidedExports, + Compilation, ExecutedRuntimeModule, Log, ModuleGraph, ModuleIdentifier, ProvidedExports, SourceType, UsedExports, }; @@ -646,15 +646,12 @@ impl Stats<'_> { .collect() } - pub fn get_logging(&self) -> Vec<(String, LogType)> { + pub fn get_logging(&self) -> Vec<(String, Log)> { self .compilation .get_logging() .iter() - .map(|item| { - let (name, logs) = item.pair(); - (name.to_owned(), logs.to_owned()) - }) + .map(|(name, logs)| (name.to_owned(), logs.to_owned())) .sorted_by(|a, b| a.0.cmp(&b.0)) .flat_map(|item| item.1.into_iter().map(move |log| (item.0.clone(), log))) .collect() diff --git a/crates/rspack_ids/src/named_module_ids_plugin.rs b/crates/rspack_ids/src/named_module_ids_plugin.rs index 1cea348c74c..ed9f01759c0 100644 --- a/crates/rspack_ids/src/named_module_ids_plugin.rs +++ b/crates/rspack_ids/src/named_module_ids_plugin.rs @@ -204,7 +204,7 @@ fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { .can_read_mutations(IncrementalPasses::MODULE_IDS) && let Some(mutations) = &mutations { - let logger = compilation.get_logger("rspack.incremental.moduleIds"); + let mut logger = compilation.get_logger("rspack.incremental.moduleIds"); logger.log(format!( "{} modules are affected, {} in total", modules_len, @@ -215,6 +215,7 @@ fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { mutations.len(), unnamed_modules_len, )); + compilation.collect_logger(logger); } if let Some(compilation_mutations) = compilation.incremental.mutations_write() diff --git a/crates/rspack_plugin_banner/src/lib.rs b/crates/rspack_plugin_banner/src/lib.rs index 6144ec3644f..753d3cabcbc 100644 --- a/crates/rspack_plugin_banner/src/lib.rs +++ b/crates/rspack_plugin_banner/src/lib.rs @@ -142,7 +142,7 @@ impl BannerPlugin { #[plugin_hook(CompilationProcessAssets for BannerPlugin, stage = self.config.stage.unwrap_or(Compilation::PROCESS_ASSETS_STAGE_ADDITIONS))] async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger("rspack.BannerPlugin"); + let mut logger = compilation.get_logger("rspack.BannerPlugin"); let start = logger.time("add banner"); let mut updates = vec![]; @@ -211,6 +211,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { logger.time_end(start); + compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_plugin_copy/src/lib.rs b/crates/rspack_plugin_copy/src/lib.rs index 171654590a2..ac8acd61854 100644 --- a/crates/rspack_plugin_copy/src/lib.rs +++ b/crates/rspack_plugin_copy/src/lib.rs @@ -17,7 +17,7 @@ use regex::Regex; use rspack_core::{ rspack_sources::{RawSource, Source}, AssetInfo, AssetInfoRelated, Compilation, CompilationAsset, CompilationLogger, - CompilationProcessAssets, FilenameTemplate, Logger, PathData, Plugin, + CompilationProcessAssets, FilenameTemplate, Logger, Logs, PathData, Plugin, }; use rspack_error::{Diagnostic, DiagnosticError, Error, ErrorExt, Result}; use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest}; @@ -124,6 +124,7 @@ pub struct RunPatternResult { pub info: Option, pub force: bool, pub priority: i32, + pub logs: Logs, } #[plugin] @@ -163,8 +164,8 @@ impl CopyRspackPlugin { file_dependencies: &DashSet, diagnostics: &Mutex>, compilation: &Compilation, - logger: &CompilationLogger, ) -> Option { + let mut logger = Logs::default(); // Exclude directories if entry.is_dir() { return None; @@ -348,6 +349,7 @@ impl CopyRspackPlugin { info: pattern.info.clone(), force: pattern.force, priority: pattern.priority, + logs: logger, }) } @@ -358,7 +360,7 @@ impl CopyRspackPlugin { file_dependencies: &DashSet, context_dependencies: &DashSet, diagnostics: &Mutex>, - logger: &CompilationLogger, + logger: &mut CompilationLogger, ) -> Option>> { let orig_from = &pattern.from; let normalized_orig_from = Utf8PathBuf::from(orig_from); @@ -516,7 +518,6 @@ impl CopyRspackPlugin { file_dependencies, diagnostics, compilation, - logger, ) .await }) @@ -574,7 +575,7 @@ impl CopyRspackPlugin { #[plugin_hook(CompilationProcessAssets for CopyRspackPlugin, stage = Compilation::PROCESS_ASSETS_STAGE_ADDITIONAL)] async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger("rspack.CopyRspackPlugin"); + let mut logger = compilation.get_logger("rspack.CopyRspackPlugin"); let start = logger.time("run pattern"); let file_dependencies = DashSet::default(); let context_dependencies = DashSet::default(); @@ -592,7 +593,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { &file_dependencies, &context_dependencies, &diagnostics, - &logger, + &mut logger, ) }) .collect::>() @@ -624,6 +625,8 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { copied_result.sort_unstable_by(|a, b| a.0.cmp(&b.0)); copied_result.into_iter().for_each(|(_priority, result)| { + logger.collect_logs(result.logs); + if let Some(exist_asset) = compilation.assets_mut().get_mut(&result.filename) { if !result.force { return; @@ -656,6 +659,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { }); logger.time_end(start); + compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs b/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs index 22289907152..c913035c3d3 100644 --- a/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs +++ b/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs @@ -538,7 +538,7 @@ impl SourceMapDevToolPlugin { #[plugin_hook(CompilationProcessAssets for SourceMapDevToolPlugin, stage = Compilation::PROCESS_ASSETS_STAGE_DEV_TOOLING)] async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger("rspack.SourceMapDevToolPlugin"); + let mut logger = compilation.get_logger("rspack.SourceMapDevToolPlugin"); // use to read let mut file_to_chunk: HashMap<&String, &Chunk> = HashMap::default(); @@ -594,6 +594,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { } } logger.time_end(start); + compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs b/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs index 5e7b6823e01..5093ee3f929 100644 --- a/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs +++ b/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs @@ -9,7 +9,7 @@ pub struct EnsureChunkConditionsPlugin; #[plugin_hook(CompilationOptimizeChunks for EnsureChunkConditionsPlugin, stage = Compilation::OPTIMIZE_CHUNKS_STAGE_BASIC)] fn optimize_chunks(&self, compilation: &mut Compilation) -> Result> { - let logger = compilation.get_logger(self.name()); + let mut logger = compilation.get_logger(self.name()); let start = logger.time("ensure chunk conditions"); let mut source_module_chunks = HashMap::default(); @@ -94,6 +94,7 @@ fn optimize_chunks(&self, compilation: &mut Compilation) -> Result> logger.time_end(start); + compilation.collect_logger(logger); Ok(None) } diff --git a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs index 978ccd83b83..ca7ba8c15c5 100644 --- a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs @@ -359,12 +359,13 @@ async fn finish_modules(&self, compilation: &mut Compilation) -> Result<()> { .mutations_read(IncrementalPasses::PROVIDED_EXPORTS) { let modules = mutations.get_affected_modules_with_module_graph(&compilation.get_module_graph()); - let logger = compilation.get_logger("rspack.incremental.providedExports"); + let mut logger = compilation.get_logger("rspack.incremental.providedExports"); logger.log(format!( "{} modules are affected, {} in total", modules.len(), compilation.get_module_graph().modules().len() )); + compilation.collect_logger(logger); modules } else { compilation diff --git a/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs index c0bef3f124c..b4154aedeff 100644 --- a/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs @@ -75,7 +75,7 @@ async fn finish_modules(&self, compilation: &mut Compilation) -> Result<()> { .can_read_mutations(IncrementalPasses::INFER_ASYNC_MODULES) && let Some(mutations) = &mutations { - let logger = compilation.get_logger("rspack.incremental.inferAsyncModules"); + let mut logger = compilation.get_logger("rspack.incremental.inferAsyncModules"); logger.log(format!( "{} modules are affected, {} in total", modules_len, total_modules_len, @@ -84,6 +84,7 @@ async fn finish_modules(&self, compilation: &mut Compilation) -> Result<()> { "{} modules are updated by set_async", mutations.len() )); + compilation.collect_logger(logger); } if let Some(compilation_mutations) = compilation.incremental.mutations_write() diff --git a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs index a3b5f012235..c49a64e8252 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -710,7 +710,7 @@ impl ModuleConcatenationPlugin { } async fn optimize_chunk_modules_impl(&self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger("rspack.ModuleConcatenationPlugin"); + let mut logger = compilation.get_logger("rspack.ModuleConcatenationPlugin"); let mut relevant_modules = vec![]; let mut possible_inners = IdentifierSet::default(); let start = logger.time("select relevant modules"); @@ -1020,6 +1020,8 @@ impl ModuleConcatenationPlugin { for config in concat_configurations { Self::process_concatenated_configuration(compilation, config, &mut used_modules).await?; } + + compilation.collect_logger(logger); Ok(()) } } diff --git a/crates/rspack_plugin_real_content_hash/src/lib.rs b/crates/rspack_plugin_real_content_hash/src/lib.rs index 4d3602c5c9b..bf56f2734c2 100644 --- a/crates/rspack_plugin_real_content_hash/src/lib.rs +++ b/crates/rspack_plugin_real_content_hash/src/lib.rs @@ -53,7 +53,7 @@ impl Plugin for RealContentHashPlugin { } fn inner_impl(compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger("rspack.RealContentHashPlugin"); + let mut logger = compilation.get_logger("rspack.RealContentHashPlugin"); let start = logger.time("hash to asset names"); let mut hash_to_asset_names: HashMap<&str, Vec<&str>> = HashMap::default(); for (name, asset) in compilation @@ -175,6 +175,7 @@ fn inner_impl(compilation: &mut Compilation) -> Result<()> { } logger.time_end(start); + compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_plugin_remove_empty_chunks/src/lib.rs b/crates/rspack_plugin_remove_empty_chunks/src/lib.rs index ec649980519..1d63fb2e814 100644 --- a/crates/rspack_plugin_remove_empty_chunks/src/lib.rs +++ b/crates/rspack_plugin_remove_empty_chunks/src/lib.rs @@ -11,7 +11,7 @@ pub struct RemoveEmptyChunksPlugin; impl RemoveEmptyChunksPlugin { fn remove_empty_chunks(&self, compilation: &mut Compilation) { - let logger = compilation.get_logger(self.name()); + let mut logger = compilation.get_logger(self.name()); let start = logger.time("remove empty chunks"); let chunk_graph = &mut compilation.chunk_graph; @@ -36,6 +36,7 @@ impl RemoveEmptyChunksPlugin { }); logger.time_end(start); + compilation.collect_logger(logger); } } diff --git a/crates/rspack_plugin_split_chunks/src/plugin/mod.rs b/crates/rspack_plugin_split_chunks/src/plugin/mod.rs index d58761da86f..9f7cdf5916b 100644 --- a/crates/rspack_plugin_split_chunks/src/plugin/mod.rs +++ b/crates/rspack_plugin_split_chunks/src/plugin/mod.rs @@ -43,7 +43,7 @@ impl SplitChunksPlugin { } fn inner_impl(&self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger(self.name()); + let mut logger = compilation.get_logger(self.name()); let start = logger.time("prepare module group map"); let mut module_group_map = self.prepare_module_group_map(compilation)?; tracing::trace!("prepared module_group_map {:#?}", module_group_map); @@ -150,6 +150,7 @@ impl SplitChunksPlugin { self.ensure_max_size_fit(compilation, max_size_setting_map)?; logger.time_end(start); + compilation.collect_logger(logger); Ok(()) } } diff --git a/crates/rspack_plugin_warn_sensitive_module/src/lib.rs b/crates/rspack_plugin_warn_sensitive_module/src/lib.rs index 83518e900db..0728693b542 100644 --- a/crates/rspack_plugin_warn_sensitive_module/src/lib.rs +++ b/crates/rspack_plugin_warn_sensitive_module/src/lib.rs @@ -48,7 +48,7 @@ impl WarnCaseSensitiveModulesPlugin { #[plugin_hook(CompilationSeal for WarnCaseSensitiveModulesPlugin)] async fn seal(&self, compilation: &mut Compilation) -> Result<()> { - let logger = compilation.get_logger(self.name()); + let mut logger = compilation.get_logger(self.name()); let start = logger.time("check case sensitive modules"); let mut diagnostics: Vec = vec![]; let module_graph = compilation.get_module_graph(); @@ -99,6 +99,7 @@ async fn seal(&self, compilation: &mut Compilation) -> Result<()> { compilation.extend_diagnostics(diagnostics); logger.time_end(start); + compilation.collect_logger(logger); Ok(()) } From 04481af0c98c9c2cfb7693f849215dd617e70d40 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Thu, 5 Dec 2024 20:55:32 +0800 Subject: [PATCH 2/2] fix --- crates/rspack_core/src/compiler/mod.rs | 1 + crates/rspack_plugin_real_content_hash/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index 47ff8c6ca50..bc43d92206b 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -236,6 +236,7 @@ impl Compiler { .compilation .extend_diagnostics(plugin_driver_diagnostics); + self.compilation.collect_logger(logger); Ok(()) } diff --git a/crates/rspack_plugin_real_content_hash/src/lib.rs b/crates/rspack_plugin_real_content_hash/src/lib.rs index bf56f2734c2..62acd452df1 100644 --- a/crates/rspack_plugin_real_content_hash/src/lib.rs +++ b/crates/rspack_plugin_real_content_hash/src/lib.rs @@ -71,6 +71,7 @@ fn inner_impl(compilation: &mut Compilation) -> Result<()> { } logger.time_end(start); if hash_to_asset_names.is_empty() { + compilation.collect_logger(logger); return Ok(()); } let start = logger.time("create hash regexp");