Skip to content

Commit 12f143d

Browse files
committed
perf(codegen): further reduce memory allocations in generate_line_offset_tables
1 parent ab685bd commit 12f143d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

crates/oxc_codegen/src/sourcemap_builder.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ impl<'a> SourcemapBuilder<'a> {
312312
let mut lines = vec![];
313313
let mut column_offsets = IndexVec::new();
314314

315+
// Used as a buffer to reduce memory reallocations
316+
let mut columns = vec![];
317+
315318
// Process content line-by-line.
316319
// For each line, start by assuming line will be entirely ASCII, and read byte-by-byte.
317320
// If line is all ASCII, UTF-8 columns and UTF-16 columns are the same,
@@ -325,7 +328,6 @@ impl<'a> SourcemapBuilder<'a> {
325328
column_offsets_id: None,
326329
});
327330

328-
let mut columns = vec![]; // Used as a buffer to reduce memory reallocations.
329331
let remaining = &content.as_bytes()[line_byte_offset as usize..];
330332
for (byte_offset_from_line_start, b) in remaining.iter().enumerate() {
331333
#[expect(clippy::cast_possible_truncation)]
@@ -351,8 +353,6 @@ impl<'a> SourcemapBuilder<'a> {
351353
line.column_offsets_id =
352354
Some(ColumnOffsetsId::from_usize(column_offsets.len()));
353355

354-
columns.clear();
355-
356356
// Loop through rest of line char-by-char.
357357
// `chunk_byte_offset` in this loop is byte offset from start of this 1st
358358
// Unicode char.
@@ -396,8 +396,9 @@ impl<'a> SourcemapBuilder<'a> {
396396
// Record column offsets
397397
column_offsets.push(ColumnOffsets {
398398
byte_offset_to_first: byte_offset_from_line_start,
399-
columns: columns.into_iter().collect(),
399+
columns: columns.clone().into_boxed_slice(),
400400
});
401+
columns.clear();
401402

402403
// Revert back to outer loop for next line
403404
continue 'lines;
@@ -410,7 +411,7 @@ impl<'a> SourcemapBuilder<'a> {
410411
// Record column offsets
411412
column_offsets.push(ColumnOffsets {
412413
byte_offset_to_first: byte_offset_from_line_start,
413-
columns: columns.into_iter().collect(),
414+
columns: columns.into_boxed_slice(),
414415
});
415416

416417
break 'lines;

0 commit comments

Comments
 (0)