mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-15 06:39:17 +00:00
perf(codegen): pre-allocate sourcemap builder vectors (#27230)
## Summary Pre-allocate the `SourcemapBuilder` `tokens`, `names`, and line-offset `lines` vectors from source length. This avoids repeated growth and memcpy work while preserving the existing `shrink_to_fit` behavior for final memory usage. ## Validation - `cargo fmt --all -- --check` passed. - `cargo test -p oxc_codegen`: all 12 sourcemap unit tests passed; the broader integration run had 172 passing tests and one unrelated snapshot mismatch because the local Node.js is v22.23.2 while the snapshot records v26.5.0. - CodSpeed CPU Simulation comparison: [base vs head](https://app.codspeed.io/oxc-project/oxc/runs/compare/6abd3aefcdcfa72af53b6cc4..6abd3b51836fe9d134c5094b) - `codegen[kitchen-sink.tsx]`: 25.7 ms → 22.1 ms (+16.5%) - `codegen[react.development.js]`: 1.9 ms → 1.7 ms (+13.16%) - `codegen[App.tsx]`: 10.9 ms → 9.7 ms (+11.69%) - `codegen[binder.ts]`: 4.0 ms → 3.6 ms (+11.44%) - 8 improvements, 12 unchanged, and no regressions reported. Wall-time validation was attempted locally but could not start because the sandbox cannot install the required `linux-tools` packages. The simulation run completed and uploaded successfully.
This commit is contained in:
1 parent
d0b9372b8b
commit
d37d1db073
1 file changed
+5
-3
@@ -66,12 +66,14 @@ pub struct SourcemapBuilder<'a> {
|
||||
impl<'a> SourcemapBuilder<'a> {
|
||||
pub fn new(path: &Path, source_text: &'a str) -> Self {
|
||||
let line_offset_tables = Self::generate_line_offset_tables(source_text);
|
||||
// Estimate mapping and name counts from source length to reduce reallocations.
|
||||
let source_len = source_text.len();
|
||||
Self {
|
||||
source_name: path.to_string_lossy().into_owned(),
|
||||
original_source: source_text,
|
||||
names: Vec::new(),
|
||||
names: Vec::with_capacity(source_len / 64),
|
||||
names_map: FxHashMap::default(),
|
||||
tokens: Vec::new(),
|
||||
tokens: Vec::with_capacity(source_len / 4),
|
||||
last_generated_update: 0,
|
||||
last_position: None,
|
||||
line_offset_tables,
|
||||
@@ -381,7 +383,7 @@ impl<'a> SourcemapBuilder<'a> {
|
||||
}
|
||||
|
||||
fn generate_line_offset_tables(content: &str) -> LineOffsetTables {
|
||||
let mut lines = vec![];
|
||||
let mut lines = Vec::with_capacity(content.len() / 24 + 1);
|
||||
let mut column_offsets = IndexVec::new();
|
||||
|
||||
// Used as a buffer to reduce memory reallocations.
|
||||
|
||||
Reference in new issue
Block a user