Skip to content

Commit 7b5e493

Browse files
committed
chore: try to using rspack_sources::SourceMap::from_slice
1 parent 29e9cbd commit 7b5e493

File tree

3 files changed

+15
-39
lines changed

3 files changed

+15
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ regex-syntax = { version = "0.8.5", default-features = false, features =
8383
regress = { version = "0.10.4", default-features = false, features = ["pattern"] }
8484
ropey = { version = "1.6.1", default-features = false }
8585
rspack_resolver = { features = ["package_json_raw_json_api", "yarn_pnp"], version = "0.6.3", default-features = false }
86-
rspack_sources = { git = "https://github.com/SyMind/rspack-sources.git", rev = "1cbefbd08acf0225a9eae9bb4e01084b6886f650", default-features = false }
86+
rspack_sources = { git = "https://github.com/SyMind/rspack-sources.git", rev = "d35ec0d64967dd368e87ca860b79496f5f27f1fe", default-features = false }
8787
rustc-hash = { version = "2.1.0", default-features = false }
8888
ryu-js = { version = "1.0.2", default-features = false }
8989
scopeguard = { version = "1.2.0", default-features = false }

crates/rspack_javascript_compiler/src/compiler/stringify.rs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use rspack_error::Result;
3+
use rspack_error::{Result, ToStringResultToRspackResultExt};
44
use rspack_sources::{Mapping, OriginalLocation, encode_mappings};
55
use rustc_hash::FxHashMap;
66
use swc_core::{
@@ -128,42 +128,18 @@ impl JavaScriptCompiler {
128128
let map = if source_map_config.enable {
129129
let combined_source_map =
130130
source_map.build_source_map(&src_map_buf, input_source_map.cloned(), source_map_config);
131-
132-
let mappings = encode_mappings(combined_source_map.tokens().map(|token| Mapping {
133-
generated_line: token.get_dst_line() + 1,
134-
generated_column: token.get_dst_col(),
135-
original: if token.has_source() {
136-
Some(OriginalLocation {
137-
source_index: token.get_src_id(),
138-
original_line: token.get_src_line() + 1,
139-
original_column: token.get_src_col(),
140-
name_index: if token.has_name() {
141-
Some(token.get_name_id())
142-
} else {
143-
None
144-
},
145-
})
146-
} else {
147-
None
148-
},
149-
}));
150-
151-
let mut rspack_source_map = rspack_sources::SourceMap::new(
152-
mappings,
153-
combined_source_map
154-
.sources()
155-
.map(ToString::to_string)
156-
.collect::<Vec<_>>(),
157-
combined_source_map
158-
.source_contents()
159-
.flatten()
160-
.map(ToString::to_string)
161-
.collect::<Vec<_>>(),
162-
combined_source_map
163-
.names()
164-
.map(ToString::to_string)
165-
.collect::<Vec<_>>(),
166-
);
131+
let mut output: Vec<u8> = vec![];
132+
combined_source_map
133+
.to_writer(&mut output)
134+
.to_rspack_result()?;
135+
136+
// Performance optimization: Use rspack_sources::SourceMap::from_slice for zero-copy parsing.
137+
// This method provides superior performance and memory efficiency compared to string-based parsing:
138+
// - Zero-copy: Parses directly from byte slice without intermediate string allocation
139+
// - SIMD acceleration: Leverages simd_json for fast JSON parsing
140+
// - Memory efficient: Creates StaticSourceMap variant that borrows from original data
141+
let mut rspack_source_map =
142+
rspack_sources::SourceMap::from_slice(output).to_rspack_result()?;
167143
rspack_source_map.set_file(combined_source_map.get_file().map(ToString::to_string));
168144

169145
Some(rspack_source_map)

0 commit comments

Comments
 (0)