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

[Turbopack] scale down process pool when module graph is finished #75896

Merged
merged 3 commits into from
Feb 12, 2025
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
9 changes: 9 additions & 0 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
self.project().hmr_identifiers()
}

/// Gets a source map for a particular `file_path`. If `dev` mode is

Check warning on line 489 in crates/next-api/src/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unresolved link to `OptionSourceMap::none`
/// disabled, this will always return [`OptionSourceMap::none`].
#[turbo_tasks::function]
pub fn get_source_map(
Expand Down Expand Up @@ -900,6 +900,15 @@
let module_graphs_op = whole_app_module_graph_operation(self);
let module_graphs_vc = module_graphs_op.connect().resolve().await?;
let _ = module_graphs_op.take_issues_with_path().await?;

// At this point all modules have been computed and we can get rid of the node.js
// process pools
if self.await?.watch.enable {
turbopack_node::evaluate::scale_down();
} else {
turbopack_node::evaluate::scale_zero();
}

Ok(module_graphs_vc)
}
.instrument(tracing::info_span!("module graph for app"))
Expand Down
8 changes: 8 additions & 0 deletions turbopack/crates/turbopack-node/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,14 @@ impl EvaluateContext for BasicEvaluateContext {
}
}

pub fn scale_zero() {
NodeJsPool::scale_zero();
}

pub fn scale_down() {
NodeJsPool::scale_down();
}

async fn print_error(
error: StructuredError,
pool: &NodeJsPool,
Expand Down
44 changes: 42 additions & 2 deletions turbopack/crates/turbopack-node/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{

use anyhow::{bail, Context, Result};
use futures::join;
use once_cell::sync::Lazy;
use owo_colors::{OwoColorize, Style};
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -705,6 +706,12 @@ enum AcquiredPermits {
},
}

type IdleProcessesList = Arc<Mutex<BinaryHeap<NodeJsPoolProcess>>>;

/// All non-empty `IdleProcessesList`s of the whole application.
/// This is used to scale down processes globally.
static ACTIVE_POOLS: Lazy<Mutex<Vec<IdleProcessesList>>> = Lazy::new(Default::default);

/// A pool of Node.js workers operating on [entrypoint] with specific [cwd] and
/// [env].
///
Expand Down Expand Up @@ -793,7 +800,14 @@ impl NodeJsPool {
let idle_process_permit = idle_process_permit.context("acquiring idle process permit")?;
let process = {
let mut processes = self.processes.lock();
processes.pop().unwrap()
let process = processes.pop().unwrap();
if processes.is_empty() {
let mut pools = ACTIVE_POOLS.lock();
if let Some(idx) = pools.iter().position(|p| Arc::ptr_eq(p, &self.processes)) {
pools.swap_remove(idx);
}
}
process
};
idle_process_permit.forget();
Ok((process, AcquiredPermits::Idle { concurrency_permit }))
Expand Down Expand Up @@ -849,6 +863,26 @@ impl NodeJsPool {
allow_process_reuse: true,
})
}

pub fn scale_down() {
let pools = ACTIVE_POOLS.lock().clone();
for pool in pools {
let mut pool = pool.lock();
let best = pool.pop().unwrap();
pool.clear();
pool.push(best);
pool.shrink_to_fit();
}
}

pub fn scale_zero() {
let pools = take(&mut *ACTIVE_POOLS.lock());
for pool in pools {
let mut pool = pool.lock();
pool.clear();
pool.shrink_to_fit();
}
}
}

pub struct NodeJsOperation {
Expand Down Expand Up @@ -971,7 +1005,13 @@ impl Drop for NodeJsOperation {
}
if self.allow_process_reuse {
process.cpu_time_invested += elapsed;
self.processes.lock().push(process);
{
let mut processes = self.processes.lock();
if processes.is_empty() {
ACTIVE_POOLS.lock().push(self.processes.clone());
}
processes.push(process);
}
self.idle_process_semaphore.add_permits(1);
}
}
Expand Down
Loading