From ff43dff99dfa852d3fbea86f30ac6181123693b9 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Mon, 5 Aug 2024 01:34:59 +0000 Subject: [PATCH] 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). --- crates/oxc_sourcemap/src/encode.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/oxc_sourcemap/src/encode.rs b/crates/oxc_sourcemap/src/encode.rs index 6accd093087e2..a67a98ab749eb 100644 --- a/crates/oxc_sourcemap/src/encode.rs +++ b/crates/oxc_sourcemap/src/encode.rs @@ -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. @@ -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);