diff --git a/src/config.rs b/src/config.rs index 6be92308..e7d5f5f6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -51,6 +51,10 @@ pub type HostProvider = FillProvider< RootProvider, >; +/// The default concurrency limit for the builder if the system call +/// fails and no user-specified value is set. +pub const DEFAULT_CONCURRENCY_LIMIT: usize = 8; + /// Configuration for a builder running a specific rollup on a specific host /// chain. #[derive(Debug, Clone, FromEnv)] @@ -152,7 +156,7 @@ pub struct BuilderConfig { var = "CONCURRENCY_LIMIT", desc = "The max number of simultaneous block simulations to run" )] - pub concurrency_limit: usize, + pub concurrency_limit: Option, /// The slot calculator for the builder. pub slot_calculator: SlotCalculator, @@ -276,4 +280,22 @@ impl BuilderConfig { pub const fn cfg_env(&self) -> SignetCfgEnv { SignetCfgEnv { chain_id: self.ru_chain_id } } + + /// Memoizes the concurrency limit for the current system. Uses [`std::thread::available_parallelism`] if no + /// value is set. If that for some reason fails, it returns the default concurrency limit. + pub fn concurrency_limit(&self) -> usize { + static ONCE: std::sync::OnceLock = std::sync::OnceLock::new(); + + if let Some(limit) = self.concurrency_limit { + if limit > 0 { + return limit; + } + } + + *ONCE.get_or_init(|| { + std::thread::available_parallelism() + .map(|p| p.get()) + .unwrap_or(DEFAULT_CONCURRENCY_LIMIT) + }) + } } diff --git a/src/tasks/block/sim.rs b/src/tasks/block/sim.rs index 20f18667..0c050654 100644 --- a/src/tasks/block/sim.rs +++ b/src/tasks/block/sim.rs @@ -100,6 +100,7 @@ impl Simulator { block_env: BlockEnv, ) -> eyre::Result { debug!(block_number = block_env.number, tx_count = sim_items.len(), "starting block build",); + let concurrency_limit = self.config.concurrency_limit(); let db = self.create_db().await.unwrap(); @@ -109,7 +110,7 @@ impl Simulator { self.config.cfg_env(), block_env, finish_by, - self.config.concurrency_limit, + concurrency_limit, sim_items, self.config.rollup_block_gas_limit, ); diff --git a/src/test_utils.rs b/src/test_utils.rs index 085a98ce..3d74ed4e 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -40,7 +40,7 @@ pub fn setup_test_config() -> Result { oauth_token_refresh_interval: 300, // 5 minutes }, builder_helper_address: Address::default(), - concurrency_limit: 1000, + concurrency_limit: None, // NB: Defaults to available parallelism slot_calculator: SlotCalculator::new( 1740681556, // pecorino start timestamp as sane default 0, 1,