Skip to content

Commit 5c60467

Browse files
committed
perf: optimize and get_generated_source_info
1 parent 6a3e298 commit 5c60467

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

src/concat_source.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ fn optimize(children: &mut Vec<BoxSource>) -> Vec<BoxSource> {
523523
}
524524

525525
/// Helper function to merge and flush pending raw sources.
526+
#[inline(always)]
526527
fn merge_raw_sources(
527528
raw_sources: &mut Vec<BoxSource>,
528529
new_children: &mut Vec<BoxSource>,
@@ -538,7 +539,7 @@ fn merge_raw_sources(
538539
let capacity = raw_sources.iter().map(|s| s.size()).sum();
539540
let mut merged_content = String::with_capacity(capacity);
540541
for source in raw_sources.drain(..) {
541-
merged_content.push_str(source.source().into_string_lossy().as_ref());
542+
source.rope(&mut |chunk| merged_content.push_str(chunk));
542543
}
543544
let merged_source = RawStringSource::from(merged_content);
544545
new_children.push(merged_source.boxed());

src/helpers.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,21 +302,30 @@ pub fn split_into_lines(source: &str) -> impl Iterator<Item = &str> {
302302
///
303303
/// See [webpack-sources getGeneratedSourceInfo](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/getGeneratedSourceInfo.js).
304304
pub fn get_generated_source_info(source: &str) -> GeneratedInfo {
305-
let (generated_line, generated_column) = if source.ends_with('\n') {
306-
(split_into_lines(source).count() + 1, 0)
307-
} else {
308-
let mut line_count = 0;
309-
let mut last_line = "";
305+
let bytes = source.as_bytes();
310306

311-
for line in split_into_lines(source) {
312-
line_count += 1;
313-
last_line = line;
314-
}
307+
let mut line_count = 0;
308+
let mut last_newline_pos = None;
309+
310+
for pos in memchr::memchr_iter(b'\n', bytes) {
311+
line_count += 1;
312+
last_newline_pos = Some(pos);
313+
}
315314

316-
(line_count.max(1), last_line.encode_utf16().count())
315+
let generated_column = if let Some(pos) = last_newline_pos {
316+
if pos == bytes.len() - 1 {
317+
0
318+
} else {
319+
#[allow(unsafe_code)]
320+
let last_line_slice = unsafe { source.get_unchecked(pos + 1..) };
321+
last_line_slice.chars().map(|c| c.len_utf16()).sum()
322+
}
323+
} else {
324+
source.chars().map(|c| c.len_utf16()).sum()
317325
};
326+
318327
GeneratedInfo {
319-
generated_line: generated_line as u32,
328+
generated_line: line_count + 1,
320329
generated_column: generated_column as u32,
321330
}
322331
}

src/source.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,8 @@ impl IndexSourceMap {
739739
let idx = global_sources.len() as u32;
740740
source_mapping.insert(source.clone(), idx);
741741
global_sources.push(source.clone());
742-
while global_sources_content.len() < global_sources.len() {
743-
global_sources_content.push("".into());
744-
}
742+
global_sources_content
743+
.resize_with(global_sources.len(), || "".into());
745744
if let Some(content) = map.get_source_content(i) {
746745
global_sources_content[idx as usize] = content.clone();
747746
}

0 commit comments

Comments
 (0)