Skip to content

Commit 4a1ed69

Browse files
committed
memoizes the available parallelism call
- bump default concurrency const to 8 from 4 - memoize the result of the available parallelism call - make `concurrency_limit` config property into `Option<usize>` instead of `usize`
1 parent bfa874f commit 4a1ed69

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/config.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ pub type HostProvider = FillProvider<
5151
RootProvider,
5252
>;
5353

54-
/// The default concurrency limit for the builder if system call fails.
55-
pub const DEFAULT_CONCURRENCY_LIMIT: usize = 4;
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;
5657

5758
/// Configuration for a builder running a specific rollup on a specific host
5859
/// chain.
@@ -153,10 +154,9 @@ pub struct BuilderConfig {
153154
/// The max number of simultaneous block simulations to run.
154155
#[from_env(
155156
var = "CONCURRENCY_LIMIT",
156-
desc = "The max number of simultaneous block simulations to run",
157-
optional
157+
desc = "The max number of simultaneous block simulations to run"
158158
)]
159-
pub concurrency_limit: usize,
159+
pub concurrency_limit: Option<usize>,
160160

161161
/// The slot calculator for the builder.
162162
pub slot_calculator: SlotCalculator,
@@ -281,13 +281,21 @@ impl BuilderConfig {
281281
SignetCfgEnv { chain_id: self.ru_chain_id }
282282
}
283283

284-
/// Get the concurrency limit for the current system.
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.
285286
pub fn concurrency_limit(&self) -> usize {
286-
if self.concurrency_limit == 0 {
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(|| {
287296
std::thread::available_parallelism()
288297
.map(|p| p.get())
289-
.unwrap_or(DEFAULT_CONCURRENCY_LIMIT);
290-
}
291-
self.concurrency_limit
298+
.unwrap_or(DEFAULT_CONCURRENCY_LIMIT)
299+
})
292300
}
293301
}

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)