Skip to content

Commit

Permalink
geckolib: use a global thread pool for styling
Browse files Browse the repository at this point in the history
By having a single thread pool, rather than one per document, we use
less memory.  This addresses
https://bugzilla.mozilla.org/show_bug.cgi?id=1324250.
  • Loading branch information
froydnj committed Feb 22, 2017
1 parent 469ed93 commit 3e81f84
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions components/style/gecko/data.rs
Expand Up @@ -14,7 +14,6 @@ use media_queries::Device;
use num_cpus;
use parking_lot::RwLock;
use properties::ComputedValues;
use rayon;
use std::cmp;
use std::collections::HashMap;
use std::env;
Expand Down Expand Up @@ -48,13 +47,6 @@ pub struct PerDocumentStyleDataImpl {
/// Unused. Will go away when we actually implement transitions and
/// animations properly.
pub expired_animations: Arc<RwLock<HashMap<OpaqueNode, Vec<Animation>>>>,

/// The worker thread pool.
/// FIXME(bholley): This shouldn't be per-document.
pub work_queue: Option<rayon::ThreadPool>,

/// The number of threads of the work queue.
pub num_threads: usize,
}

/// The data itself is an `AtomicRefCell`, which guarantees the proper semantics
Expand Down Expand Up @@ -86,14 +78,6 @@ impl PerDocumentStyleData {
new_animations_receiver: new_anims_receiver,
running_animations: Arc::new(RwLock::new(HashMap::new())),
expired_animations: Arc::new(RwLock::new(HashMap::new())),
work_queue: if *NUM_THREADS <= 1 {
None
} else {
let configuration =
rayon::Configuration::new().set_num_threads(*NUM_THREADS);
rayon::ThreadPool::new(configuration).ok()
},
num_threads: *NUM_THREADS,
}))
}

Expand Down Expand Up @@ -141,9 +125,3 @@ unsafe impl HasFFI for PerDocumentStyleData {
}
unsafe impl HasSimpleFFI for PerDocumentStyleData {}
unsafe impl HasBoxFFI for PerDocumentStyleData {}

impl Drop for PerDocumentStyleDataImpl {
fn drop(&mut self) {
let _ = self.work_queue.take();
}
}
1 change: 1 addition & 0 deletions ports/geckolib/Cargo.toml
Expand Up @@ -23,6 +23,7 @@ libc = "0.2"
log = {version = "0.3.5", features = ["release_max_level_info"]}
num_cpus = "1.1.0"
parking_lot = "0.3"
rayon = "0.6"
selectors = {path = "../../components/selectors"}
servo_url = {path = "../../components/url"}
style = {path = "../../components/style", features = ["gecko"]}
Expand Down
21 changes: 18 additions & 3 deletions ports/geckolib/glue.rs
Expand Up @@ -9,6 +9,7 @@ use cssparser::ToCss as ParserToCss;
use env_logger::LogBuilder;
use euclid::Size2D;
use parking_lot::RwLock;
use rayon;
use selectors::Element;
use servo_url::ServoUrl;
use std::borrow::Cow;
Expand Down Expand Up @@ -90,6 +91,20 @@ use stylesheet_loader::StylesheetLoader;
* depend on but good enough for our purposes.
*/

lazy_static! {
static ref STYLE_THREAD_POOL: Option<rayon::ThreadPool> = {
let num_threads = *NUM_THREADS;
if num_threads <= 1 {
return None;
}

let configuration =
rayon::Configuration::new().set_num_threads(num_threads);
let pool = rayon::ThreadPool::new(configuration).ok();
pool
};
}

#[no_mangle]
pub extern "C" fn Servo_Initialize() -> () {
// Initialize logging.
Expand Down Expand Up @@ -148,7 +163,7 @@ fn traverse_subtree(element: GeckoElement, raw_data: RawServoStyleSetBorrowed,
}
}

let mut per_doc_data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
let per_doc_data = PerDocumentStyleData::from_ffi(raw_data).borrow();

let token = RecalcStyleOnly::pre_traverse(element, &per_doc_data.stylist, unstyled_children_only);
if !token.should_traverse() {
Expand All @@ -159,7 +174,7 @@ fn traverse_subtree(element: GeckoElement, raw_data: RawServoStyleSetBorrowed,
debug!("{:?}", ShowSubtreeData(element.as_node()));

let shared_style_context = create_shared_context(&per_doc_data);
let traversal_driver = if per_doc_data.num_threads == 1 || per_doc_data.work_queue.is_none() {
let traversal_driver = if STYLE_THREAD_POOL.is_none() {
TraversalDriver::Sequential
} else {
TraversalDriver::Parallel
Expand All @@ -169,7 +184,7 @@ fn traverse_subtree(element: GeckoElement, raw_data: RawServoStyleSetBorrowed,
let known_depth = None;
if traversal_driver.is_parallel() {
parallel::traverse_dom(&traversal, element, known_depth, token,
per_doc_data.work_queue.as_mut().unwrap());
STYLE_THREAD_POOL.as_ref().unwrap());
} else {
sequential::traverse_dom(&traversal, element, token);
}
Expand Down
2 changes: 2 additions & 0 deletions ports/geckolib/lib.rs
Expand Up @@ -9,9 +9,11 @@ extern crate atomic_refcell;
extern crate cssparser;
extern crate env_logger;
extern crate euclid;
#[macro_use] extern crate lazy_static;
extern crate libc;
#[macro_use] extern crate log;
extern crate parking_lot;
extern crate rayon;
extern crate selectors;
extern crate servo_url;
extern crate style;
Expand Down

0 comments on commit 3e81f84

Please sign in to comment.