Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(common): Fix perf bug of sourcemap for inputs with multi-byte chars #6523

Merged
merged 11 commits into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/swc/benches/minify.rs
Expand Up @@ -46,7 +46,7 @@ fn bench_minify(b: &mut Bencher, filename: &str) {
module: Default::default(),
safari10: Default::default(),
toplevel: true,
source_map: Default::default(),
source_map: BoolOrDataConfig::from_bool(true),
output_path: Default::default(),
inline_sources_content: true,
emit_source_map_columns: true,
Expand All @@ -56,9 +56,9 @@ fn bench_minify(b: &mut Bencher, filename: &str) {
})
.unwrap();

let res = black_box(res);
black_box(res);

assert_eq!(res.map, None);
// assert_eq!(res.map, None);
})
}

Expand All @@ -84,6 +84,7 @@ fn files_group(c: &mut Criterion) {
bench_file("typescript");
bench_file("victory");
bench_file("vue");
// bench_file("large");
}

criterion_group!(benches, files_group);
Expand Down
4 changes: 3 additions & 1 deletion crates/swc/scripts/instrument.sh
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
set -eu

cargo profile instruments --release -t time --bench typescript --features tracing/release_max_level_info -- --bench $@
export RUST_LOG=off

cargo profile instruments --release -t time --bench minify --features tracing/release_max_level_info -- --bench $@
# MINIFY=1 cargo profile instruments --release -t time --bench typescript --features concurrent,tracing/release_max_level_info $@
1 change: 0 additions & 1 deletion crates/swc_common/src/lib.rs
Expand Up @@ -32,7 +32,6 @@
//!
//! Use `fxhash` instead of `ahash` for `AHashMap` and `AHashSet`.
#![deny(clippy::all)]
#![deny(unused)]
#![cfg_attr(docsrs, feature(doc_cfg))]

use std::fmt::Debug;
Expand Down
37 changes: 32 additions & 5 deletions crates/swc_common/src/source_map.rs
Expand Up @@ -954,7 +954,7 @@ impl SourceMap {
}

fn bytepos_to_file_charpos_with(&self, map: &SourceFile, bpos: BytePos) -> CharPos {
let total_extra_bytes = self.calc_extra_bytes(map, &mut 0, bpos);
let total_extra_bytes = self.calc_extra_bytes(map, &mut 0, &mut 0, bpos);
assert!(
map.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32(),
"map.start_pos = {:?}; total_extra_bytes = {}; bpos = {:?}",
Expand All @@ -966,9 +966,15 @@ impl SourceMap {
}

/// Converts an absolute BytePos to a CharPos relative to the source_file.
fn calc_extra_bytes(&self, map: &SourceFile, start: &mut usize, bpos: BytePos) -> u32 {
fn calc_extra_bytes(
&self,
map: &SourceFile,
prev_total_extra_bytes: &mut u32,
start: &mut usize,
bpos: BytePos,
) -> u32 {
// The number of extra bytes due to multibyte chars in the SourceFile
let mut total_extra_bytes = 0;
let mut total_extra_bytes = *prev_total_extra_bytes;

for (i, &mbc) in map.multibyte_chars[*start..].iter().enumerate() {
debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
Expand All @@ -991,6 +997,8 @@ impl SourceMap {
}
}

*prev_total_extra_bytes = total_extra_bytes;

total_extra_bytes
}

Expand Down Expand Up @@ -1183,6 +1191,11 @@ impl SourceMap {

let mut prev_dst_line = u32::MAX;

let mut prev_extra_bytes = 0;
let mut ch_start = 0;
let mut line_prev_extra_bytes = 0;
let mut line_ch_start = 0;

for (pos, lc) in mappings.iter() {
let pos = *pos;

Expand Down Expand Up @@ -1214,6 +1227,12 @@ impl SourceMap {
builder.set_source_contents(src_id, Some(&f.src));
}

prev_extra_bytes = 0;
ch_start = 0;

line_prev_extra_bytes = 0;
line_ch_start = 0;

cur_file = Some(f.clone());
&f
}
Expand All @@ -1240,6 +1259,7 @@ impl SourceMap {
}

let mut line = a + 1; // Line numbers start at 1

let linebpos = f.lines[a as usize];
debug_assert!(
pos >= linebpos,
Expand All @@ -1248,8 +1268,15 @@ impl SourceMap {
pos,
linebpos,
);
let chpos = pos.to_u32() - self.calc_extra_bytes(f, &mut 0, pos);
let linechpos = linebpos.to_u32() - self.calc_extra_bytes(f, &mut 0, linebpos);
let chpos =
pos.to_u32() - self.calc_extra_bytes(f, &mut prev_extra_bytes, &mut ch_start, pos);
let linechpos = linebpos.to_u32()
- self.calc_extra_bytes(
f,
&mut line_prev_extra_bytes,
&mut line_ch_start,
linebpos,
);

let mut col = max(chpos, linechpos) - min(chpos, linechpos);

Expand Down