Skip to content

Commit

Permalink
perf(sourcemap): speed up VLQ encoding (#4633)
Browse files Browse the repository at this point in the history
Speed up source map VLQ encoding by removing a couple of operations from `serialize_mappings`'s hot loop.

On a local benchmark of just VLQ encoding, this change produces 5% performance increase (benchmarked on MacBook Pro M1).
  • Loading branch information
overlookmotel committed Aug 5, 2024
1 parent a330773 commit ff43dff
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions crates/oxc_sourcemap/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {

let mut prev_token = if start == 0 { None } else { Some(&tokens[start as usize - 1]) };

for (idx, token) in tokens[start as usize..end as usize].iter().enumerate() {
for token in &tokens[start as usize..end as usize] {
// Max length of a single VLQ encoding is 7 bytes. Max number of calls to `encode_vlq_diff` is 5.
// Also need 1 byte for each line number difference, or 1 byte if no line num difference.
// Reserve this amount of capacity in `rv` early, so can skip bounds checks in code below.
Expand All @@ -143,15 +143,14 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {
const MAX_TOTAL_VLQ_BYTES: usize = 5 * MAX_VLQ_BYTES;

let num_line_breaks = token.get_dst_line() - prev_dst_line;
let index = start as usize + idx;
if num_line_breaks != 0 {
rv.reserve(MAX_TOTAL_VLQ_BYTES + num_line_breaks as usize);
// SAFETY: We have reserved sufficient capacity for `num_line_breaks` bytes
unsafe { push_bytes_unchecked(&mut rv, b';', num_line_breaks) };
prev_dst_col = 0;
prev_dst_line += num_line_breaks;
} else if index > 0 {
if prev_token == Some(token) {
} else if let Some(prev_token) = prev_token {
if prev_token == token {
continue;
}
rv.reserve(MAX_TOTAL_VLQ_BYTES + 1);
Expand Down

0 comments on commit ff43dff

Please sign in to comment.