From a211427f39a6e08adda7fe1e5d28d933fb6af59a Mon Sep 17 00:00:00 2001 From: Hana Date: Thu, 5 Dec 2024 22:04:34 +0800 Subject: [PATCH] perf: use `MapCachedSource` --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- crates/rspack_binding_values/src/source.rs | 2 +- .../src/with/as_preset/rspack_sources/mod.rs | 2 +- .../tests/with/as_preset/rspack_sources.rs | 4 ++-- crates/rspack_core/src/concatenated_module.rs | 6 ++++-- crates/rspack_core/src/normal_module.rs | 7 +++++-- .../src/eval_source_map_dev_tool_plugin.rs | 4 +++- .../src/source_map_dev_tool_plugin.rs | 2 +- crates/rspack_plugin_lightning_css_minimizer/src/lib.rs | 2 +- crates/rspack_plugin_swc_js_minimizer/src/lib.rs | 2 +- 11 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bfd3308e0a..33723d8d801 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4917,9 +4917,9 @@ dependencies = [ [[package]] name = "rspack_sources" version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d52f5aa6afd8cc49a3e3f796a00918d11d97ed5043d0881165d8fc1aca1ae8c" +source = "git+https://github.com/web-infra-dev/rspack-sources?rev=30b24267f406a03ce8d958a9acf2f8db9d50913c#30b24267f406a03ce8d958a9acf2f8db9d50913c" dependencies = [ + "bumpalo", "dashmap 5.5.3", "dyn-clone", "memchr", diff --git a/Cargo.toml b/Cargo.toml index affa51f803d..00e54469b0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,7 +66,7 @@ rayon = { version = "1.10.0" } regex = { version = "1.11.1" } ropey = "1.6.1" rspack_resolver = { version = "0.3.5", features = ["package_json_raw_json_api"] } -rspack_sources = { version = "=0.3.6" } +rspack_sources = { git = "https://github.com/web-infra-dev/rspack-sources", rev = "30b24267f406a03ce8d958a9acf2f8db9d50913c" } rustc-hash = { version = "2.1.0" } serde = { version = "1.0.215" } serde_json = { version = "1.0.133" } diff --git a/crates/rspack_binding_values/src/source.rs b/crates/rspack_binding_values/src/source.rs index a6708088a21..298d61d931c 100644 --- a/crates/rspack_binding_values/src/source.rs +++ b/crates/rspack_binding_values/src/source.rs @@ -319,7 +319,7 @@ impl ToJsCompatSourceOwned for dyn Source + '_ { } fn to_webpack_map(source: &dyn Source) -> Result> { - let map = source.map(&MapOptions::default()); + let map = source.map(&MapOptions::default(), &Default::default()); map .map(|m| m.to_json()) diff --git a/crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs b/crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs index 7ca6b637542..39c156c7004 100644 --- a/crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs +++ b/crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs @@ -41,7 +41,7 @@ where field: &BoxSource, serializer: &mut S, ) -> Result { - let map = match field.map(&Default::default()) { + let map = match field.map(&Default::default(), &Default::default()) { Some(map) => Some( map .to_json() diff --git a/crates/rspack_cacheable_test/tests/with/as_preset/rspack_sources.rs b/crates/rspack_cacheable_test/tests/with/as_preset/rspack_sources.rs index 9a1fd765351..4e28511d3ee 100644 --- a/crates/rspack_cacheable_test/tests/with/as_preset/rspack_sources.rs +++ b/crates/rspack_cacheable_test/tests/with/as_preset/rspack_sources.rs @@ -12,8 +12,8 @@ fn test_rspack_source() { let new_data: Data = from_bytes(&bytes, &()).unwrap(); assert_eq!(data.0.buffer(), new_data.0.buffer()); assert_eq!( - data.0.map(&Default::default()), - new_data.0.map(&Default::default()) + data.0.map(&Default::default(), &Default::default()), + new_data.0.map(&Default::default(), &Default::default()) ); } diff --git a/crates/rspack_core/src/concatenated_module.rs b/crates/rspack_core/src/concatenated_module.rs index e2a295aba0a..71eea247e95 100644 --- a/crates/rspack_core/src/concatenated_module.rs +++ b/crates/rspack_core/src/concatenated_module.rs @@ -17,7 +17,9 @@ use rspack_collections::{ use rspack_error::{Diagnosable, Diagnostic, DiagnosticKind, Result, TraceableError}; use rspack_hash::{HashDigest, HashFunction, RspackHash}; use rspack_hook::define_hook; -use rspack_sources::{ConcatSource, RawSource, RawStringSource, ReplaceSource, Source, SourceExt}; +use rspack_sources::{ + ConcatSource, MapCachedSource, RawStringSource, ReplaceSource, Source, SourceExt, +}; use rspack_util::{ext::DynHash, itoa, source_map::SourceMapKind, swc::join_atom}; use rustc_hash::FxHasher; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; @@ -1280,7 +1282,7 @@ impl Module for ConcatenatedModule { } let mut code_generation_result = CodeGenerationResult::default(); - code_generation_result.add(SourceType::JavaScript, result.boxed()); + code_generation_result.add(SourceType::JavaScript, MapCachedSource::new(result).boxed()); code_generation_result.chunk_init_fragments = chunk_init_fragments; code_generation_result.runtime_requirements = runtime_requirements; code_generation_result diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index 968eb3f1d00..2ecfd193867 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -18,7 +18,7 @@ use rspack_hook::define_hook; use rspack_loader_runner::{run_loaders, AdditionalData, Content, LoaderContext, ResourceData}; use rspack_macros::impl_source_map_config; use rspack_sources::{ - BoxSource, OriginalSource, RawBufferSource, RawSource, RawStringSource, Source, SourceExt, + BoxSource, MapCachedSource, OriginalSource, RawBufferSource, RawStringSource, Source, SourceExt, SourceMap, SourceMapSource, WithoutOriginalOptions, }; use rspack_util::{ @@ -650,7 +650,10 @@ impl Module for NormalModule { concatenation_scope: concatenation_scope.as_mut(), }, )?; - code_generation_result.add(*source_type, generation_result.boxed()); + code_generation_result.add( + *source_type, + MapCachedSource::new(generation_result).boxed(), + ); } code_generation_result.concatenation_scope = concatenation_scope; Ok(code_generation_result) diff --git a/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs b/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs index f55fe2cf123..3f37afe51da 100644 --- a/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs +++ b/crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs @@ -95,7 +95,9 @@ fn eval_source_map_devtool_plugin_render_module_content( if let Some(cached_source) = self.cache.get(&origin_source) { render_source.source = cached_source.value().clone(); return Ok(()); - } else if let Some(mut map) = origin_source.map(&MapOptions::new(self.columns)) { + } else if let Some(mut map) = + origin_source.map(&MapOptions::new(self.columns), &Default::default()) + { let source = { let source = &origin_source.source(); 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 6f30034eaa5..413226fd699 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 @@ -208,7 +208,7 @@ impl SourceMapDevToolPlugin { }; let source = if is_match { asset.get_source().map(|source| { - let source_map = source.map(&map_options); + let source_map = source.map(&map_options, &Default::default()); (file, source, source_map) }) } else { diff --git a/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs b/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs index 351b179d244..9ca4abc6c97 100644 --- a/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs +++ b/crates/rspack_plugin_lightning_css_minimizer/src/lib.rs @@ -169,7 +169,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { if let Some(original_source) = original.get_source() { let input = original_source.source().into_owned(); - let input_source_map = original_source.map(&MapOptions::default()); + let input_source_map = original_source.map(&MapOptions::default(), &Default::default()); let mut parser_flags = ParserFlags::empty(); parser_flags.set( diff --git a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs index 02402c86785..51ac4dad39f 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs @@ -186,7 +186,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { let filename = filename.split('?').next().expect("Should have filename"); if let Some(original_source) = original.get_source() { let input = original_source.source().to_string(); - let input_source_map = original_source.map(&MapOptions::default()); + let input_source_map = original_source.map(&MapOptions::default(), &Default::default()); let is_module = if let Some(module) = minimizer_options.module { Some(module)