Skip to content
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

feat: introduce global thread pool and rename ShellOtps to GlobalOpts #9313

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

zerosnacks
Copy link
Member

Motivation

Closes: #8408

Solution

  • Introduces global Rayon thread pool spawned by passing the --jobs flag. If not passed it is not spawned. If passed as --jobs 0 it will use the default Rayon configuration (number of logical CPUs).
  • Renames ShellOpts to GlobalOpts.
  • Only selectively passes down GlobalOpts where required, this can be the case when wanting to indicate a conflicts_with a global argument or you need access to the thread pool. In most cases when checking against the shell configuration it is probably easier to use the e.g. shell::is_json variant over passing it all the way down.

@zerosnacks zerosnacks marked this pull request as ready for review November 13, 2024 16:11
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl looks good! there's a regression re the way rayon thread pool is spawned though, a command like

forge test --show-progress --jobs 3

always fail now with Error: The global thread pool has already been initialized.

same on cast create2

cast create2 --starts-with dead --case-sensitive --deployer 0x0000000000FFe8B47B3e2130213B802212439497 --init-code-hash 0x0c591f26891d6443cf08c5be3584c1e6ae10a4c2f07c5c53218741e9755fb9cd --jobs 5

fails with

Error: The global thread pool has already been initialized.

I think we should keep the current behavior, that is if --jobs flag not passed then we use rayon default (the number of CPUs available) instead single thread.

Probably this one should also follow the config

foundry/crates/cast/src/lib.rs

Lines 2062 to 2063 in a65a5b1

let num_threads = std::thread::available_parallelism().map_or(1, |n| n.get());
let found = AtomicBool::new(false);

@zerosnacks
Copy link
Member Author

zerosnacks commented Nov 14, 2024

impl looks good! there's a regression re the way rayon thread pool is spawned though, a command like

Thanks, good catch!

I think we should keep the current behavior, that is if --jobs flag not passed then we use rayon default (the number of CPUs available) instead single thread.

I had implemented it this way to avoid spawning the global thread pool as the vast majority of commands are not multi-threaded, cc @DaniPopes should we spawn by default?

@zerosnacks zerosnacks marked this pull request as draft November 14, 2024 12:05
let args = Chisel::parse();
args.shell.shell().set();
args.global.try_spawn()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can have an "init" that does both of these?

let found = Arc::new(AtomicBool::new(false));
let timer = Instant::now();

// Loops through all possible salts in parallel until a result is found.
// Each thread iterates over `(i..).step_by(n_threads)`.
for i in 0..n_threads {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't change this, rayon has some overhead here

if let Some(num_threads) = num_threads {
return Some(num_threads.min(2));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the opposite, it caps at 2; this was just to reduce pressure on tests in cast create2, should keep it there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

feat: add global Clap arguments
3 participants