style: Update StyleSource to use ArcUnion.

Bug: 1455784
Reviewed-by: Manishearth
MozReview-Commit-ID: AT4sud9goGV
This commit is contained in:
Bobby Holley 2018-04-20 16:28:33 -07:00 committed by Emilio Cobos Álvarez
parent cbbefebdba
commit 48558e313a
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
7 changed files with 99 additions and 109 deletions

View file

@ -116,7 +116,7 @@ impl ApplicableDeclarationBlock {
level: CascadeLevel, level: CascadeLevel,
) -> Self { ) -> Self {
ApplicableDeclarationBlock { ApplicableDeclarationBlock {
source: StyleSource::Declarations(declarations), source: StyleSource::from_declarations(declarations),
bits: ApplicableDeclarationBits::new(0, level, 0), bits: ApplicableDeclarationBits::new(0, level, 0),
specificity: 0, specificity: 0,
} }

View file

@ -3424,7 +3424,7 @@ where
let source = node.style_source(); let source = node.style_source();
let declarations = if source.is_some() { let declarations = if source.is_some() {
source.read(cascade_level.guard(guards)).declaration_importance_iter() source.as_ref().unwrap().read(cascade_level.guard(guards)).declaration_importance_iter()
} else { } else {
// The root node has no style source. // The root node has no style source.
DeclarationImportanceIterator::new(&[], &empty) DeclarationImportanceIterator::new(&[], &empty)

View file

@ -8,7 +8,7 @@
use fnv::FnvHashMap; use fnv::FnvHashMap;
use logical_geometry::WritingMode; use logical_geometry::WritingMode;
use properties::{ComputedValues, StyleBuilder}; use properties::{ComputedValues, StyleBuilder};
use rule_tree::{StrongRuleNode, StyleSource}; use rule_tree::StrongRuleNode;
use selector_parser::PseudoElement; use selector_parser::PseudoElement;
use servo_arc::Arc; use servo_arc::Arc;
use shared_lock::StylesheetGuards; use shared_lock::StylesheetGuards;
@ -97,16 +97,18 @@ impl RuleCache {
mut rule_node: Option<&'r StrongRuleNode>, mut rule_node: Option<&'r StrongRuleNode>,
) -> Option<&'r StrongRuleNode> { ) -> Option<&'r StrongRuleNode> {
while let Some(node) = rule_node { while let Some(node) = rule_node {
match *node.style_source() { match node.style_source() {
StyleSource::Declarations(ref decls) => { Some(s) => match s.as_declarations() {
let cascade_level = node.cascade_level(); Some(decls) => {
let decls = decls.read_with(cascade_level.guard(guards)); let cascade_level = node.cascade_level();
if decls.contains_any_reset() { let decls = decls.read_with(cascade_level.guard(guards));
break; if decls.contains_any_reset() {
} break;
}
},
None => break,
}, },
StyleSource::None => {}, None => {},
StyleSource::Style(_) => break,
} }
rule_node = node.parent(); rule_node = node.parent();
} }

View file

@ -12,7 +12,7 @@ use gecko::selector_parser::PseudoElement;
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use properties::{Importance, LonghandIdSet, PropertyDeclarationBlock}; use properties::{Importance, LonghandIdSet, PropertyDeclarationBlock};
use servo_arc::{Arc, ArcBorrow, NonZeroPtrMut}; use servo_arc::{Arc, ArcBorrow, ArcUnion, ArcUnionBorrow, NonZeroPtrMut};
use shared_lock::{Locked, SharedRwLockReadGuard, StylesheetGuards}; use shared_lock::{Locked, SharedRwLockReadGuard, StylesheetGuards};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::io::{self, Write}; use std::io::{self, Write};
@ -89,40 +89,27 @@ impl MallocSizeOf for RuleTree {
/// more debuggability, and also the ability of show those selectors to /// more debuggability, and also the ability of show those selectors to
/// devtools. /// devtools.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum StyleSource { pub struct StyleSource(ArcUnion<Locked<StyleRule>, Locked<PropertyDeclarationBlock>>);
/// A style rule stable pointer.
Style(Arc<Locked<StyleRule>>),
/// A declaration block stable pointer.
Declarations(Arc<Locked<PropertyDeclarationBlock>>),
/// Indicates no style source. Used to save an Option wrapper around the stylesource in
/// RuleNode
None,
}
impl PartialEq for StyleSource { impl PartialEq for StyleSource {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.ptr_equals(other) ArcUnion::ptr_eq(&self.0, &other.0)
} }
} }
impl StyleSource { impl StyleSource {
#[inline] /// Creates a StyleSource from a StyleRule.
fn ptr_equals(&self, other: &Self) -> bool { pub fn from_rule(rule: Arc<Locked<StyleRule>>) -> Self {
use self::StyleSource::*; StyleSource(ArcUnion::from_first(rule))
match (self, other) { }
(&Style(ref one), &Style(ref other)) => Arc::ptr_eq(one, other),
(&Declarations(ref one), &Declarations(ref other)) => Arc::ptr_eq(one, other), /// Creates a StyleSource from a PropertyDeclarationBlock.
(&None, _) | (_, &None) => { pub fn from_declarations(decls: Arc<Locked<PropertyDeclarationBlock>>) -> Self {
panic!("Should not check for equality between null StyleSource objects") StyleSource(ArcUnion::from_second(decls))
},
_ => false,
}
} }
fn dump<W: Write>(&self, guard: &SharedRwLockReadGuard, writer: &mut W) { fn dump<W: Write>(&self, guard: &SharedRwLockReadGuard, writer: &mut W) {
use self::StyleSource::*; if let Some(ref rule) = self.0.as_first() {
if let Style(ref rule) = *self {
let rule = rule.read_with(guard); let rule = rule.read_with(guard);
let _ = write!(writer, "{:?}", rule.selectors); let _ = write!(writer, "{:?}", rule.selectors);
} }
@ -134,20 +121,31 @@ impl StyleSource {
/// underlying property declaration block. /// underlying property declaration block.
#[inline] #[inline]
pub fn read<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a PropertyDeclarationBlock { pub fn read<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a PropertyDeclarationBlock {
let block = match *self { let block: &Locked<PropertyDeclarationBlock> = match self.0.borrow() {
StyleSource::Style(ref rule) => &rule.read_with(guard).block, ArcUnionBorrow::First(ref rule) => &rule.get().read_with(guard).block,
StyleSource::Declarations(ref block) => block, ArcUnionBorrow::Second(ref block) => block.get(),
StyleSource::None => panic!("Cannot call read on StyleSource::None"),
}; };
block.read_with(guard) block.read_with(guard)
} }
/// Indicates if this StyleSource has a value /// Indicates if this StyleSource is a style rule.
pub fn is_some(&self) -> bool { pub fn is_rule(&self) -> bool {
match *self { self.0.is_first()
StyleSource::None => false, }
_ => true,
} /// Indicates if this StyleSource is a PropertyDeclarationBlock.
pub fn is_declarations(&self) -> bool {
self.0.is_second()
}
/// Returns the style rule if applicable, otherwise None.
pub fn as_rule(&self) -> Option<ArcBorrow<Locked<StyleRule>>> {
self.0.as_first()
}
/// Returns the declaration block if applicable, otherwise None.
pub fn as_declarations(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>> {
self.0.as_second()
} }
} }
@ -248,11 +246,12 @@ impl RuleTree {
last_cascade_order = shadow_cascade_order; last_cascade_order = shadow_cascade_order;
important_inner_shadow.push(SmallVec::new()); important_inner_shadow.push(SmallVec::new());
} }
important_inner_shadow.last_mut().unwrap().push(source.clone()) important_inner_shadow
} .last_mut()
SameTreeAuthorNormal => { .unwrap()
important_same_tree.push(source.clone()) .push(source.clone())
}, },
SameTreeAuthorNormal => important_same_tree.push(source.clone()),
UANormal => important_ua.push(source.clone()), UANormal => important_ua.push(source.clone()),
UserNormal => important_user.push(source.clone()), UserNormal => important_user.push(source.clone()),
StyleAttributeNormal => { StyleAttributeNormal => {
@ -391,7 +390,10 @@ impl RuleTree {
// First walk up until the first less-or-equally specific rule. // First walk up until the first less-or-equally specific rule.
let mut children = SmallVec::<[_; 10]>::new(); let mut children = SmallVec::<[_; 10]>::new();
while current.get().level > level { while current.get().level > level {
children.push((current.get().source.clone(), current.get().level)); children.push((
current.get().source.as_ref().unwrap().clone(),
current.get().level,
));
current = current.parent().unwrap().clone(); current = current.parent().unwrap().clone();
} }
@ -418,13 +420,14 @@ impl RuleTree {
// also equally valid. This is less likely, and would require an // also equally valid. This is less likely, and would require an
// in-place mutation of the source, which is, at best, fiddly, // in-place mutation of the source, which is, at best, fiddly,
// so let's skip it for now. // so let's skip it for now.
let is_here_already = match current.get().source { let current_decls = current
StyleSource::Declarations(ref already_here) => { .get()
pdb.with_arc(|arc| Arc::ptr_eq(arc, already_here)) .source
}, .as_ref()
_ => unreachable!("Replacing non-declarations style?"), .unwrap()
}; .as_declarations()
.expect("Replacing non-declarations style?");
let is_here_already = ArcBorrow::ptr_eq(&pdb, &current_decls);
if is_here_already { if is_here_already {
debug!("Picking the fast path in rule replacement"); debug!("Picking the fast path in rule replacement");
return None; return None;
@ -447,7 +450,7 @@ impl RuleTree {
if pdb.read_with(level.guard(guards)).any_important() { if pdb.read_with(level.guard(guards)).any_important() {
current = current.ensure_child( current = current.ensure_child(
self.root.downgrade(), self.root.downgrade(),
StyleSource::Declarations(pdb.clone_arc()), StyleSource::from_declarations(pdb.clone_arc()),
level, level,
); );
} }
@ -455,7 +458,7 @@ impl RuleTree {
if pdb.read_with(level.guard(guards)).any_normal() { if pdb.read_with(level.guard(guards)).any_normal() {
current = current.ensure_child( current = current.ensure_child(
self.root.downgrade(), self.root.downgrade(),
StyleSource::Declarations(pdb.clone_arc()), StyleSource::from_declarations(pdb.clone_arc()),
level, level,
); );
} }
@ -491,7 +494,10 @@ impl RuleTree {
let mut children = SmallVec::<[_; 10]>::new(); let mut children = SmallVec::<[_; 10]>::new();
for node in iter { for node in iter {
if !node.cascade_level().is_animation() { if !node.cascade_level().is_animation() {
children.push((node.get().source.clone(), node.cascade_level())); children.push((
node.get().source.as_ref().unwrap().clone(),
node.cascade_level(),
));
} }
last = node; last = node;
} }
@ -689,7 +695,9 @@ pub struct RuleNode {
/// The actual style source, either coming from a selector in a StyleRule, /// The actual style source, either coming from a selector in a StyleRule,
/// or a raw property declaration block (like the style attribute). /// or a raw property declaration block (like the style attribute).
source: StyleSource, ///
/// None for the root node.
source: Option<StyleSource>,
/// The cascade level this rule is positioned at. /// The cascade level this rule is positioned at.
level: CascadeLevel, level: CascadeLevel,
@ -775,7 +783,7 @@ impl RuleNode {
RuleNode { RuleNode {
root: Some(root), root: Some(root),
parent: Some(parent), parent: Some(parent),
source: source, source: Some(source),
level: level, level: level,
refcount: AtomicUsize::new(1), refcount: AtomicUsize::new(1),
first_child: AtomicPtr::new(ptr::null_mut()), first_child: AtomicPtr::new(ptr::null_mut()),
@ -789,7 +797,7 @@ impl RuleNode {
RuleNode { RuleNode {
root: None, root: None,
parent: None, parent: None,
source: StyleSource::None, source: None,
level: CascadeLevel::UANormal, level: CascadeLevel::UANormal,
refcount: AtomicUsize::new(1), refcount: AtomicUsize::new(1),
first_child: AtomicPtr::new(ptr::null_mut()), first_child: AtomicPtr::new(ptr::null_mut()),
@ -884,7 +892,10 @@ impl RuleNode {
} }
if self.source.is_some() { if self.source.is_some() {
self.source.dump(self.level.guard(guards), writer); self.source
.as_ref()
.unwrap()
.dump(self.level.guard(guards), writer);
} else { } else {
if indent != 0 { if indent != 0 {
warn!("How has this happened?"); warn!("How has this happened?");
@ -986,7 +997,7 @@ impl StrongRuleNode {
// WeakRuleNode, and implementing this on WeakRuleNode itself... // WeakRuleNode, and implementing this on WeakRuleNode itself...
for child in self.get().iter_children() { for child in self.get().iter_children() {
let child_node = unsafe { &*child.ptr() }; let child_node = unsafe { &*child.ptr() };
if child_node.level == level && child_node.source.ptr_equals(&source) { if child_node.level == level && child_node.source.as_ref().unwrap() == &source {
return child.upgrade(); return child.upgrade();
} }
last = Some(child); last = Some(child);
@ -1026,7 +1037,7 @@ impl StrongRuleNode {
// we accessed `last`. // we accessed `last`.
next = WeakRuleNode::from_ptr(existing); next = WeakRuleNode::from_ptr(existing);
if unsafe { &*next.ptr() }.source.ptr_equals(&source) { if unsafe { &*next.ptr() }.source.as_ref().unwrap() == &source {
// That node happens to be for the same style source, use // That node happens to be for the same style source, use
// that, and let node fall out of scope. // that, and let node fall out of scope.
return next.upgrade(); return next.upgrade();
@ -1054,8 +1065,8 @@ impl StrongRuleNode {
/// Get the style source corresponding to this rule node. May return `None` /// Get the style source corresponding to this rule node. May return `None`
/// if it's the root node, which means that the node hasn't matched any /// if it's the root node, which means that the node hasn't matched any
/// rules. /// rules.
pub fn style_source(&self) -> &StyleSource { pub fn style_source(&self) -> Option<&StyleSource> {
&self.get().source self.get().source.as_ref()
} }
/// The cascade level for this node /// The cascade level for this node
@ -1317,6 +1328,8 @@ impl StrongRuleNode {
let source = node.style_source(); let source = node.style_source();
let declarations = if source.is_some() { let declarations = if source.is_some() {
source source
.as_ref()
.unwrap()
.read(node.cascade_level().guard(guards)) .read(node.cascade_level().guard(guards))
.declaration_importance_iter() .declaration_importance_iter()
} else { } else {
@ -1444,7 +1457,7 @@ impl StrongRuleNode {
.take_while(|node| node.cascade_level() > CascadeLevel::Animations); .take_while(|node| node.cascade_level() > CascadeLevel::Animations);
let mut result = (LonghandIdSet::new(), false); let mut result = (LonghandIdSet::new(), false);
for node in iter { for node in iter {
let style = node.style_source(); let style = node.style_source().unwrap();
for (decl, important) in style for (decl, important) in style
.read(node.cascade_level().guard(guards)) .read(node.cascade_level().guard(guards))
.declaration_importance_iter() .declaration_importance_iter()
@ -1464,33 +1477,6 @@ impl StrongRuleNode {
} }
result result
} }
/// Returns PropertyDeclarationBlock for this node.
/// This function must be called only for animation level node.
fn get_animation_style(&self) -> &Arc<Locked<PropertyDeclarationBlock>> {
debug_assert!(
self.cascade_level().is_animation(),
"The cascade level should be an animation level"
);
match *self.style_source() {
StyleSource::Declarations(ref block) => block,
StyleSource::Style(_) => unreachable!("animating style should not be a style rule"),
StyleSource::None => unreachable!("animating style should not be none"),
}
}
/// Returns SMIL override declaration block if exists.
pub fn get_smil_animation_rule(&self) -> Option<&Arc<Locked<PropertyDeclarationBlock>>> {
if cfg!(feature = "servo") {
// Servo has no knowledge of a SMIL rule, so just avoid looking for it.
return None;
}
self.self_and_ancestors()
.take_while(|node| node.cascade_level() >= CascadeLevel::SMILOverride)
.find(|node| node.cascade_level() == CascadeLevel::SMILOverride)
.map(|node| node.get_animation_style())
}
} }
/// An iterator over a rule node and its ancestors. /// An iterator over a rule node and its ancestors.

View file

@ -1523,7 +1523,7 @@ impl Stylist {
// just avoid allocating it and calling `apply_declarations` directly, // just avoid allocating it and calling `apply_declarations` directly,
// maybe... // maybe...
let rule_node = self.rule_tree.insert_ordered_rules(iter::once(( let rule_node = self.rule_tree.insert_ordered_rules(iter::once((
StyleSource::Declarations(declarations), StyleSource::from_declarations(declarations),
CascadeLevel::StyleAttributeNormal, CascadeLevel::StyleAttributeNormal,
))); )));
@ -2177,7 +2177,7 @@ impl CascadeData {
.expect("Expected precomputed declarations for the UA level") .expect("Expected precomputed declarations for the UA level")
.get_or_insert_with(&pseudo.canonical(), Vec::new) .get_or_insert_with(&pseudo.canonical(), Vec::new)
.push(ApplicableDeclarationBlock::new( .push(ApplicableDeclarationBlock::new(
StyleSource::Style(locked.clone()), StyleSource::from_rule(locked.clone()),
self.rules_source_order, self.rules_source_order,
CascadeLevel::UANormal, CascadeLevel::UANormal,
selector.specificity(), selector.specificity(),
@ -2480,7 +2480,7 @@ impl Rule {
level: CascadeLevel, level: CascadeLevel,
shadow_cascade_order: ShadowCascadeOrder, shadow_cascade_order: ShadowCascadeOrder,
) -> ApplicableDeclarationBlock { ) -> ApplicableDeclarationBlock {
let source = StyleSource::Style(self.style_rule.clone()); let source = StyleSource::from_rule(self.style_rule.clone());
ApplicableDeclarationBlock::new(source, self.source_order, level, self.specificity(), shadow_cascade_order) ApplicableDeclarationBlock::new(source, self.source_order, level, self.specificity(), shadow_cascade_order)
} }

View file

@ -137,7 +137,7 @@ use style::properties::{parse_one_declaration_into, parse_style_attribute};
use style::properties::animated_properties::AnimationValue; use style::properties::animated_properties::AnimationValue;
use style::properties::animated_properties::compare_property_priority; use style::properties::animated_properties::compare_property_priority;
use style::rule_cache::RuleCacheConditions; use style::rule_cache::RuleCacheConditions;
use style::rule_tree::{CascadeLevel, StrongRuleNode, StyleSource}; use style::rule_tree::{CascadeLevel, StrongRuleNode};
use style::selector_parser::{PseudoElementCascadeType, SelectorImpl}; use style::selector_parser::{PseudoElementCascadeType, SelectorImpl};
use style::shared_lock::{SharedRwLockReadGuard, StylesheetGuards, ToCssWithGuard, Locked}; use style::shared_lock::{SharedRwLockReadGuard, StylesheetGuards, ToCssWithGuard, Locked};
use style::string_cache::{Atom, WeakAtom}; use style::string_cache::{Atom, WeakAtom};
@ -3123,8 +3123,8 @@ pub extern "C" fn Servo_ComputedValues_GetStyleRuleList(
let mut result = SmallVec::<[_; 10]>::new(); let mut result = SmallVec::<[_; 10]>::new();
for node in rule_node.self_and_ancestors() { for node in rule_node.self_and_ancestors() {
let style_rule = match *node.style_source() { let style_rule = match node.style_source().and_then(|x| x.as_rule()) {
StyleSource::Style(ref rule) => rule, Some(rule) => rule,
_ => continue, _ => continue,
}; };
@ -3141,9 +3141,11 @@ pub extern "C" fn Servo_ComputedValues_GetStyleRuleList(
unsafe { rules.set_len(result.len() as u32) }; unsafe { rules.set_len(result.len() as u32) };
for (ref src, ref mut dest) in result.into_iter().zip(rules.iter_mut()) { for (ref src, ref mut dest) in result.into_iter().zip(rules.iter_mut()) {
src.with_raw_offset_arc(|arc| { src.with_arc(|a| {
**dest = *Locked::<StyleRule>::arc_as_borrowed(arc); a.with_raw_offset_arc(|arc| {
}) **dest = *Locked::<StyleRule>::arc_as_borrowed(arc);
})
});
} }
} }

View file

@ -35,8 +35,8 @@ size_of_test!(test_size_of_element_data, ElementData, 24);
size_of_test!(test_size_of_property_declaration, style::properties::PropertyDeclaration, 32); size_of_test!(test_size_of_property_declaration, style::properties::PropertyDeclaration, 32);
size_of_test!(test_size_of_application_declaration_block, ApplicableDeclarationBlock, 24); size_of_test!(test_size_of_application_declaration_block, ApplicableDeclarationBlock, 16);
size_of_test!(test_size_of_rule_node, RuleNode, 80); size_of_test!(test_size_of_rule_node, RuleNode, 72);
// This is huge, but we allocate it on the stack and then never move it, // This is huge, but we allocate it on the stack and then never move it,
// we only pass `&mut SourcePropertyDeclaration` references around. // we only pass `&mut SourcePropertyDeclaration` references around.