Make the DomTraversalContext own the SharedStyleContext and share it immutably across the traversal.

This allows us to get rid of a bunch of lifetimes and simplify a lot of code. It
also lets us get rid of that nasty lifetime transmute, which is awesome.

The situation with thread-local contexts is still suboptimal, but we fix that in
subsequent patches.
This commit is contained in:
Bobby Holley 2016-12-15 16:00:40 -08:00
parent 1b2daae453
commit 648ce1e44e
18 changed files with 329 additions and 323 deletions

View file

@ -9,19 +9,20 @@
use dom::{OpaqueNode, TElement, TNode, UnsafeNode};
use rayon;
use servo_config::opts;
use std::borrow::Borrow;
use std::sync::atomic::Ordering;
use traversal::{DomTraversalContext, PerLevelTraversalData, PreTraverseToken};
use traversal::{DomTraversal, PerLevelTraversalData, PreTraverseToken};
use traversal::{STYLE_SHARING_CACHE_HITS, STYLE_SHARING_CACHE_MISSES};
pub const CHUNK_SIZE: usize = 64;
pub fn traverse_dom<N, C>(root: N::ConcreteElement,
pub fn traverse_dom<N, D>(traversal: &D,
root: N::ConcreteElement,
known_root_dom_depth: Option<usize>,
shared_context: &C::SharedContext,
token: PreTraverseToken,
queue: &rayon::ThreadPool)
where N: TNode,
C: DomTraversalContext<N>
D: DomTraversal<N>
{
if opts::get().style_sharing_stats {
STYLE_SHARING_CACHE_HITS.store(0, Ordering::SeqCst);
@ -32,7 +33,7 @@ pub fn traverse_dom<N, C>(root: N::ConcreteElement,
// in conjunction with bottom-up traversal. If we did, we'd need to put
// it on the context to make it available to the bottom-up phase.
let (nodes, depth) = if token.traverse_unstyled_children_only() {
debug_assert!(!C::needs_postorder_traversal());
debug_assert!(!D::needs_postorder_traversal());
let mut children = vec![];
for kid in root.as_node().children() {
if kid.as_element().map_or(false, |el| el.get_data().is_none()) {
@ -51,7 +52,7 @@ pub fn traverse_dom<N, C>(root: N::ConcreteElement,
let root = root.as_node().opaque();
queue.install(|| {
rayon::scope(|scope| {
traverse_nodes::<_, C>(nodes, root, data, scope, shared_context);
traverse_nodes(nodes, root, data, scope, traversal);
});
});
@ -68,16 +69,14 @@ pub fn traverse_dom<N, C>(root: N::ConcreteElement,
/// A parallel top-down DOM traversal.
#[inline(always)]
#[allow(unsafe_code)]
fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
fn top_down_dom<'a, 'scope, N, D>(unsafe_nodes: &'a [UnsafeNode],
root: OpaqueNode,
mut data: PerLevelTraversalData,
scope: &'a rayon::Scope<'scope>,
shared_context: &'scope C::SharedContext)
traversal: &'scope D)
where N: TNode,
C: DomTraversalContext<N>,
D: DomTraversal<N>,
{
let context = C::new(shared_context, root);
let mut discovered_child_nodes = vec![];
for unsafe_node in unsafe_nodes {
// Get a real layout node.
@ -85,9 +84,9 @@ fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
// Perform the appropriate traversal.
let mut children_to_process = 0isize;
context.process_preorder(node, &mut data);
traversal.process_preorder(node, &mut data);
if let Some(el) = node.as_element() {
C::traverse_children(el, |kid| {
D::traverse_children(el, |kid| {
children_to_process += 1;
discovered_child_nodes.push(kid.to_unsafe())
});
@ -95,10 +94,10 @@ fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
// Reset the count of children if we need to do a bottom-up traversal
// after the top up.
if C::needs_postorder_traversal() {
if D::needs_postorder_traversal() {
if children_to_process == 0 {
// If there were no more children, start walking back up.
bottom_up_dom::<N, C>(root, *unsafe_node, shared_context)
bottom_up_dom(root, *unsafe_node, traversal)
} else {
// Otherwise record the number of children to process when the
// time comes.
@ -109,21 +108,22 @@ fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
// NB: In parallel traversal mode we have to purge the LRU cache in order to
// be able to access it without races.
context.local_context().style_sharing_candidate_cache.borrow_mut().clear();
let tlc = traversal.create_or_get_thread_local_context();
(*tlc).borrow().style_sharing_candidate_cache.borrow_mut().clear();
if let Some(ref mut depth) = data.current_dom_depth {
*depth += 1;
}
traverse_nodes::<_, C>(discovered_child_nodes, root, data, scope, shared_context);
traverse_nodes(discovered_child_nodes, root, data, scope, traversal);
}
fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
fn traverse_nodes<'a, 'scope, N, D>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
data: PerLevelTraversalData,
scope: &'a rayon::Scope<'scope>,
shared_context: &'scope C::SharedContext)
traversal: &'scope D)
where N: TNode,
C: DomTraversalContext<N>,
D: DomTraversal<N>,
{
if nodes.is_empty() {
return;
@ -133,7 +133,7 @@ fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
// we're only pushing one work unit.
if nodes.len() <= CHUNK_SIZE {
let nodes = nodes.into_boxed_slice();
top_down_dom::<N, C>(&nodes, root, data, scope, shared_context);
top_down_dom(&nodes, root, data, scope, traversal);
return;
}
@ -143,7 +143,7 @@ fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
let data = data.clone();
scope.spawn(move |scope| {
let nodes = nodes;
top_down_dom::<N, C>(&nodes, root, data, scope, shared_context)
top_down_dom(&nodes, root, data, scope, traversal)
})
}
}
@ -160,19 +160,17 @@ fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
/// The only communication between siblings is that they both
/// fetch-and-subtract the parent's children count.
#[allow(unsafe_code)]
fn bottom_up_dom<N, C>(root: OpaqueNode,
fn bottom_up_dom<N, D>(root: OpaqueNode,
unsafe_node: UnsafeNode,
shared_context: &C::SharedContext)
traversal: &D)
where N: TNode,
C: DomTraversalContext<N>
D: DomTraversal<N>
{
let context = C::new(shared_context, root);
// Get a real layout node.
let mut node = unsafe { N::from_unsafe(&unsafe_node) };
loop {
// Perform the appropriate operation.
context.process_postorder(node);
traversal.process_postorder(node);
if node.opaque() == root {
break;