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(sourcemap): speed up encoding mappings #4492

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions crates/oxc_sourcemap/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,52 +132,63 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {

let capacity = ((end - start) * 10) as usize;

let mut rv = String::with_capacity(capacity);
let mut rv_str = String::with_capacity(capacity);
// SAFETY: We only push ASCII bytes to `rv`, so we can't produce an invalid UTF-8 string
let rv = unsafe { rv_str.as_mut_vec() };

for (idx, token) in tokens[start as usize..end as usize].iter().enumerate() {
let index = start as usize + idx;
if token.get_dst_line() != prev_dst_line {
prev_dst_col = 0;
while token.get_dst_line() != prev_dst_line {
rv.push(';');
rv.push(b';');
prev_dst_line += 1;
}
} else if index > 0 {
if Some(token) == tokens.get(index - 1) {
continue;
}
rv.push(',');
rv.push(b',');
}

encode_vlq_diff(&mut rv, token.get_dst_col(), prev_dst_col);
encode_vlq_diff(rv, token.get_dst_col(), prev_dst_col);
prev_dst_col = token.get_dst_col();

if let Some(source_id) = token.get_source_id() {
encode_vlq_diff(&mut rv, source_id, prev_source_id);
encode_vlq_diff(rv, source_id, prev_source_id);
prev_source_id = source_id;
encode_vlq_diff(&mut rv, token.get_src_line(), prev_src_line);
encode_vlq_diff(rv, token.get_src_line(), prev_src_line);
prev_src_line = token.get_src_line();
encode_vlq_diff(&mut rv, token.get_src_col(), prev_src_col);
encode_vlq_diff(rv, token.get_src_col(), prev_src_col);
prev_src_col = token.get_src_col();
if let Some(name_id) = token.get_name_id() {
encode_vlq_diff(&mut rv, name_id, prev_name_id);
encode_vlq_diff(rv, name_id, prev_name_id);
prev_name_id = name_id;
}
}
}

rv
rv_str
}

#[inline]
fn encode_vlq_diff(out: &mut String, a: u32, b: u32) {
fn encode_vlq_diff(out: &mut Vec<u8>, a: u32, b: u32) {
encode_vlq(out, i64::from(a) - i64::from(b));
}

const B64_CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Align chars lookup table on 64 so occupies a single cache line
#[repr(align(64))]
struct Aligned64([u8; 64]);

static B64_CHARS: Aligned64 = Aligned64([
b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P',
b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd', b'e', b'f',
b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v',
b'w', b'x', b'y', b'z', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'+', b'/',
]);

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn encode_vlq(out: &mut String, num: i64) {
fn encode_vlq(out: &mut Vec<u8>, num: i64) {
let mut num = if num < 0 { ((-num) << 1) + 1 } else { num << 1 };

loop {
Expand All @@ -186,7 +197,7 @@ fn encode_vlq(out: &mut String, num: i64) {
if num > 0 {
digit |= 1 << 5;
}
out.push(B64_CHARS[digit as usize] as char);
out.push(B64_CHARS.0[digit as usize]);
if num == 0 {
break;
}
Expand Down
Loading