From 73917cce83d2225b51b29c374d861d71ec69435f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 7 Oct 2016 12:22:06 +0200 Subject: style: Use rayon instead of our custom work queue. --- components/layout/parallel.rs | 90 ++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 40 deletions(-) (limited to 'components/layout/parallel.rs') diff --git a/components/layout/parallel.rs b/components/layout/parallel.rs index 29e53bf9077..03aaa512d3e 100644 --- a/components/layout/parallel.rs +++ b/components/layout/parallel.rs @@ -12,12 +12,11 @@ use context::{LayoutContext, SharedLayoutContext}; use flow::{self, Flow, MutableFlowUtils, PostorderFlowTraversal, PreorderFlowTraversal}; use flow_ref::FlowRef; use profile_traits::time::{self, TimerMetadata, profile}; +use rayon; use std::mem; use std::sync::atomic::{AtomicIsize, Ordering}; use style::dom::UnsafeNode; -use style::parallel::{CHUNK_SIZE, WorkQueueData}; -use style::parallel::run_queue_with_custom_work_data_type; -use style::workqueue::{WorkQueue, WorkUnit, WorkerProxy}; +use style::parallel::CHUNK_SIZE; use traversal::{AssignISizes, BubbleISizes}; use traversal::AssignBSizes; use util::opts; @@ -50,10 +49,8 @@ pub fn borrowed_flow_to_unsafe_flow(flow: &Flow) -> UnsafeFlow { } } -pub type UnsafeFlowList = (Box>, usize); - -pub type ChunkedFlowTraversalFunction = - extern "Rust" fn(UnsafeFlowList, &mut WorkerProxy); +pub type ChunkedFlowTraversalFunction<'scope> = + extern "Rust" fn(Box<[UnsafeFlow]>, &'scope SharedLayoutContext, &rayon::Scope<'scope>); pub type FlowTraversalFunction = extern "Rust" fn(UnsafeFlow, &SharedLayoutContext); @@ -133,27 +130,35 @@ trait ParallelPostorderFlowTraversal : PostorderFlowTraversal { /// A parallel top-down flow traversal. trait ParallelPreorderFlowTraversal : PreorderFlowTraversal { - fn run_parallel(&self, - unsafe_flows: UnsafeFlowList, - proxy: &mut WorkerProxy); + fn run_parallel<'scope>(&self, + unsafe_flows: &[UnsafeFlow], + layout_context: &'scope SharedLayoutContext, + scope: &rayon::Scope<'scope>); fn should_record_thread_ids(&self) -> bool; #[inline(always)] - fn run_parallel_helper(&self, - unsafe_flows: UnsafeFlowList, - proxy: &mut WorkerProxy, - top_down_func: ChunkedFlowTraversalFunction, - bottom_up_func: FlowTraversalFunction) { - let mut discovered_child_flows = Vec::new(); - for unsafe_flow in *unsafe_flows.0 { + fn run_parallel_helper<'scope>(&self, + unsafe_flows: &[UnsafeFlow], + layout_context: &'scope SharedLayoutContext, + scope: &rayon::Scope<'scope>, + top_down_func: ChunkedFlowTraversalFunction<'scope>, + bottom_up_func: FlowTraversalFunction) + { + let mut discovered_child_flows = vec![]; + for unsafe_flow in unsafe_flows { let mut had_children = false; unsafe { // Get a real flow. - let flow: &mut Flow = mem::transmute(unsafe_flow); + let flow: &mut Flow = mem::transmute(*unsafe_flow); if self.should_record_thread_ids() { - flow::mut_base(flow).thread_id = proxy.worker_index(); + // FIXME(emilio): With the switch to rayon we can no longer + // access a thread id from here easily. Either instrument + // rayon (the unstable feature) to get a worker thread + // identifier, or remove all the layout tinting mode. + // + // flow::mut_base(flow).thread_id = proxy.worker_index(); } if self.should_process(flow) { @@ -170,25 +175,29 @@ trait ParallelPreorderFlowTraversal : PreorderFlowTraversal { // If there were no more children, start assigning block-sizes. if !had_children { - bottom_up_func(unsafe_flow, proxy.user_data()) + bottom_up_func(*unsafe_flow, layout_context) } } for chunk in discovered_child_flows.chunks(CHUNK_SIZE) { - proxy.push(WorkUnit { - fun: top_down_func, - data: (box chunk.iter().cloned().collect(), 0), + let nodes = chunk.iter().cloned().collect::>().into_boxed_slice(); + + scope.spawn(move |scope| { + top_down_func(nodes, layout_context, scope); }); } } } impl<'a> ParallelPreorderFlowTraversal for AssignISizes<'a> { - fn run_parallel(&self, - unsafe_flows: UnsafeFlowList, - proxy: &mut WorkerProxy) { + fn run_parallel<'scope>(&self, + unsafe_flows: &[UnsafeFlow], + layout_context: &'scope SharedLayoutContext, + scope: &rayon::Scope<'scope>) + { self.run_parallel_helper(unsafe_flows, - proxy, + layout_context, + scope, assign_inline_sizes, assign_block_sizes_and_store_overflow) } @@ -200,13 +209,13 @@ impl<'a> ParallelPreorderFlowTraversal for AssignISizes<'a> { impl<'a> ParallelPostorderFlowTraversal for AssignBSizes<'a> {} -fn assign_inline_sizes(unsafe_flows: UnsafeFlowList, - proxy: &mut WorkerProxy) { - let shared_layout_context = proxy.user_data(); +fn assign_inline_sizes<'scope>(unsafe_flows: Box<[UnsafeFlow]>, + shared_layout_context: &'scope SharedLayoutContext, + scope: &rayon::Scope<'scope>) { let assign_inline_sizes_traversal = AssignISizes { shared_context: &shared_layout_context.style_context, }; - assign_inline_sizes_traversal.run_parallel(unsafe_flows, proxy) + assign_inline_sizes_traversal.run_parallel(&unsafe_flows, shared_layout_context, scope) } fn assign_block_sizes_and_store_overflow( @@ -224,20 +233,21 @@ pub fn traverse_flow_tree_preorder( profiler_metadata: Option, time_profiler_chan: time::ProfilerChan, shared_layout_context: &SharedLayoutContext, - queue: &mut WorkQueue) { + queue: &rayon::ThreadPool) { if opts::get().bubble_inline_sizes_separately { let layout_context = LayoutContext::new(shared_layout_context); let bubble_inline_sizes = BubbleISizes { layout_context: &layout_context }; root.traverse_postorder(&bubble_inline_sizes); } - run_queue_with_custom_work_data_type(queue, |queue| { - profile(time::ProfilerCategory::LayoutParallelWarmup, profiler_metadata, - time_profiler_chan, || { - queue.push(WorkUnit { - fun: assign_inline_sizes, - data: (box vec![borrowed_flow_to_unsafe_flow(root)], 0), - }) + let nodes = vec![borrowed_flow_to_unsafe_flow(root)].into_boxed_slice(); + + queue.install(move || { + rayon::scope(move |scope| { + profile(time::ProfilerCategory::LayoutParallelWarmup, + profiler_metadata, time_profiler_chan, move || { + assign_inline_sizes(nodes, &shared_layout_context, scope); + }); }); - }, shared_layout_context); + }); } -- cgit v1.2.3