Skip to content

Commit b5a10e9

Browse files
author
bors-servo
authored
Auto merge of #22487 - emilio:single-layout-thread-pool, r=jdm
style: Make Servo use a single thread-pool for layout-related tasks per process. Instead of per-document. This also allows to reuse this thread-pool if needed for other stuff, like parallel CSS parsing (#22478), and to share more code with Gecko, which is always nice. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/22487) <!-- Reviewable:end -->
2 parents 596b063 + 006e71c commit b5a10e9

File tree

12 files changed

+56
-77
lines changed

12 files changed

+56
-77
lines changed

components/constellation/pipeline.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ impl Pipeline {
219219
window_size: window_size,
220220
pipeline_port: pipeline_port,
221221
content_process_shutdown_chan: Some(layout_content_process_shutdown_chan),
222-
layout_threads: PREFS.get("layout.threads").as_u64().expect("count") as usize,
223222
};
224223

225224
if let Err(e) =
@@ -556,12 +555,6 @@ impl UnprivilegedPipelineContent {
556555
Some(self.layout_content_process_shutdown_chan),
557556
self.webrender_api_sender,
558557
self.webrender_document,
559-
self.prefs
560-
.get("layout.threads")
561-
.expect("exists")
562-
.value()
563-
.as_u64()
564-
.expect("count") as usize,
565558
paint_time_metrics,
566559
);
567560

components/layout_thread/lib.rs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ use std::thread;
102102
use std::time::Duration;
103103
use style::animation::Animation;
104104
use style::context::{QuirksMode, RegisteredSpeculativePainter, RegisteredSpeculativePainters};
105-
use style::context::{SharedStyleContext, StyleSystemOptions, ThreadLocalStyleContextCreationInfo};
105+
use style::context::{SharedStyleContext, ThreadLocalStyleContextCreationInfo};
106106
use style::dom::{ShowSubtree, ShowSubtreeDataAndPrimaryValues, TElement, TNode};
107107
use style::driver;
108108
use style::error_reporting::RustLogReporter;
109+
use style::global_style_data::{GLOBAL_STYLE_DATA, STYLE_THREAD_POOL};
109110
use style::invalidation::element::restyle_hints::RestyleHint;
110111
use style::logical_geometry::LogicalPoint;
111112
use style::media_queries::{Device, MediaList, MediaType};
@@ -178,9 +179,6 @@ pub struct LayoutThread {
178179
/// Is this the first reflow in this LayoutThread?
179180
first_reflow: Cell<bool>,
180181

181-
/// The workers that we use for parallel operation.
182-
parallel_traversal: Option<rayon::ThreadPool>,
183-
184182
/// Flag to indicate whether to use parallel operations
185183
parallel_flag: bool,
186184

@@ -238,10 +236,6 @@ pub struct LayoutThread {
238236
/// only be a test-mode timer during testing for animations.
239237
timer: Timer,
240238

241-
// Number of layout threads. This is copied from `servo_config::opts`, but we'd
242-
// rather limit the dependency on that module here.
243-
layout_threads: usize,
244-
245239
/// Paint time metrics.
246240
paint_time_metrics: PaintTimeMetrics,
247241

@@ -270,7 +264,6 @@ impl LayoutThreadFactory for LayoutThread {
270264
content_process_shutdown_chan: Option<IpcSender<()>>,
271265
webrender_api_sender: webrender_api::RenderApiSender,
272266
webrender_document: webrender_api::DocumentId,
273-
layout_threads: usize,
274267
paint_time_metrics: PaintTimeMetrics,
275268
) {
276269
thread::Builder::new()
@@ -308,7 +301,6 @@ impl LayoutThreadFactory for LayoutThread {
308301
mem_profiler_chan.clone(),
309302
webrender_api_sender,
310303
webrender_document,
311-
layout_threads,
312304
paint_time_metrics,
313305
);
314306

@@ -470,7 +462,6 @@ impl LayoutThread {
470462
mem_profiler_chan: profile_mem::ProfilerChan,
471463
webrender_api_sender: webrender_api::RenderApiSender,
472464
webrender_document: webrender_api::DocumentId,
473-
layout_threads: usize,
474465
paint_time_metrics: PaintTimeMetrics,
475466
) -> LayoutThread {
476467
// The device pixel ratio is incorrect (it does not have the hidpi value),
@@ -481,17 +472,6 @@ impl LayoutThread {
481472
TypedScale::new(opts::get().device_pixels_per_px.unwrap_or(1.0)),
482473
);
483474

484-
let workers = rayon::ThreadPoolBuilder::new()
485-
.num_threads(layout_threads)
486-
.start_handler(|_| thread_state::initialize_layout_worker_thread())
487-
.build();
488-
let parallel_traversal = if layout_threads > 1 {
489-
Some(workers.expect("ThreadPool creation failed"))
490-
} else {
491-
None
492-
};
493-
debug!("Possible layout Threads: {}", layout_threads);
494-
495475
// Create the channel on which new animations can be sent.
496476
let (new_animations_sender, new_animations_receiver) = unbounded();
497477

@@ -521,7 +501,6 @@ impl LayoutThread {
521501
first_reflow: Cell::new(true),
522502
font_cache_receiver: font_cache_receiver,
523503
font_cache_sender: ipc_font_cache_sender,
524-
parallel_traversal: parallel_traversal,
525504
parallel_flag: true,
526505
generation: Cell::new(0),
527506
new_animations_sender: new_animations_sender,
@@ -563,7 +542,6 @@ impl LayoutThread {
563542
} else {
564543
Timer::new()
565544
},
566-
layout_threads: layout_threads,
567545
paint_time_metrics: paint_time_metrics,
568546
layout_query_waiting_time: Histogram::new(),
569547
}
@@ -596,8 +574,8 @@ impl LayoutThread {
596574
id: self.id,
597575
style_context: SharedStyleContext {
598576
stylist: &self.stylist,
599-
options: StyleSystemOptions::default(),
600-
guards: guards,
577+
options: GLOBAL_STYLE_DATA.options.clone(),
578+
guards,
601579
visited_styles_enabled: false,
602580
running_animations: self.running_animations.clone(),
603581
expired_animations: self.expired_animations.clone(),
@@ -881,7 +859,6 @@ impl LayoutThread {
881859
info.content_process_shutdown_chan,
882860
self.webrender_api.clone_sender(),
883861
self.webrender_document,
884-
info.layout_threads,
885862
info.paint_time_metrics,
886863
);
887864
}
@@ -924,8 +901,6 @@ impl LayoutThread {
924901
);
925902

926903
self.root_flow.borrow_mut().take();
927-
// Drop the rayon threadpool if present.
928-
let _ = self.parallel_traversal.take();
929904
self.background_hang_monitor.unregister();
930905
}
931906

@@ -1166,7 +1141,7 @@ impl LayoutThread {
11661141

11671142
// Parallelize if there's more than 750 objects based on rzambre's suggestion
11681143
// https://github.com/servo/servo/issues/10110
1169-
self.parallel_flag = self.layout_threads > 1 && data.dom_count > 750;
1144+
self.parallel_flag = data.dom_count > 750;
11701145
debug!("layout: received layout request for: {}", self.url);
11711146
debug!("Number of objects in DOM: {}", data.dom_count);
11721147
debug!("layout: parallel? {}", self.parallel_flag);
@@ -1373,10 +1348,13 @@ impl LayoutThread {
13731348
// Create a layout context for use throughout the following passes.
13741349
let mut layout_context = self.build_layout_context(guards.clone(), true, &map);
13751350

1376-
let thread_pool = if self.parallel_flag {
1377-
self.parallel_traversal.as_ref()
1351+
let (thread_pool, num_threads) = if self.parallel_flag {
1352+
(
1353+
STYLE_THREAD_POOL.style_thread_pool.as_ref(),
1354+
STYLE_THREAD_POOL.num_threads,
1355+
)
13781356
} else {
1379-
None
1357+
(None, 1)
13801358
};
13811359

13821360
let traversal = RecalcStyleAndConstructFlows::new(layout_context);
@@ -1404,14 +1382,14 @@ impl LayoutThread {
14041382
},
14051383
);
14061384
// TODO(pcwalton): Measure energy usage of text shaping, perhaps?
1407-
let text_shaping_time = (font::get_and_reset_text_shaping_performance_counter() as u64) /
1408-
(self.layout_threads as u64);
1385+
let text_shaping_time =
1386+
font::get_and_reset_text_shaping_performance_counter() / num_threads;
14091387
profile_time::send_profile_data(
14101388
profile_time::ProfilerCategory::LayoutTextShaping,
14111389
self.profiler_metadata(),
14121390
&self.time_profiler_chan,
14131391
0,
1414-
text_shaping_time,
1392+
text_shaping_time as u64,
14151393
0,
14161394
0,
14171395
);
@@ -1742,12 +1720,16 @@ impl LayoutThread {
17421720
|| {
17431721
let profiler_metadata = self.profiler_metadata();
17441722

1745-
if let (true, Some(traversal)) =
1746-
(self.parallel_flag, self.parallel_traversal.as_ref())
1747-
{
1723+
let thread_pool = if self.parallel_flag {
1724+
STYLE_THREAD_POOL.style_thread_pool.as_ref()
1725+
} else {
1726+
None
1727+
};
1728+
1729+
if let Some(pool) = thread_pool {
17481730
// Parallel mode.
17491731
LayoutThread::solve_constraints_parallel(
1750-
traversal,
1732+
pool,
17511733
FlowRef::deref_mut(root_flow),
17521734
profiler_metadata,
17531735
self.time_profiler_chan.clone(),
@@ -1913,7 +1895,8 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
19131895
))))
19141896
}
19151897

1916-
let shared_lock = SharedRwLock::new();
1898+
let shared_lock = &GLOBAL_STYLE_DATA.shared_lock;
1899+
19171900
// FIXME: presentational-hints.css should be at author origin with zero specificity.
19181901
// (Does it make a difference?)
19191902
let mut user_or_user_agent_stylesheets = vec![
@@ -1958,7 +1941,7 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
19581941
)?;
19591942

19601943
Ok(UserAgentStylesheets {
1961-
shared_lock: shared_lock,
1944+
shared_lock: shared_lock.clone(),
19621945
user_or_user_agent_stylesheets: user_or_user_agent_stylesheets,
19631946
quirks_mode_stylesheet: quirks_mode_stylesheet,
19641947
})

components/layout_traits/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub trait LayoutThreadFactory {
4343
content_process_shutdown_chan: Option<IpcSender<()>>,
4444
webrender_api_sender: webrender_api::RenderApiSender,
4545
webrender_document: webrender_api::DocumentId,
46-
layout_threads: usize,
4746
paint_time_metrics: PaintTimeMetrics,
4847
);
4948
}

components/script/dom/htmliframeelement.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use script_traits::{
3838
WindowSizeData,
3939
};
4040
use script_traits::{NewLayoutInfo, ScriptMsg};
41-
use servo_config::prefs::PREFS;
4241
use servo_url::ServoUrl;
4342
use std::cell::Cell;
4443
use style::attr::{AttrValue, LengthOrPercentageOrAuto};
@@ -204,7 +203,6 @@ impl HTMLIFrameElement {
204203
},
205204
device_pixel_ratio: window.device_pixel_ratio(),
206205
},
207-
layout_threads: PREFS.get("layout.threads").as_u64().expect("count") as usize,
208206
};
209207

210208
self.pipeline_id.set(Some(new_pipeline_id));

components/script/dom/windowproxy.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use msg::constellation_msg::BrowsingContextId;
4545
use msg::constellation_msg::PipelineId;
4646
use msg::constellation_msg::TopLevelBrowsingContextId;
4747
use script_traits::{AuxiliaryBrowsingContextLoadInfo, LoadData, NewLayoutInfo, ScriptMsg};
48-
use servo_config::prefs::PREFS;
4948
use servo_url::ServoUrl;
5049
use std::cell::Cell;
5150
use std::ptr;
@@ -290,7 +289,6 @@ impl WindowProxy {
290289
pipeline_port: pipeline_receiver,
291290
content_process_shutdown_chan: None,
292291
window_size: window.window_size(),
293-
layout_threads: PREFS.get("layout.threads").as_u64().expect("count") as usize,
294292
};
295293
let constellation_msg = ScriptMsg::ScriptNewAuxiliary(load_info, pipeline_sender);
296294
window.send_to_constellation(constellation_msg);

components/script/script_thread.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,6 @@ impl ScriptThread {
19101910
window_size,
19111911
pipeline_port,
19121912
content_process_shutdown_chan,
1913-
layout_threads,
19141913
} = new_layout_info;
19151914

19161915
let layout_pair = unbounded();
@@ -1927,7 +1926,6 @@ impl ScriptThread {
19271926
script_chan: self.control_chan.clone(),
19281927
image_cache: self.image_cache.clone(),
19291928
content_process_shutdown_chan: content_process_shutdown_chan,
1930-
layout_threads: layout_threads,
19311929
paint_time_metrics: PaintTimeMetrics::new(
19321930
new_pipeline_id,
19331931
self.time_profiler_chan.clone(),

components/script_layout_interface/message.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,5 @@ pub struct NewLayoutThreadInfo {
220220
pub script_chan: IpcSender<ConstellationControlMsg>,
221221
pub image_cache: Arc<dyn ImageCache>,
222222
pub content_process_shutdown_chan: Option<IpcSender<()>>,
223-
pub layout_threads: usize,
224223
pub paint_time_metrics: PaintTimeMetrics,
225224
}

components/script_traits/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ pub struct NewLayoutInfo {
195195
pub pipeline_port: IpcReceiver<LayoutControlMsg>,
196196
/// A shutdown channel so that layout can tell the content process to shut down when it's done.
197197
pub content_process_shutdown_chan: Option<IpcSender<()>>,
198-
/// Number of threads to use for layout.
199-
pub layout_threads: usize,
200198
}
201199

202200
/// When a pipeline is closed, should its browsing context be discarded too?

components/style/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ path = "lib.rs"
1616
doctest = false
1717

1818
[features]
19-
gecko = ["num_cpus",
20-
"style_traits/gecko", "fallible/known_system_malloc"]
19+
gecko = ["style_traits/gecko", "fallible/known_system_malloc"]
2120
use_bindgen = ["bindgen", "regex", "toml"]
2221
servo = ["serde", "style_traits/servo", "servo_atoms", "servo_config", "html5ever",
2322
"cssparser/serde", "encoding_rs", "malloc_size_of/servo", "arrayvec/use_union",
@@ -47,7 +46,7 @@ log = "0.4"
4746
malloc_size_of = { path = "../malloc_size_of" }
4847
malloc_size_of_derive = { path = "../malloc_size_of_derive" }
4948
matches = "0.1"
50-
num_cpus = {version = "1.1.0", optional = true}
49+
num_cpus = {version = "1.1.0"}
5150
num-integer = "0.1"
5251
num-traits = "0.2"
5352
num-derive = "0.2"

components/style/gecko/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod non_ts_pseudo_class_list;
1010
pub mod arc_types;
1111
pub mod conversions;
1212
pub mod data;
13-
pub mod global_style_data;
1413
pub mod media_features;
1514
pub mod media_queries;
1615
pub mod pseudo_element;

0 commit comments

Comments
 (0)