Skip to content

Commit e0c2e2a

Browse files
authored
perf(turbopack): Use rayon threadpool for minify() (vercel#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
1 parent 13af7b5 commit e0c2e2a

File tree

6 files changed

+19
-6
lines changed

6 files changed

+19
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-browser/src/ecmascript/content.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl EcmascriptBrowserChunkContent {
128128
let mut code = code.build();
129129

130130
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
131-
code = minify(&code, source_maps, mangle)?;
131+
code = minify(code, source_maps, mangle).await?;
132132
}
133133

134134
Ok(code.cell())

turbopack/crates/turbopack-browser/src/ecmascript/evaluate/chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl EcmascriptBrowserEvaluateChunk {
194194
let mut code = code.build();
195195

196196
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
197-
code = minify(&code, source_maps, mangle)?;
197+
code = minify(code, source_maps, mangle).await?;
198198
}
199199

200200
Ok(code.cell())

turbopack/crates/turbopack-ecmascript/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ turbopack-resolve = { workspace = true }
4444
turbopack-swc-utils = { workspace = true }
4545
url = { workspace = true }
4646
urlencoding = { workspace = true }
47+
rayon = { workspace = true }
4748

4849
swc_core = { workspace = true, features = [
4950
"ecma_ast",

turbopack/crates/turbopack-ecmascript/src/minify.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ use turbopack_core::{
3131
use crate::parse::generate_js_source_map;
3232

3333
#[instrument(level = Level::INFO, skip_all)]
34-
pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
34+
pub async fn minify(code: Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
35+
let (tx, rx) = tokio::sync::oneshot::channel();
36+
37+
rayon::spawn(move || {
38+
let result = minify_inner(code, source_maps, mangle);
39+
tx.send(result).unwrap();
40+
});
41+
42+
rx.await.unwrap()
43+
}
44+
45+
fn minify_inner(code: Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
3546
let source_maps = source_maps
3647
.then(|| code.generate_source_map_ref())
3748
.transpose()?;
@@ -139,8 +150,8 @@ pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Res
139150
src_map_buf,
140151
Some(original_map),
141152
// We do not inline source contents.
142-
// We provide a synthesized value to `cm.new_source_file` above, so it cannot be
143-
// the value user expect anyway.
153+
// We provide a synthesized value to `cm.new_source_file` above, so it cannot
154+
// be the value user expect anyway.
144155
false,
145156
)?),
146157
);

turbopack/crates/turbopack-nodejs/src/ecmascript/node/content.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl EcmascriptBuildNodeChunkContent {
7878
let mut code = code.build();
7979

8080
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
81-
code = minify(&code, source_maps, mangle)?;
81+
code = minify(code, source_maps, mangle).await?;
8282
}
8383

8484
Ok(code.cell())

0 commit comments

Comments
 (0)