Skip to content

Commit

Permalink
perf(sourcemap): keep local copy of previous token in VLQ encode (#4596)
Browse files Browse the repository at this point in the history
In source map VLQ encoding, keep local copy of previous `Token`, rather than looking up up from `tokens`.

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

let mut rv = String::with_capacity(capacity);

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() {
// 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.
Expand All @@ -149,7 +151,7 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {
prev_dst_col = 0;
prev_dst_line += num_line_breaks;
} else if index > 0 {
if Some(token) == tokens.get(index - 1) {
if prev_token == Some(token) {
continue;
}
rv.reserve(MAX_TOTAL_VLQ_BYTES + 1);
Expand All @@ -176,6 +178,8 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {
}
}
}

prev_token = Some(token);
}

rv
Expand Down

0 comments on commit c7f1d48

Please sign in to comment.