mirror of
https://github.com/danbulant/oxc
synced 2026-05-20 20:58:48 +00:00
perf(sourcemap): speed up VLQ encoding (#4633)
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).
This commit is contained in:
parent
a3307734c5
commit
ff43dff99d
1 changed files with 3 additions and 4 deletions
|
|
@ -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]) };
|
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.
|
// 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.
|
// 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.
|
// Reserve this amount of capacity in `rv` early, so can skip bounds checks in code below.
|
||||||
|
|
@ -143,15 +143,14 @@ fn serialize_mappings(tokens: &[Token], token_chunk: &TokenChunk) -> String {
|
||||||
const MAX_TOTAL_VLQ_BYTES: usize = 5 * MAX_VLQ_BYTES;
|
const MAX_TOTAL_VLQ_BYTES: usize = 5 * MAX_VLQ_BYTES;
|
||||||
|
|
||||||
let num_line_breaks = token.get_dst_line() - prev_dst_line;
|
let num_line_breaks = token.get_dst_line() - prev_dst_line;
|
||||||
let index = start as usize + idx;
|
|
||||||
if num_line_breaks != 0 {
|
if num_line_breaks != 0 {
|
||||||
rv.reserve(MAX_TOTAL_VLQ_BYTES + num_line_breaks as usize);
|
rv.reserve(MAX_TOTAL_VLQ_BYTES + num_line_breaks as usize);
|
||||||
// SAFETY: We have reserved sufficient capacity for `num_line_breaks` bytes
|
// SAFETY: We have reserved sufficient capacity for `num_line_breaks` bytes
|
||||||
unsafe { push_bytes_unchecked(&mut rv, b';', num_line_breaks) };
|
unsafe { push_bytes_unchecked(&mut rv, b';', num_line_breaks) };
|
||||||
prev_dst_col = 0;
|
prev_dst_col = 0;
|
||||||
prev_dst_line += num_line_breaks;
|
prev_dst_line += num_line_breaks;
|
||||||
} else if index > 0 {
|
} else if let Some(prev_token) = prev_token {
|
||||||
if prev_token == Some(token) {
|
if prev_token == token {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
rv.reserve(MAX_TOTAL_VLQ_BYTES + 1);
|
rv.reserve(MAX_TOTAL_VLQ_BYTES + 1);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue