Skip to content

Commit 8917a5f

Browse files
authored
refactor(codegen): remove clone of source text in sourcemap builder (#12393)
1 parent 46e33d5 commit 8917a5f

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

crates/oxc_codegen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub struct Codegen<'a> {
115115
// Builders
116116
comments: CommentsMap,
117117

118-
sourcemap_builder: Option<SourcemapBuilder>,
118+
sourcemap_builder: Option<SourcemapBuilder<'a>>,
119119
}
120120

121121
impl Default for Codegen<'_> {

crates/oxc_codegen/src/sourcemap_builder.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::Path, sync::Arc};
1+
use std::path::Path;
22

33
use nonmax::NonMaxU32;
44
use oxc_index::{Idx, IndexVec};
@@ -65,9 +65,9 @@ pub struct ColumnOffsets {
6565
}
6666

6767
#[expect(clippy::struct_field_names)]
68-
pub struct SourcemapBuilder {
68+
pub struct SourcemapBuilder<'a> {
6969
source_id: u32,
70-
original_source: Arc<str>,
70+
original_source: &'a str,
7171
last_generated_update: usize,
7272
last_position: Option<u32>,
7373
line_offset_tables: LineOffsetTables,
@@ -80,15 +80,15 @@ pub struct SourcemapBuilder {
8080
last_line_lookup: u32,
8181
}
8282

83-
impl SourcemapBuilder {
84-
pub fn new(path: &Path, source_text: &str) -> Self {
83+
impl<'a> SourcemapBuilder<'a> {
84+
pub fn new(path: &Path, source_text: &'a str) -> Self {
8585
let mut sourcemap_builder = oxc_sourcemap::SourceMapBuilder::default();
8686
let line_offset_tables = Self::generate_line_offset_tables(source_text);
8787
let source_id =
8888
sourcemap_builder.set_source_and_content(path.to_string_lossy().as_ref(), source_text);
8989
Self {
9090
source_id,
91-
original_source: Arc::from(source_text),
91+
original_source: source_text,
9292
last_generated_update: 0,
9393
last_position: None,
9494
line_offset_tables,
@@ -114,18 +114,17 @@ impl SourcemapBuilder {
114114
let original_name = self.original_source.get(span.start as usize..span.end as usize);
115115
// The token name should be original name.
116116
// If it hasn't change, name should be `None` to reduce `SourceMap` size.
117-
let token_name =
118-
if original_name == Some(name) { None } else { original_name.map(Into::into) };
117+
let token_name = if original_name == Some(name) { None } else { original_name };
119118
self.add_source_mapping(output, span.start, token_name);
120119
}
121120

122-
pub fn add_source_mapping(&mut self, output: &[u8], position: u32, name: Option<Arc<str>>) {
121+
pub fn add_source_mapping(&mut self, output: &[u8], position: u32, name: Option<&str>) {
123122
if matches!(self.last_position, Some(last_position) if last_position == position) {
124123
return;
125124
}
126125
let (original_line, original_column) = self.search_original_line_and_column(position);
127126
self.update_generated_line_and_column(output);
128-
let name_id = name.map(|s| self.sourcemap_builder.add_name(&s));
127+
let name_id = name.map(|s| self.sourcemap_builder.add_name(s));
129128
self.sourcemap_builder.add_token(
130129
self.generated_line,
131130
self.generated_column,

0 commit comments

Comments
 (0)