Skip to content

Commit ae37a40

Browse files
authored
env: use available concurrency to set concurrency defaults (#104)
1 parent 59d1fc9 commit ae37a40

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/config.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ pub type HostProvider = FillProvider<
5151
RootProvider,
5252
>;
5353

54+
/// The default concurrency limit for the builder if the system call
55+
/// fails and no user-specified value is set.
56+
pub const DEFAULT_CONCURRENCY_LIMIT: usize = 8;
57+
5458
/// Configuration for a builder running a specific rollup on a specific host
5559
/// chain.
5660
#[derive(Debug, Clone, FromEnv)]
@@ -152,7 +156,7 @@ pub struct BuilderConfig {
152156
var = "CONCURRENCY_LIMIT",
153157
desc = "The max number of simultaneous block simulations to run"
154158
)]
155-
pub concurrency_limit: usize,
159+
pub concurrency_limit: Option<usize>,
156160

157161
/// The slot calculator for the builder.
158162
pub slot_calculator: SlotCalculator,
@@ -276,4 +280,22 @@ impl BuilderConfig {
276280
pub const fn cfg_env(&self) -> SignetCfgEnv {
277281
SignetCfgEnv { chain_id: self.ru_chain_id }
278282
}
283+
284+
/// Memoizes the concurrency limit for the current system. Uses [`std::thread::available_parallelism`] if no
285+
/// value is set. If that for some reason fails, it returns the default concurrency limit.
286+
pub fn concurrency_limit(&self) -> usize {
287+
static ONCE: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
288+
289+
if let Some(limit) = self.concurrency_limit {
290+
if limit > 0 {
291+
return limit;
292+
}
293+
}
294+
295+
*ONCE.get_or_init(|| {
296+
std::thread::available_parallelism()
297+
.map(|p| p.get())
298+
.unwrap_or(DEFAULT_CONCURRENCY_LIMIT)
299+
})
300+
}
279301
}

src/tasks/block/sim.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl Simulator {
100100
block_env: BlockEnv,
101101
) -> eyre::Result<BuiltBlock> {
102102
debug!(block_number = block_env.number, tx_count = sim_items.len(), "starting block build",);
103+
let concurrency_limit = self.config.concurrency_limit();
103104

104105
let db = self.create_db().await.unwrap();
105106

@@ -109,7 +110,7 @@ impl Simulator {
109110
self.config.cfg_env(),
110111
block_env,
111112
finish_by,
112-
self.config.concurrency_limit,
113+
concurrency_limit,
113114
sim_items,
114115
self.config.rollup_block_gas_limit,
115116
);

src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
4040
oauth_token_refresh_interval: 300, // 5 minutes
4141
},
4242
builder_helper_address: Address::default(),
43-
concurrency_limit: 1000,
43+
concurrency_limit: None, // NB: Defaults to available parallelism
4444
slot_calculator: SlotCalculator::new(
4545
1740681556, // pecorino start timestamp as sane default
4646
0, 1,

0 commit comments

Comments
 (0)