Unverified Commit d910a6cf authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] try to avoid cloning AST during code generation, pass final_read_hint (#75853)

### What?

* try to avoid cloning AST during code generation, pass final_read_hint
* fix tree shake ident on code generation
parent 3127bdfd
...@@ -34,11 +34,16 @@ pub mod utils; ...@@ -34,11 +34,16 @@ pub mod utils;
pub mod webpack; pub mod webpack;
pub mod worker_chunk; pub mod worker_chunk;
use std::fmt::{Display, Formatter}; use std::{
fmt::{Display, Formatter},
mem::take,
sync::Arc,
};
use anyhow::Result; use anyhow::Result;
use chunk::EcmascriptChunkItem; use chunk::EcmascriptChunkItem;
use code_gen::{CodeGenerateable, CodeGeneration, CodeGenerationHoistedStmt}; use code_gen::{CodeGenerateable, CodeGeneration, CodeGenerationHoistedStmt};
use either::Either;
use parse::{parse, ParseResult}; use parse::{parse, ParseResult};
use path_visitor::ApplyVisitors; use path_visitor::ApplyVisitors;
use references::esm::UrlRewriteBehavior; use references::esm::UrlRewriteBehavior;
...@@ -46,7 +51,7 @@ pub use references::{AnalyzeEcmascriptModuleResult, TURBOPACK_HELPER}; ...@@ -46,7 +51,7 @@ pub use references::{AnalyzeEcmascriptModuleResult, TURBOPACK_HELPER};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub use static_code::StaticEcmascriptCode; pub use static_code::StaticEcmascriptCode;
use swc_core::{ use swc_core::{
common::{Globals, Mark, GLOBALS}, common::{comments::Comments, util::take::Take, Globals, Mark, GLOBALS},
ecma::{ ecma::{
ast::{self, ModuleItem, Program, Script}, ast::{self, ModuleItem, Program, Script},
codegen::{text_writer::JsWriter, Emitter}, codegen::{text_writer::JsWriter, Emitter},
...@@ -835,18 +840,51 @@ async fn gen_content_with_code_gens( ...@@ -835,18 +840,51 @@ async fn gen_content_with_code_gens(
generate_source_map: Vc<bool>, generate_source_map: Vc<bool>,
original_source_map: ResolvedVc<OptionStringifiedSourceMap>, original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
) -> Result<Vc<EcmascriptModuleContent>> { ) -> Result<Vc<EcmascriptModuleContent>> {
let parsed = parsed.await?; let parsed = parsed.final_read_hint().await?;
match &*parsed { match &*parsed {
ParseResult::Ok { ParseResult::Ok { .. } => {
program, // We need a mutable version of the AST. We try to avoid cloning it by unwrapping the
source_map, // ReadRef.
globals, let mut parsed = ReadRef::try_unwrap(parsed);
eval_context, let (mut program, source_map, globals, eval_context, comments) = match &mut parsed {
comments, Ok(ParseResult::Ok {
.. program,
} => { source_map,
let mut program = program.clone(); globals,
eval_context,
comments,
}) => (
program.take(),
&*source_map,
&*globals,
&*eval_context,
match Arc::try_unwrap(take(comments)) {
Ok(comments) => Either::Left(comments),
Err(comments) => Either::Right(comments),
},
),
Err(parsed) => {
let ParseResult::Ok {
program,
source_map,
globals,
eval_context,
comments,
} = &**parsed
else {
unreachable!();
};
(
program.clone(),
source_map,
globals,
eval_context,
Either::Right(comments.clone()),
)
}
_ => unreachable!(),
};
process_content_with_code_gens( process_content_with_code_gens(
&mut program, &mut program,
...@@ -864,7 +902,15 @@ async fn gen_content_with_code_gens( ...@@ -864,7 +902,15 @@ async fn gen_content_with_code_gens(
let generate_source_map = *generate_source_map.await?; let generate_source_map = *generate_source_map.await?;
{ {
let comments = comments.consumable(); let comments = match comments {
Either::Left(comments) => Either::Left(comments.into_consumable()),
Either::Right(ref comments) => Either::Right(comments.consumable()),
};
let comments: &dyn Comments = match &comments {
Either::Left(comments) => comments,
Either::Right(comments) => comments,
};
let mut emitter = Emitter { let mut emitter = Emitter {
cfg: swc_core::ecma::codegen::Config::default(), cfg: swc_core::ecma::codegen::Config::default(),
cm: source_map.clone(), cm: source_map.clone(),
...@@ -876,6 +922,7 @@ async fn gen_content_with_code_gens( ...@@ -876,6 +922,7 @@ async fn gen_content_with_code_gens(
generate_source_map.then_some(&mut mappings), generate_source_map.then_some(&mut mappings),
), ),
}; };
emitter.emit_program(&program)?; emitter.emit_program(&program)?;
} }
......
use std::{cell::RefCell, mem::take}; use std::{borrow::Cow, cell::RefCell, mem::take};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use swc_core::{ use swc_core::{
...@@ -12,6 +12,7 @@ use swc_core::{ ...@@ -12,6 +12,7 @@ use swc_core::{
/// Immutable version of [SwcComments] which doesn't allow mutation. The `take` /// Immutable version of [SwcComments] which doesn't allow mutation. The `take`
/// variants are still implemented, but do not mutate the content. They are used /// variants are still implemented, but do not mutate the content. They are used
/// by the SWC Emitter. /// by the SWC Emitter.
#[derive(Default)]
pub struct ImmutableComments { pub struct ImmutableComments {
pub leading: FxHashMap<BytePos, Vec<Comment>>, pub leading: FxHashMap<BytePos, Vec<Comment>>,
pub trailing: FxHashMap<BytePos, Vec<Comment>>, pub trailing: FxHashMap<BytePos, Vec<Comment>>,
...@@ -39,8 +40,12 @@ impl ImmutableComments { ...@@ -39,8 +40,12 @@ impl ImmutableComments {
} }
} }
pub fn into_consumable(self) -> CowComments<'static> {
CowComments::owned(self)
}
pub fn consumable(&self) -> CowComments<'_> { pub fn consumable(&self) -> CowComments<'_> {
CowComments::new(self) CowComments::borrowed(self)
} }
} }
...@@ -186,25 +191,44 @@ impl Comments for ImmutableComments { ...@@ -186,25 +191,44 @@ impl Comments for ImmutableComments {
} }
pub struct CowComments<'a> { pub struct CowComments<'a> {
leading: RefCell<FxHashMap<BytePos, &'a Vec<Comment>>>, leading: RefCell<FxHashMap<BytePos, Cow<'a, Vec<Comment>>>>,
trailing: RefCell<FxHashMap<BytePos, &'a Vec<Comment>>>, trailing: RefCell<FxHashMap<BytePos, Cow<'a, Vec<Comment>>>>,
} }
impl<'a> CowComments<'a> { impl<'a> CowComments<'a> {
fn new(comments: &'a ImmutableComments) -> Self { fn borrowed(comments: &'a ImmutableComments) -> Self {
Self { Self {
leading: RefCell::new( leading: RefCell::new(
comments comments
.leading .leading
.iter() .iter()
.map(|(&key, value)| (key, value)) .map(|(&key, value)| (key, Cow::Borrowed(value)))
.collect(), .collect(),
), ),
trailing: RefCell::new( trailing: RefCell::new(
comments comments
.trailing .trailing
.iter() .iter()
.map(|(&key, value)| (key, value)) .map(|(&key, value)| (key, Cow::Borrowed(value)))
.collect(),
),
}
}
fn owned(comments: ImmutableComments) -> Self {
Self {
leading: RefCell::new(
comments
.leading
.into_iter()
.map(|(key, value)| (key, Cow::Owned(value)))
.collect(),
),
trailing: RefCell::new(
comments
.trailing
.into_iter()
.map(|(key, value)| (key, Cow::Owned(value)))
.collect(), .collect(),
), ),
} }
...@@ -240,14 +264,17 @@ impl Comments for CowComments<'_> { ...@@ -240,14 +264,17 @@ impl Comments for CowComments<'_> {
&self, &self,
pos: swc_core::common::BytePos, pos: swc_core::common::BytePos,
) -> Option<Vec<swc_core::common::comments::Comment>> { ) -> Option<Vec<swc_core::common::comments::Comment>> {
self.leading.borrow_mut().remove(&pos).map(|v| v.to_owned()) self.leading
.borrow_mut()
.remove(&pos)
.map(|v| v.into_owned())
} }
fn get_leading( fn get_leading(
&self, &self,
pos: swc_core::common::BytePos, pos: swc_core::common::BytePos,
) -> Option<Vec<swc_core::common::comments::Comment>> { ) -> Option<Vec<swc_core::common::comments::Comment>> {
self.leading.borrow().get(&pos).map(|&v| v.to_owned()) self.leading.borrow().get(&pos).map(|v| (**v).clone())
} }
fn add_trailing( fn add_trailing(
...@@ -281,14 +308,14 @@ impl Comments for CowComments<'_> { ...@@ -281,14 +308,14 @@ impl Comments for CowComments<'_> {
self.trailing self.trailing
.borrow_mut() .borrow_mut()
.remove(&pos) .remove(&pos)
.map(|v| v.to_owned()) .map(|v| v.into_owned())
} }
fn get_trailing( fn get_trailing(
&self, &self,
pos: swc_core::common::BytePos, pos: swc_core::common::BytePos,
) -> Option<Vec<swc_core::common::comments::Comment>> { ) -> Option<Vec<swc_core::common::comments::Comment>> {
self.trailing.borrow().get(&pos).map(|&v| v.to_owned()) self.trailing.borrow().get(&pos).map(|v| (**v).clone())
} }
fn add_pure_comment(&self, _pos: swc_core::common::BytePos) { fn add_pure_comment(&self, _pos: swc_core::common::BytePos) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment