Define share_style_if_possible on LayoutElement.

This commit is contained in:
Ms2ger 2015-10-15 13:55:02 +02:00
parent e6e514c89a
commit f3faaa6b01
2 changed files with 26 additions and 23 deletions

View file

@ -377,6 +377,15 @@ pub trait ElementMatchMethods {
parent_bf: Option<&BloomFilter>, parent_bf: Option<&BloomFilter>,
applicable_declarations: &mut ApplicableDeclarations) applicable_declarations: &mut ApplicableDeclarations)
-> bool; -> bool;
/// Attempts to share a style with another node. This method is unsafe because it depends on
/// the `style_sharing_candidate_cache` having only live nodes in it, and we have no way to
/// guarantee that at the type system level yet.
unsafe fn share_style_if_possible(&self,
style_sharing_candidate_cache:
&mut StyleSharingCandidateCache,
parent: Option<LayoutNode>)
-> StyleSharingResult;
} }
pub trait MatchMethods { pub trait MatchMethods {
@ -393,15 +402,6 @@ pub trait MatchMethods {
/// called to reset the bloom filter after an `insert`. /// called to reset the bloom filter after an `insert`.
fn remove_from_bloom_filter(&self, bf: &mut BloomFilter); fn remove_from_bloom_filter(&self, bf: &mut BloomFilter);
/// Attempts to share a style with another node. This method is unsafe because it depends on
/// the `style_sharing_candidate_cache` having only live nodes in it, and we have no way to
/// guarantee that at the type system level yet.
unsafe fn share_style_if_possible(&self,
style_sharing_candidate_cache:
&mut StyleSharingCandidateCache,
parent: Option<LayoutNode>)
-> StyleSharingResult;
unsafe fn cascade_node(&self, unsafe fn cascade_node(&self,
layout_context: &SharedLayoutContext, layout_context: &SharedLayoutContext,
parent: Option<LayoutNode>, parent: Option<LayoutNode>,
@ -574,9 +574,7 @@ impl<'ln> ElementMatchMethods for LayoutElement<'ln> {
applicable_declarations.before.is_empty() && applicable_declarations.before.is_empty() &&
applicable_declarations.after.is_empty() applicable_declarations.after.is_empty()
} }
}
impl<'ln> MatchMethods for LayoutNode<'ln> {
unsafe fn share_style_if_possible(&self, unsafe fn share_style_if_possible(&self,
style_sharing_candidate_cache: style_sharing_candidate_cache:
&mut StyleSharingCandidateCache, &mut StyleSharingCandidateCache,
@ -586,23 +584,19 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
return StyleSharingResult::CannotShare(false) return StyleSharingResult::CannotShare(false)
} }
let element = match self.as_element() { if self.style_attribute().is_some() {
Some(element) => element,
None => return StyleSharingResult::CannotShare(false),
};
if element.style_attribute().is_some() {
return StyleSharingResult::CannotShare(false) return StyleSharingResult::CannotShare(false)
} }
if element.get_attr(&ns!(""), &atom!("id")).is_some() { if self.get_attr(&ns!(""), &atom!("id")).is_some() {
return StyleSharingResult::CannotShare(false) return StyleSharingResult::CannotShare(false)
} }
for (i, &(ref candidate, ())) in style_sharing_candidate_cache.iter().enumerate() { for (i, &(ref candidate, ())) in style_sharing_candidate_cache.iter().enumerate() {
match element.share_style_with_candidate_if_possible(parent.clone(), candidate) { match self.share_style_with_candidate_if_possible(parent.clone(), candidate) {
Some(shared_style) => { Some(shared_style) => {
// Yay, cache hit. Share the style. // Yay, cache hit. Share the style.
let mut layout_data_ref = self.mutate_layout_data(); let node = self.as_node();
let mut layout_data_ref = node.mutate_layout_data();
let shared_data = &mut layout_data_ref.as_mut().unwrap().shared_data; let shared_data = &mut layout_data_ref.as_mut().unwrap().shared_data;
let style = &mut shared_data.style; let style = &mut shared_data.style;
let damage = incremental::compute_damage(style, &*shared_style); let damage = incremental::compute_damage(style, &*shared_style);
@ -615,7 +609,9 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
StyleSharingResult::CannotShare(true) StyleSharingResult::CannotShare(true)
} }
}
impl<'ln> MatchMethods for LayoutNode<'ln> {
// The below two functions are copy+paste because I can't figure out how to // The below two functions are copy+paste because I can't figure out how to
// write a function which takes a generic function. I don't think it can // write a function which takes a generic function. I don't think it can
// be done. // be done.

View file

@ -165,10 +165,17 @@ impl<'a> PreorderDomTraversal for RecalcStyleForNode<'a> {
// Check to see whether we can share a style with someone. // Check to see whether we can share a style with someone.
let style_sharing_candidate_cache = let style_sharing_candidate_cache =
&mut self.layout_context.style_sharing_candidate_cache(); &mut self.layout_context.style_sharing_candidate_cache();
let sharing_result = unsafe {
node.share_style_if_possible(style_sharing_candidate_cache, let sharing_result = match node.as_element() {
parent_opt.clone()) Some(element) => {
unsafe {
element.share_style_if_possible(style_sharing_candidate_cache,
parent_opt.clone())
}
},
None => StyleSharingResult::CannotShare(false),
}; };
// Otherwise, match and cascade selectors. // Otherwise, match and cascade selectors.
match sharing_result { match sharing_result {
StyleSharingResult::CannotShare(mut shareable) => { StyleSharingResult::CannotShare(mut shareable) => {