Unverified Commit e0c2e2ae authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

perf(turbopack): Use rayon threadpool for `minify()` (#79261)

### What?

Run `minify()` on the `rayon` threadpool instead of the `tokio` threadpool.

### Why?

ES minification is a very CPU-intensive operation, and more importantly, it calls `rayon` internally anyway. So it's better to run it from the rayon thread pool from the start.

It seems like this PR makes the total build ~ 3% faster, at the cost of a small RSS increase.

x-ref: Numbers at
https://vercel.slack.com/archives/C06PPGZ0FD3/p1747349799503749?thread_ts=1747349784.352809&cid=C06PPGZ0FD3
parent 13af7b57
...@@ -10162,6 +10162,7 @@ dependencies = [ ...@@ -10162,6 +10162,7 @@ dependencies = [
"par-core", "par-core",
"parking_lot", "parking_lot",
"petgraph 0.6.3", "petgraph 0.6.3",
"rayon",
"regex", "regex",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"serde", "serde",
......
...@@ -128,7 +128,7 @@ impl EcmascriptBrowserChunkContent { ...@@ -128,7 +128,7 @@ impl EcmascriptBrowserChunkContent {
let mut code = code.build(); let mut code = code.build();
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() { if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
code = minify(&code, source_maps, mangle)?; code = minify(code, source_maps, mangle).await?;
} }
Ok(code.cell()) Ok(code.cell())
......
...@@ -194,7 +194,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -194,7 +194,7 @@ impl EcmascriptBrowserEvaluateChunk {
let mut code = code.build(); let mut code = code.build();
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() { if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
code = minify(&code, source_maps, mangle)?; code = minify(code, source_maps, mangle).await?;
} }
Ok(code.cell()) Ok(code.cell())
......
...@@ -44,6 +44,7 @@ turbopack-resolve = { workspace = true } ...@@ -44,6 +44,7 @@ turbopack-resolve = { workspace = true }
turbopack-swc-utils = { workspace = true } turbopack-swc-utils = { workspace = true }
url = { workspace = true } url = { workspace = true }
urlencoding = { workspace = true } urlencoding = { workspace = true }
rayon = { workspace = true }
swc_core = { workspace = true, features = [ swc_core = { workspace = true, features = [
"ecma_ast", "ecma_ast",
......
...@@ -31,7 +31,18 @@ use turbopack_core::{ ...@@ -31,7 +31,18 @@ use turbopack_core::{
use crate::parse::generate_js_source_map; use crate::parse::generate_js_source_map;
#[instrument(level = Level::INFO, skip_all)] #[instrument(level = Level::INFO, skip_all)]
pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> { pub async fn minify(code: Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
let (tx, rx) = tokio::sync::oneshot::channel();
rayon::spawn(move || {
let result = minify_inner(code, source_maps, mangle);
tx.send(result).unwrap();
});
rx.await.unwrap()
}
fn minify_inner(code: Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
let source_maps = source_maps let source_maps = source_maps
.then(|| code.generate_source_map_ref()) .then(|| code.generate_source_map_ref())
.transpose()?; .transpose()?;
...@@ -139,8 +150,8 @@ pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Res ...@@ -139,8 +150,8 @@ pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Res
src_map_buf, src_map_buf,
Some(original_map), Some(original_map),
// We do not inline source contents. // We do not inline source contents.
// We provide a synthesized value to `cm.new_source_file` above, so it cannot be // We provide a synthesized value to `cm.new_source_file` above, so it cannot
// the value user expect anyway. // be the value user expect anyway.
false, false,
)?), )?),
); );
......
...@@ -78,7 +78,7 @@ impl EcmascriptBuildNodeChunkContent { ...@@ -78,7 +78,7 @@ impl EcmascriptBuildNodeChunkContent {
let mut code = code.build(); let mut code = code.build();
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() { if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
code = minify(&code, source_maps, mangle)?; code = minify(code, source_maps, mangle).await?;
} }
Ok(code.cell()) Ok(code.cell())
......
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