diff --git a/components/style/global_style_data.rs b/components/style/global_style_data.rs index 576ac425b5a..eda1d771a6b 100644 --- a/components/style/global_style_data.rs +++ b/components/style/global_style_data.rs @@ -14,9 +14,7 @@ use crate::thread_state; use gecko_profiler; use parking_lot::{Mutex, RwLock, RwLockReadGuard}; use rayon; -use std::env; -use std::io; -use std::thread; +use std::{io, thread}; /// Global style data pub struct GlobalStyleData { @@ -114,50 +112,40 @@ impl StyleThreadPool { } } +#[cfg(feature = "servo")] +fn stylo_threads_pref() -> i32 { + pref!(layout.threads) +} + +#[cfg(feature = "gecko")] +fn stylo_threads_pref() -> i32 { + static_prefs::pref!("layout.css.stylo-threads") +} + lazy_static! { /// Global thread pool pub static ref STYLE_THREAD_POOL: std::sync::Mutex = { - let stylo_threads = env::var("STYLO_THREADS") - .map(|s| s.parse::().expect("invalid STYLO_THREADS value")); - let mut num_threads = match stylo_threads { - Ok(num) => num, - #[cfg(feature = "servo")] - _ => { - use servo_config::pref; - // We always set this pref on startup, before layout or script - // have had a chance of accessing (and thus creating) the - // thread-pool. - pref!(layout.threads) as usize - } - #[cfg(feature = "gecko")] - _ => { - // The default heuristic is num_virtual_cores * .75. This gives - // us three threads on a hyper-threaded dual core, and six - // threads on a hyper-threaded quad core. The performance - // benefit of additional threads seems to level off at around - // six, so we cap it there on many-core machines - // (see bug 1431285 comment 14). - use num_cpus; - use std::cmp; - cmp::min(cmp::max(num_cpus::get() * 3 / 4, 1), 6) - } + // We always set this pref on startup, before layout or script have had a chance of + // accessing (and thus creating) the thread-pool. + let threads_pref: i32 = stylo_threads_pref(); + + let num_threads = if threads_pref >= 0 { + threads_pref as usize + } else { + use num_cpus; + use std::cmp; + // The default heuristic is num_virtual_cores * .75. This gives us three threads on a + // hyper-threaded dual core, and six threads on a hyper-threaded quad core. + // + // The performance benefit of additional threads seems to level off at around six, so + // we cap it there on many-core machines (see bug 1431285 comment 14). + let threads = cmp::min(cmp::max(num_cpus::get() * 3 / 4, 1), 6); + // There's no point in creating a thread pool if there's one thread. + if threads == 1 { 0 } else { threads } }; - // If num_threads is one, there's no point in creating a thread pool, so - // force it to zero. - // - // We allow developers to force a one-thread pool for testing via a - // special environmental variable. - if num_threads == 1 { - let force_pool = env::var("FORCE_STYLO_THREAD_POOL") - .ok().map_or(false, |s| s.parse::().expect("invalid FORCE_STYLO_THREAD_POOL value") == 1); - if !force_pool { - num_threads = 0; - } - } - - let pool = if num_threads < 1 { - None + let (pool, num_threads) = if num_threads < 1 { + (None, None) } else { let workers = rayon::ThreadPoolBuilder::new() .spawn_handler(thread_spawn) @@ -167,15 +155,11 @@ lazy_static! { .exit_handler(thread_shutdown) .stack_size(STYLE_THREAD_STACK_SIZE_KB * 1024) .build(); - workers.ok() + (workers.ok(), Some(num_threads)) }; std::sync::Mutex::new(StyleThreadPool { - num_threads: if num_threads > 0 { - Some(num_threads) - } else { - None - }, + num_threads, style_thread_pool: RwLock::new(pool), }) };