Skip to content

Commit

Permalink
scale down process pool when module graph is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 11, 2025
1 parent f16aa7b commit 28a6df0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,15 @@ impl Project {
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
41 changes: 39 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,9 @@ enum AcquiredPermits {
},
}

static ACTIVE_POOLS: Lazy<Mutex<Vec<Arc<Mutex<BinaryHeap<NodeJsPoolProcess>>>>>> =

Check failure on line 709 in turbopack/crates/turbopack-node/src/pool.rs

View workflow job for this annotation

GitHub Actions / rust check / build

very complex type used. Consider factoring parts into `type` definitions
Lazy::new(|| Default::default());

Check failure on line 710 in turbopack/crates/turbopack-node/src/pool.rs

View workflow job for this annotation

GitHub Actions / rust check / build

redundant closure

/// A pool of Node.js workers operating on [entrypoint] with specific [cwd] and
/// [env].
///
Expand Down Expand Up @@ -793,7 +797,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();
{
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 +860,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 +1002,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

0 comments on commit 28a6df0

Please # to comment.