Refactor parallel dom traversal to be agnostic to the processing steps themselves.

This commit is contained in:
Bobby Holley 2015-12-30 17:02:38 -08:00
parent 5ad9207a99
commit 947134949a
4 changed files with 262 additions and 327 deletions

View file

@ -118,17 +118,30 @@ fn insert_ancestors_into_bloom_filter<'ln, N>(bf: &mut Box<BloomFilter>,
debug!("[{}] Inserted {} ancestors.", tid(), ancestors);
}
/// A top-down traversal.
pub trait PreorderDomTraversal<'ln, ConcreteLayoutNode: LayoutNode<'ln>> {
/// The operation to perform. Return true to continue or false to stop.
fn process(&self, node: ConcreteLayoutNode);
#[derive(Copy, Clone)]
pub struct DomTraversalContext<'a> {
pub layout_context: &'a LayoutContext<'a>,
pub root: OpaqueNode,
}
/// A bottom-up traversal, with a optional in-order pass.
pub trait PostorderDomTraversal<'ln, ConcreteLayoutNode: LayoutNode<'ln>> {
/// The operation to perform. Return true to continue or false to stop.
fn process(&self, node: ConcreteLayoutNode);
pub trait DomTraversal<'ln, N: LayoutNode<'ln>> {
fn process_preorder<'a>(context: &'a DomTraversalContext<'a>, node: N);
fn process_postorder<'a>(context: &'a DomTraversalContext<'a>, node: N);
}
/// FIXME(bholley): I added this now to demonstrate the usefulness of the new design.
/// This is currently unused, but will be used shortly.
#[allow(dead_code)]
pub struct RecalcStyleOnly;
impl<'ln, N: LayoutNode<'ln>> DomTraversal<'ln, N> for RecalcStyleOnly {
fn process_preorder<'a>(context: &'a DomTraversalContext<'a>, node: N) { recalc_style_at(context, node); }
fn process_postorder<'a>(_: &'a DomTraversalContext<'a>, _: N) {}
}
pub struct RecalcStyleAndConstructFlows;
impl<'ln, N: LayoutNode<'ln>> DomTraversal<'ln, N> for RecalcStyleAndConstructFlows {
fn process_preorder<'a>(context: &'a DomTraversalContext<'a>, node: N) { recalc_style_at(context, node); }
fn process_postorder<'a>(context: &'a DomTraversalContext<'a>, node: N) { construct_flows_at(context, node); }
}
/// A bottom-up, parallelizable traversal.
@ -139,176 +152,156 @@ pub trait PostorderNodeMutTraversal<'ln, ConcreteThreadSafeLayoutNode: ThreadSaf
/// The recalc-style-for-node traversal, which styles each node and must run before
/// layout computation. This computes the styles applied to each node.
#[derive(Copy, Clone)]
pub struct RecalcStyleForNode<'a> {
pub layout_context: &'a LayoutContext<'a>,
pub root: OpaqueNode,
}
#[inline]
#[allow(unsafe_code)]
fn recalc_style_at<'a, 'ln, N: LayoutNode<'ln>> (context: &'a DomTraversalContext<'a>, node: N) {
// Initialize layout data.
//
// FIXME(pcwalton): Stop allocating here. Ideally this should just be done by the HTML
// parser.
node.initialize_data();
impl<'a, 'ln, ConcreteLayoutNode> PreorderDomTraversal<'ln, ConcreteLayoutNode>
for RecalcStyleForNode<'a>
where ConcreteLayoutNode: LayoutNode<'ln> {
#[inline]
#[allow(unsafe_code)]
fn process(&self, node: ConcreteLayoutNode) {
// Initialize layout data.
//
// FIXME(pcwalton): Stop allocating here. Ideally this should just be done by the HTML
// parser.
node.initialize_data();
// Get the parent node.
let parent_opt = node.layout_parent_node(context.root);
// Get the parent node.
let parent_opt = node.layout_parent_node(self.root);
// Get the style bloom filter.
let mut bf = take_task_local_bloom_filter(parent_opt, context.root, context.layout_context);
// Get the style bloom filter.
let mut bf = take_task_local_bloom_filter(parent_opt, self.root, self.layout_context);
let nonincremental_layout = opts::get().nonincremental_layout;
if nonincremental_layout || node.is_dirty() {
// Remove existing CSS styles from nodes whose content has changed (e.g. text changed),
// to force non-incremental reflow.
if node.has_changed() {
let node = node.to_threadsafe();
node.unstyle();
}
// Check to see whether we can share a style with someone.
let style_sharing_candidate_cache =
&mut self.layout_context.style_sharing_candidate_cache();
let sharing_result = match node.as_element() {
Some(element) => {
unsafe {
element.share_style_if_possible(style_sharing_candidate_cache,
parent_opt.clone())
}
},
None => StyleSharingResult::CannotShare,
};
// Otherwise, match and cascade selectors.
match sharing_result {
StyleSharingResult::CannotShare => {
let mut applicable_declarations = ApplicableDeclarations::new();
let shareable_element = match node.as_element() {
Some(element) => {
// Perform the CSS selector matching.
let stylist = unsafe { &*self.layout_context.shared_context().stylist.0 };
if element.match_element(stylist,
Some(&*bf),
&mut applicable_declarations) {
Some(element)
} else {
None
}
},
None => {
if node.has_changed() {
node.to_threadsafe().set_restyle_damage(
incremental::rebuild_and_reflow())
}
None
},
};
// Perform the CSS cascade.
unsafe {
node.cascade_node(self.layout_context.shared,
parent_opt,
&applicable_declarations,
&mut self.layout_context.applicable_declarations_cache(),
&self.layout_context.shared_context().new_animations_sender);
}
// Add ourselves to the LRU cache.
if let Some(element) = shareable_element {
style_sharing_candidate_cache.insert_if_possible(&element);
}
}
StyleSharingResult::StyleWasShared(index, damage) => {
style_sharing_candidate_cache.touch(index);
node.to_threadsafe().set_restyle_damage(damage);
}
}
let nonincremental_layout = opts::get().nonincremental_layout;
if nonincremental_layout || node.is_dirty() {
// Remove existing CSS styles from nodes whose content has changed (e.g. text changed),
// to force non-incremental reflow.
if node.has_changed() {
let node = node.to_threadsafe();
node.unstyle();
}
let unsafe_layout_node = node.to_unsafe();
// Check to see whether we can share a style with someone.
let style_sharing_candidate_cache =
&mut context.layout_context.style_sharing_candidate_cache();
// Before running the children, we need to insert our nodes into the bloom
// filter.
debug!("[{}] + {:X}", tid(), unsafe_layout_node.0);
node.insert_into_bloom_filter(&mut *bf);
let sharing_result = match node.as_element() {
Some(element) => {
unsafe {
element.share_style_if_possible(style_sharing_candidate_cache,
parent_opt.clone())
}
},
None => StyleSharingResult::CannotShare,
};
// NB: flow construction updates the bloom filter on the way up.
put_task_local_bloom_filter(bf, &unsafe_layout_node, self.layout_context);
// Otherwise, match and cascade selectors.
match sharing_result {
StyleSharingResult::CannotShare => {
let mut applicable_declarations = ApplicableDeclarations::new();
let shareable_element = match node.as_element() {
Some(element) => {
// Perform the CSS selector matching.
let stylist = unsafe { &*context.layout_context.shared_context().stylist.0 };
if element.match_element(stylist,
Some(&*bf),
&mut applicable_declarations) {
Some(element)
} else {
None
}
},
None => {
if node.has_changed() {
node.to_threadsafe().set_restyle_damage(
incremental::rebuild_and_reflow())
}
None
},
};
// Perform the CSS cascade.
unsafe {
node.cascade_node(context.layout_context.shared,
parent_opt,
&applicable_declarations,
&mut context.layout_context.applicable_declarations_cache(),
&context.layout_context.shared_context().new_animations_sender);
}
// Add ourselves to the LRU cache.
if let Some(element) = shareable_element {
style_sharing_candidate_cache.insert_if_possible(&element);
}
}
StyleSharingResult::StyleWasShared(index, damage) => {
style_sharing_candidate_cache.touch(index);
node.to_threadsafe().set_restyle_damage(damage);
}
}
}
let unsafe_layout_node = node.to_unsafe();
// Before running the children, we need to insert our nodes into the bloom
// filter.
debug!("[{}] + {:X}", tid(), unsafe_layout_node.0);
node.insert_into_bloom_filter(&mut *bf);
// NB: flow construction updates the bloom filter on the way up.
put_task_local_bloom_filter(bf, &unsafe_layout_node, context.layout_context);
}
/// The flow construction traversal, which builds flows for styled nodes.
#[derive(Copy, Clone)]
pub struct ConstructFlows<'a> {
pub layout_context: &'a LayoutContext<'a>,
pub root: OpaqueNode,
}
#[inline]
#[allow(unsafe_code)]
fn construct_flows_at<'a, 'ln, N: LayoutNode<'ln>>(context: &'a DomTraversalContext<'a>, node: N) {
// Construct flows for this node.
{
let tnode = node.to_threadsafe();
impl<'a, 'ln, ConcreteLayoutNode> PostorderDomTraversal<'ln, ConcreteLayoutNode>
for ConstructFlows<'a>
where ConcreteLayoutNode: LayoutNode<'ln> {
#[inline]
#[allow(unsafe_code)]
fn process(&self, node: ConcreteLayoutNode) {
// Construct flows for this node.
{
let tnode = node.to_threadsafe();
// Always reconstruct if incremental layout is turned off.
let nonincremental_layout = opts::get().nonincremental_layout;
if nonincremental_layout || node.has_dirty_descendants() {
let mut flow_constructor = FlowConstructor::new(self.layout_context);
if nonincremental_layout || !flow_constructor.repair_if_possible(&tnode) {
flow_constructor.process(&tnode);
debug!("Constructed flow for {:x}: {:x}",
tnode.debug_id(),
tnode.flow_debug_id());
}
// Always reconstruct if incremental layout is turned off.
let nonincremental_layout = opts::get().nonincremental_layout;
if nonincremental_layout || node.has_dirty_descendants() {
let mut flow_constructor = FlowConstructor::new(context.layout_context);
if nonincremental_layout || !flow_constructor.repair_if_possible(&tnode) {
flow_constructor.process(&tnode);
debug!("Constructed flow for {:x}: {:x}",
tnode.debug_id(),
tnode.flow_debug_id());
}
// Reset the layout damage in this node. It's been propagated to the
// flow by the flow constructor.
tnode.set_restyle_damage(RestyleDamage::empty());
}
unsafe {
node.set_changed(false);
node.set_dirty(false);
node.set_dirty_descendants(false);
}
let unsafe_layout_node = node.to_unsafe();
let (mut bf, old_node, old_generation) =
STYLE_BLOOM.with(|style_bloom| {
mem::replace(&mut *style_bloom.borrow_mut(), None)
.expect("The bloom filter should have been set by style recalc.")
});
assert_eq!(old_node, unsafe_layout_node);
assert_eq!(old_generation, self.layout_context.shared_context().generation);
match node.layout_parent_node(self.root) {
None => {
debug!("[{}] - {:X}, and deleting BF.", tid(), unsafe_layout_node.0);
// If this is the reflow root, eat the task-local bloom filter.
}
Some(parent) => {
// Otherwise, put it back, but remove this node.
node.remove_from_bloom_filter(&mut *bf);
let unsafe_parent = parent.to_unsafe();
put_task_local_bloom_filter(bf, &unsafe_parent, self.layout_context);
},
};
// Reset the layout damage in this node. It's been propagated to the
// flow by the flow constructor.
tnode.set_restyle_damage(RestyleDamage::empty());
}
unsafe {
node.set_changed(false);
node.set_dirty(false);
node.set_dirty_descendants(false);
}
let unsafe_layout_node = node.to_unsafe();
let (mut bf, old_node, old_generation) =
STYLE_BLOOM.with(|style_bloom| {
mem::replace(&mut *style_bloom.borrow_mut(), None)
.expect("The bloom filter should have been set by style recalc.")
});
assert_eq!(old_node, unsafe_layout_node);
assert_eq!(old_generation, context.layout_context.shared_context().generation);
match node.layout_parent_node(context.root) {
None => {
debug!("[{}] - {:X}, and deleting BF.", tid(), unsafe_layout_node.0);
// If this is the reflow root, eat the task-local bloom filter.
}
Some(parent) => {
// Otherwise, put it back, but remove this node.
node.remove_from_bloom_filter(&mut *bf);
let unsafe_parent = parent.to_unsafe();
put_task_local_bloom_filter(bf, &unsafe_parent, context.layout_context);
},
};
}
/// The bubble-inline-sizes traversal, the first part of layout computation. This computes