hook up the max_threads flag

This commit is contained in:
Folkert 2022-05-07 14:18:28 +02:00
parent 2066999908
commit 35bf66f650
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C

View File

@ -1158,10 +1158,32 @@ pub fn load<'a>(
render: RenderTarget,
threading: Threading,
) -> Result<LoadResult<'a>, LoadingProblem<'a>> {
// When compiling to wasm, we cannot spawn extra threads
// so we have a single-threaded implementation
if threading == Threading::Single || cfg!(target_family = "wasm") {
load_single_threaded(
enum Threads {
Single,
Many(usize),
}
let threads = {
if cfg!(target_family = "wasm") {
// When compiling to wasm, we cannot spawn extra threads
// so we have a single-threaded implementation
Threads::Single
} else {
match std::thread::available_parallelism().map(|v| v.get()) {
Err(_) => Threads::Single,
Ok(0) => unreachable!("NonZeroUsize"),
Ok(1) => Threads::Single,
Ok(reported) => match threading {
Threading::Single => Threads::Single,
Threading::AllAvailable => Threads::Many(reported),
Threading::AtMost(at_most) => Threads::Many(Ord::min(reported, at_most)),
},
}
}
};
match threads {
Threads::Single => load_single_threaded(
arena,
load_start,
src_dir,
@ -1170,9 +1192,8 @@ pub fn load<'a>(
target_info,
cached_subs,
render,
)
} else {
load_multi_threaded(
),
Threads::Many(threads) => load_multi_threaded(
arena,
load_start,
src_dir,
@ -1181,8 +1202,8 @@ pub fn load<'a>(
target_info,
cached_subs,
render,
threading,
)
threads,
),
}
}
@ -1392,7 +1413,7 @@ fn load_multi_threaded<'a>(
target_info: TargetInfo,
cached_subs: MutMap<ModuleId, (Subs, Vec<(Symbol, Variable)>)>,
render: RenderTarget,
threading: Threading,
available_threads: usize,
) -> Result<LoadResult<'a>, LoadingProblem<'a>> {
let LoadStart {
arc_modules,
@ -1426,13 +1447,22 @@ fn load_multi_threaded<'a>(
// doing .max(1) on the entire expression guards against
// num_cpus returning 0, while also avoiding wrapping
// unsigned subtraction overflow.
let default_num_workers = num_cpus::get().max(2) - 1;
// let default_num_workers = num_cpus::get().max(2) - 1;
let available_workers = available_threads - 1;
let num_workers = match env::var("ROC_NUM_WORKERS") {
Ok(env_str) => env_str.parse::<usize>().unwrap_or(default_num_workers),
Err(_) => default_num_workers,
Ok(env_str) => env_str
.parse::<usize>()
.unwrap_or(available_workers)
.min(available_workers),
Err(_) => available_workers,
};
assert!(
num_workers >= 1,
"`load_multi_threaded` needs at least one worker"
);
// an arena for every worker, stored in an arena-allocated bumpalo vec to make the lifetimes work
let arenas = std::iter::repeat_with(Bump::new).take(num_workers);
let worker_arenas = arena.alloc(bumpalo::collections::Vec::from_iter_in(arenas, arena));