|
1 | 1 | use std::sync::Arc; |
2 | 2 |
|
3 | | -use rspack_error::Result; |
| 3 | +use rspack_error::{Result, ToStringResultToRspackResultExt}; |
4 | 4 | use rspack_sources::{Mapping, OriginalLocation, encode_mappings}; |
5 | 5 | use rustc_hash::FxHashMap; |
6 | 6 | use swc_core::{ |
@@ -128,42 +128,18 @@ impl JavaScriptCompiler { |
128 | 128 | let map = if source_map_config.enable { |
129 | 129 | let combined_source_map = |
130 | 130 | 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()?; |
167 | 143 | rspack_source_map.set_file(combined_source_map.get_file().map(ToString::to_string)); |
168 | 144 |
|
169 | 145 | Some(rspack_source_map) |
|
0 commit comments