Skip to content

Commit

Permalink
perf: move swc transform into another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Oct 1, 2024
1 parent 33a5468 commit b070fab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
120 changes: 60 additions & 60 deletions crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use compiler::{IntoJsAst, SwcCompiler};
use options::SwcCompilerOptionsWithAdditional;
pub use options::SwcLoaderJsOptions;
use rspack_core::{rspack_sources::SourceMap, Mode, RunnerContext};
use rspack_error::miette::{Error, IntoDiagnostic};
use rspack_error::{error, AnyhowError, Diagnostic, Result};
use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext};
use rspack_plugin_javascript::ast::{self, SourceMapConfig};
Expand Down Expand Up @@ -43,7 +44,7 @@ impl SwcLoader {
self
}

fn loader_impl(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
async fn loader_impl(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
let resource_path = loader_context
.resource_path()
.map(|p| p.to_path_buf())
Expand Down Expand Up @@ -87,56 +88,67 @@ impl SwcLoader {
};

let source = content.into_string_lossy();
let c = SwcCompiler::new(
resource_path.into_std_path_buf(),
source.clone(),
swc_options,
)
.map_err(AnyhowError::from)?;

let built = c
.parse(None, |_| {
transformer::transform(&self.options_with_additional.rspack_experiments)
})
let options_with_additional = self.options_with_additional.clone();
let (code, map) = tokio::task::spawn_blocking(move || {
let c = SwcCompiler::new(
resource_path.into_std_path_buf(),
source.clone(),
swc_options,
)
.map_err(AnyhowError::from)?;

let input_source_map = c
.input_source_map(&built.input_source_map)
.map_err(|e| error!(e.to_string()))?;
let mut codegen_options = ast::CodegenOptions {
target: Some(built.target),
minify: Some(built.minify),
input_source_map: input_source_map.as_ref(),
ascii_only: built
.output
.charset
.as_ref()
.map(|v| matches!(v, OutputCharset::Ascii)),
source_map_config: SourceMapConfig {
enable: source_map_kind.source_map(),
inline_sources_content: source_map_kind.source_map(),
emit_columns: !source_map_kind.cheap(),
names: Default::default(),
},
inline_script: Some(false),
keep_comments: Some(true),
};

let program = tokio::task::block_in_place(|| c.transform(built).map_err(AnyhowError::from))?;
if source_map_kind.enabled() {
let mut v = IdentCollector {
names: Default::default(),
let built = c
.parse(None, |_| {
transformer::transform(&options_with_additional.rspack_experiments)
})
.map_err(AnyhowError::from)?;

let input_source_map = c
.input_source_map(&built.input_source_map)
.map_err(|e| error!(e.to_string()))?;
let mut codegen_options = ast::CodegenOptions {
target: Some(built.target),
minify: Some(built.minify),
input_source_map: input_source_map.as_ref(),
ascii_only: built
.output
.charset
.as_ref()
.map(|v| matches!(v, OutputCharset::Ascii)),
source_map_config: SourceMapConfig {
enable: source_map_kind.source_map(),
inline_sources_content: source_map_kind.source_map(),
emit_columns: !source_map_kind.cheap(),
names: Default::default(),
},
inline_script: Some(false),
keep_comments: Some(true),
};
program.visit_with(&mut v);
codegen_options.source_map_config.names = v.names;
}
let ast = c.into_js_ast(program);
let TransformOutput { code, map } = ast::stringify(&ast, codegen_options)?;
let program = c.transform(built).map_err(AnyhowError::from)?;
if source_map_kind.enabled() {
let mut v = IdentCollector {
names: Default::default(),
};
program.visit_with(&mut v);
codegen_options.source_map_config.names = v.names;
}
let ast = c.into_js_ast(program);
let TransformOutput { code, map } = ast::stringify(&ast, codegen_options)?;
let map = map
.map(|m| SourceMap::from_json(&m))
.transpose()
.map_err(|e| error!(e.to_string()))?;
Ok::<
(
std::string::String,
std::option::Option<rspack_core::rspack_sources::SourceMap>,
),
Error,
>((code, map))
})
.await
.into_diagnostic()??;

let map = map
.map(|m| SourceMap::from_json(&m))
.transpose()
.map_err(|e| error!(e.to_string()))?;
loader_context.finish_with((code, map));

Ok(())
Expand All @@ -148,19 +160,7 @@ pub const SWC_LOADER_IDENTIFIER: &str = "builtin:swc-loader";
#[async_trait::async_trait]
impl Loader<RunnerContext> for SwcLoader {
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
#[allow(unused_mut)]
let mut inner = || self.loader_impl(loader_context);
#[cfg(debug_assertions)]
{
// Adjust stack to avoid stack overflow.
stacker::maybe_grow(
2 * 1024 * 1024, /* 2mb */
4 * 1024 * 1024, /* 4mb */
inner,
)
}
#[cfg(not(debug_assertions))]
inner()
self.loader_impl(loader_context).await
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_loader_swc/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct RawRspackExperiments {
pub import: Option<Vec<RawImportOptions>>,
}

#[derive(Default, Debug)]
#[derive(Default, Debug, Clone)]
pub(crate) struct RspackExperiments {
pub(crate) import: Option<Vec<ImportOptions>>,
}
Expand Down Expand Up @@ -74,7 +74,7 @@ pub struct SwcLoaderJsOptions {
pub rspack_experiments: Option<RawRspackExperiments>,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct SwcCompilerOptionsWithAdditional {
pub(crate) swc_options: Options,
pub(crate) rspack_experiments: RspackExperiments,
Expand Down

0 comments on commit b070fab

Please sign in to comment.