geckolib: merge NUM_THREADS and STYLE_THREAD_POOL into a GlobalStyleData struct

Doing this provides a handy place to drop any future global style things
and also takes up slightly less space with a single `lazy_static!`.
This commit is contained in:
Nathan Froyd 2017-02-22 16:15:58 -05:00
parent fafcdda16a
commit a5d285f102

View file

@ -93,27 +93,42 @@ use stylesheet_loader::StylesheetLoader;
* depend on but good enough for our purposes. * depend on but good enough for our purposes.
*/ */
lazy_static! { struct GlobalStyleData {
/// The number of layout threads, computed statically. // How many threads parallel styling can use.
static ref NUM_THREADS: usize = { pub num_threads: usize,
match env::var("STYLO_THREADS").map(|s| s.parse::<usize>().expect("invalid STYLO_THREADS")) {
// The parallel styling thread pool.
pub style_thread_pool: Option<rayon::ThreadPool>,
}
impl GlobalStyleData {
pub fn new() -> Self {
let stylo_threads = env::var("STYLO_THREADS")
.map(|s| s.parse::<usize>().expect("invalid STYLO_THREADS value"));
let num_threads = match stylo_threads {
Ok(num) => num, Ok(num) => num,
_ => cmp::max(num_cpus::get() * 3 / 4, 1), _ => cmp::max(num_cpus::get() * 3 / 4, 1),
};
let pool = if num_threads <= 1 {
None
} else {
let configuration =
rayon::Configuration::new().set_num_threads(num_threads);
let pool = rayon::ThreadPool::new(configuration).ok();
pool
};
GlobalStyleData {
num_threads: num_threads,
style_thread_pool: pool,
} }
}; }
} }
lazy_static! { lazy_static! {
static ref STYLE_THREAD_POOL: Option<rayon::ThreadPool> = { static ref GLOBAL_STYLE_DATA: GlobalStyleData = {
let num_threads = *NUM_THREADS; GlobalStyleData::new()
if num_threads <= 1 {
return None;
}
let configuration =
rayon::Configuration::new().set_num_threads(num_threads);
let pool = rayon::ThreadPool::new(configuration).ok();
pool
}; };
} }
@ -186,7 +201,9 @@ fn traverse_subtree(element: GeckoElement, raw_data: RawServoStyleSetBorrowed,
debug!("{:?}", ShowSubtreeData(element.as_node())); debug!("{:?}", ShowSubtreeData(element.as_node()));
let shared_style_context = create_shared_context(&per_doc_data); let shared_style_context = create_shared_context(&per_doc_data);
let traversal_driver = if STYLE_THREAD_POOL.is_none() { let ref global_style_data = *GLOBAL_STYLE_DATA;
let traversal_driver = if global_style_data.style_thread_pool.is_none() {
TraversalDriver::Sequential TraversalDriver::Sequential
} else { } else {
TraversalDriver::Parallel TraversalDriver::Parallel
@ -196,7 +213,7 @@ fn traverse_subtree(element: GeckoElement, raw_data: RawServoStyleSetBorrowed,
let known_depth = None; let known_depth = None;
if traversal_driver.is_parallel() { if traversal_driver.is_parallel() {
parallel::traverse_dom(&traversal, element, known_depth, token, parallel::traverse_dom(&traversal, element, known_depth, token,
STYLE_THREAD_POOL.as_ref().unwrap()); global_style_data.style_thread_pool.as_ref().unwrap());
} else { } else {
sequential::traverse_dom(&traversal, element, token); sequential::traverse_dom(&traversal, element, token);
} }
@ -372,7 +389,7 @@ pub extern "C" fn Servo_AnimationValue_Release(anim: RawServoAnimationValueBorro
#[no_mangle] #[no_mangle]
pub extern "C" fn Servo_StyleWorkerThreadCount() -> u32 { pub extern "C" fn Servo_StyleWorkerThreadCount() -> u32 {
*NUM_THREADS as u32 GLOBAL_STYLE_DATA.num_threads as u32
} }
#[no_mangle] #[no_mangle]