From b35bf301dd182e7a733f6932113bc59c2ca91788 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Thu, 11 Sep 2025 05:28:51 +0000 Subject: [PATCH] perf(codegen): optimize sourcemap builder to reduce allocations (#13670) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary This PR optimizes the sourcemap builder to reduce memory allocations and improve performance. Profiling showed that `update_generated_line_and_column` and `add_source_mapping` were performance bottlenecks. ### Changes Made **Optimization 4: Optimize Line Offset Table Generation** - Pre-allocate `columns` vector with capacity 256 to avoid frequent reallocations - Replace `columns.clone().into_boxed_slice()` with `std::mem::take(&mut columns).into_boxed_slice()` to avoid unnecessary cloning - Reserve capacity after taking the vector to maintain performance for subsequent lines ### Performance Impact These changes reduce memory allocations when generating sourcemaps, especially for files with Unicode characters. The `clone()` operation was creating unnecessary copies of potentially large vectors on every Unicode line, which is now eliminated. ### Future Optimizations Additional optimizations from the analysis that could be implemented in follow-up PRs: 1. Use SIMD-accelerated line break detection with `memchr` 2. Optimize UTF-16 column calculation to avoid iterator allocation 3. Add fast path for sequential token processing 5. Inline hot functions with `#[inline(always)]` ## Test Plan - [x] All existing tests pass - [x] No functional changes, only performance optimizations - [x] Verified that sourcemap generation still works correctly 🤖 Generated with [Claude Code](https://claude.ai/code) --- crates/oxc_codegen/src/sourcemap_builder.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/oxc_codegen/src/sourcemap_builder.rs b/crates/oxc_codegen/src/sourcemap_builder.rs index 671671b630d8d..5df20385ce336 100644 --- a/crates/oxc_codegen/src/sourcemap_builder.rs +++ b/crates/oxc_codegen/src/sourcemap_builder.rs @@ -308,7 +308,8 @@ impl<'a> SourcemapBuilder<'a> { let mut column_offsets = IndexVec::new(); // Used as a buffer to reduce memory reallocations - let mut columns = vec![]; + // Pre-allocate with reasonable capacity to avoid frequent reallocations + let mut columns = Vec::with_capacity(256); // Process content line-by-line. // For each line, start by assuming line will be entirely ASCII, and read byte-by-byte. @@ -389,11 +390,13 @@ impl<'a> SourcemapBuilder<'a> { line_byte_offset += chunk_byte_offset; // Record column offsets + // Use mem::take to avoid clone - moves columns out and replaces with empty Vec column_offsets.push(ColumnOffsets { byte_offset_to_first: byte_offset_from_line_start, - columns: columns.clone().into_boxed_slice(), + columns: std::mem::take(&mut columns).into_boxed_slice(), }); - columns.clear(); + // Reserve capacity for next line to avoid reallocation + columns.reserve(256); // Revert back to outer loop for next line continue 'lines;