Skip to content

Commit

Permalink
Reduce allocations during parallel layout
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Jun 6, 2017
1 parent ea154d8 commit 7fd1626
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions components/layout/parallel.rs
Expand Up @@ -14,6 +14,7 @@ use flow_ref::FlowRef;
use profile_traits::time::{self, TimerMetadata, profile};
use rayon;
use servo_config::opts;
use smallvec::SmallVec;
use std::mem;
use std::sync::atomic::{AtomicIsize, Ordering};
use style::dom::UnsafeNode;
Expand All @@ -25,6 +26,8 @@ pub use style::parallel::traverse_dom;
/// Traversal chunk size.
const CHUNK_SIZE: usize = 16;

pub type FlowList = SmallVec<[UnsafeNode; CHUNK_SIZE]>;

#[allow(dead_code)]
fn static_assertion(node: UnsafeNode) {
unsafe {
Expand Down Expand Up @@ -128,7 +131,7 @@ fn top_down_flow<'scope>(unsafe_flows: &[UnsafeFlow],
assign_isize_traversal: &'scope AssignISizes,
assign_bsize_traversal: &'scope AssignBSizes)
{
let mut discovered_child_flows = vec![];
let mut discovered_child_flows = FlowList::new();

for unsafe_flow in unsafe_flows {
let mut had_children = false;
Expand Down Expand Up @@ -161,12 +164,29 @@ fn top_down_flow<'scope>(unsafe_flows: &[UnsafeFlow],
}
}

for chunk in discovered_child_flows.chunks(CHUNK_SIZE) {
let nodes = chunk.iter().cloned().collect::<Vec<_>>().into_boxed_slice();
if discovered_child_flows.is_empty() {
return
}

scope.spawn(move |scope| {
top_down_flow(&nodes, scope, &assign_isize_traversal, &assign_bsize_traversal);
});
if discovered_child_flows.len() <= CHUNK_SIZE {
// We can handle all the children in this work unit.
top_down_flow(&discovered_child_flows,
scope,
&assign_isize_traversal,
&assign_bsize_traversal);
} else {
// Spawn a new work unit for each chunk after the first.
let mut chunks = discovered_child_flows.chunks(CHUNK_SIZE);
let first_chunk = chunks.next();
for chunk in chunks {
let nodes = chunk.iter().cloned().collect::<FlowList>();
scope.spawn(move |scope| {
top_down_flow(&nodes, scope, &assign_isize_traversal, &assign_bsize_traversal);
});
}
if let Some(chunk) = first_chunk {
top_down_flow(chunk, scope, &assign_isize_traversal, &assign_bsize_traversal);
}
}
}

Expand All @@ -183,7 +203,7 @@ pub fn traverse_flow_tree_preorder(

let assign_isize_traversal = &AssignISizes { layout_context: &context };
let assign_bsize_traversal = &AssignBSizes { layout_context: &context };
let nodes = vec![borrowed_flow_to_unsafe_flow(root)].into_boxed_slice();
let nodes = [borrowed_flow_to_unsafe_flow(root)];

queue.install(move || {
rayon::scope(move |scope| {
Expand Down

0 comments on commit 7fd1626

Please sign in to comment.