Skip to content

env: use available concurrency to set concurrency defaults #104

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

Merged
merged 2 commits into from
Jun 16, 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
24 changes: 23 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<usize>,

/// The slot calculator for the builder.
pub slot_calculator: SlotCalculator,
Expand Down Expand Up @@ -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<usize> = 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)
})
}
}
3 changes: 2 additions & 1 deletion src/tasks/block/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Simulator {
block_env: BlockEnv,
) -> eyre::Result<BuiltBlock> {
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();

Expand All @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
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,
Expand Down