Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): Add an API to create a SourceFile without allocation #7029

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions crates/swc_common/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,17 @@ impl SourceMap {

/// Creates a new source_file.
/// This does not ensure that only one SourceFile exists per file name.
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
pub fn new_source_file(&self, filename: FileName, mut src: String) -> Lrc<SourceFile> {
remove_bom(&mut src);

self.new_source_file_from(filename, Lrc::new(src))
}

/// Creates a new source_file.
/// This does not ensure that only one SourceFile exists per file name.
///
/// `src` should not have UTF8 BOM
pub fn new_source_file_from(&self, filename: FileName, src: Lrc<String>) -> Lrc<SourceFile> {
// The path is used to determine the directory for loading submodules and
// include files, so it must be before remapping.
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
Expand All @@ -224,7 +234,7 @@ impl SourceMap {

let start_pos = self.next_start_pos(src.len());

let source_file = Lrc::new(SourceFile::new(
let source_file = Lrc::new(SourceFile::new_from(
filename,
was_remapped,
unmapped_path,
Expand Down
25 changes: 21 additions & 4 deletions crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,32 @@ impl SourceFile {
unmapped_path: FileName,
mut src: String,
start_pos: BytePos,
) -> SourceFile {
remove_bom(&mut src);

Self::new_from(
name,
name_was_remapped,
unmapped_path,
Lrc::new(src),
start_pos,
)
}

/// `src` should not have UTF8 BOM
pub fn new_from(
name: FileName,
name_was_remapped: bool,
unmapped_path: FileName,
src: Lrc<String>,
start_pos: BytePos,
) -> SourceFile {
debug_assert_ne!(
start_pos,
BytePos::DUMMY,
"BytePos::DUMMY is reserved and `SourceFile` should not use it"
);

remove_bom(&mut src);

let src_hash = {
let mut hasher: StableHasher = StableHasher::new();
hasher.write(src.as_bytes());
Expand All @@ -898,7 +915,7 @@ impl SourceFile {
name_was_remapped,
unmapped_path: Some(unmapped_path),
crate_of_origin: 0,
src: Lrc::new(src),
src,
src_hash,
start_pos,
end_pos: Pos::from_usize(end_pos),
Expand Down Expand Up @@ -988,7 +1005,7 @@ impl SourceFile {
}

/// Remove utf-8 BOM if any.
fn remove_bom(src: &mut String) {
pub(super) fn remove_bom(src: &mut String) {
if src.starts_with('\u{feff}') {
src.drain(..3);
}
Expand Down