style: Store ::slotted rules separately on the cascade data, since they may cross the shadow boundary.

MozReview-Commit-ID: EY9nK3169vv
This commit is contained in:
Emilio Cobos Álvarez 2017-12-18 09:08:24 +01:00
parent b26f3280d2
commit 5115cbd1c0
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
4 changed files with 161 additions and 51 deletions

View file

@ -98,13 +98,21 @@ impl<Impl: SelectorImpl> SelectorBuilder<Impl> {
/// Consumes the builder, producing a Selector. /// Consumes the builder, producing a Selector.
#[inline(always)] #[inline(always)]
pub fn build(&mut self, parsed_pseudo: bool) -> ThinArc<SpecificityAndFlags, Component<Impl>> { pub fn build(
&mut self,
parsed_pseudo: bool,
parsed_slotted: bool,
) -> ThinArc<SpecificityAndFlags, Component<Impl>> {
// Compute the specificity and flags. // Compute the specificity and flags.
let mut spec = SpecificityAndFlags(specificity(self.simple_selectors.iter())); let mut spec = SpecificityAndFlags(specificity(self.simple_selectors.iter()));
if parsed_pseudo { if parsed_pseudo {
spec.0 |= HAS_PSEUDO_BIT; spec.0 |= HAS_PSEUDO_BIT;
} }
if parsed_slotted {
spec.0 |= HAS_SLOTTED_BIT;
}
self.build_with_specificity_and_flags(spec) self.build_with_specificity_and_flags(spec)
} }
@ -188,18 +196,28 @@ fn split_from_end<T>(s: &[T], at: usize) -> (&[T], &[T]) {
} }
pub const HAS_PSEUDO_BIT: u32 = 1 << 30; pub const HAS_PSEUDO_BIT: u32 = 1 << 30;
pub const HAS_SLOTTED_BIT: u32 = 1 << 31;
/// We use ten bits for each specificity kind (id, class, element), and the two
/// high bits for the pseudo and slotted flags.
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SpecificityAndFlags(pub u32); pub struct SpecificityAndFlags(pub u32);
impl SpecificityAndFlags { impl SpecificityAndFlags {
#[inline]
pub fn specificity(&self) -> u32 { pub fn specificity(&self) -> u32 {
self.0 & !HAS_PSEUDO_BIT self.0 & !(HAS_PSEUDO_BIT | HAS_SLOTTED_BIT)
} }
#[inline]
pub fn has_pseudo_element(&self) -> bool { pub fn has_pseudo_element(&self) -> bool {
(self.0 & HAS_PSEUDO_BIT) != 0 (self.0 & HAS_PSEUDO_BIT) != 0
} }
#[inline]
pub fn is_slotted(&self) -> bool {
(self.0 & HAS_SLOTTED_BIT) != 0
}
} }
const MAX_10BIT: u32 = (1u32 << 10) - 1; const MAX_10BIT: u32 = (1u32 << 10) - 1;

View file

@ -445,14 +445,22 @@ pub fn namespace_empty_string<Impl: SelectorImpl>() -> Impl::NamespaceUrl {
pub struct Selector<Impl: SelectorImpl>(ThinArc<SpecificityAndFlags, Component<Impl>>); pub struct Selector<Impl: SelectorImpl>(ThinArc<SpecificityAndFlags, Component<Impl>>);
impl<Impl: SelectorImpl> Selector<Impl> { impl<Impl: SelectorImpl> Selector<Impl> {
#[inline]
pub fn specificity(&self) -> u32 { pub fn specificity(&self) -> u32 {
self.0.header.header.specificity() self.0.header.header.specificity()
} }
#[inline]
pub fn has_pseudo_element(&self) -> bool { pub fn has_pseudo_element(&self) -> bool {
self.0.header.header.has_pseudo_element() self.0.header.header.has_pseudo_element()
} }
#[inline]
pub fn is_slotted(&self) -> bool {
self.0.header.header.is_slotted()
}
#[inline]
pub fn pseudo_element(&self) -> Option<&Impl::PseudoElement> { pub fn pseudo_element(&self) -> Option<&Impl::PseudoElement> {
if !self.has_pseudo_element() { if !self.has_pseudo_element() {
return None return None
@ -1219,7 +1227,7 @@ where
builder.push_combinator(combinator); builder.push_combinator(combinator);
} }
Ok(Selector(builder.build(has_pseudo_element))) Ok(Selector(builder.build(has_pseudo_element, slotted)))
} }
impl<Impl: SelectorImpl> Selector<Impl> { impl<Impl: SelectorImpl> Selector<Impl> {

View file

@ -106,6 +106,13 @@ pub struct SelectorMap<T: 'static> {
pub count: usize, pub count: usize,
} }
impl<T: 'static> Default for SelectorMap<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
// FIXME(Manishearth) the 'static bound can be removed when // FIXME(Manishearth) the 'static bound can be removed when
// our HashMap fork (hashglobe) is able to use NonZero, // our HashMap fork (hashglobe) is able to use NonZero,
// or when stdlib gets fallible collections // or when stdlib gets fallible collections

View file

@ -1232,7 +1232,7 @@ impl Stylist {
rule_hash_target.matches_user_and_author_rules(); rule_hash_target.matches_user_and_author_rules();
// Step 1: Normal user-agent rules. // Step 1: Normal user-agent rules.
if let Some(map) = self.cascade_data.user_agent.cascade_data.borrow_for_pseudo(pseudo_element) { if let Some(map) = self.cascade_data.user_agent.cascade_data.rules.borrow_for_pseudo(pseudo_element) {
map.get_all_matching_rules( map.get_all_matching_rules(
element, element,
&rule_hash_target, &rule_hash_target,
@ -1270,7 +1270,7 @@ impl Stylist {
// Which may be more what you would probably expect. // Which may be more what you would probably expect.
if matches_user_and_author_rules { if matches_user_and_author_rules {
// Step 3a: User normal rules. // Step 3a: User normal rules.
if let Some(map) = self.cascade_data.user.borrow_for_pseudo(pseudo_element) { if let Some(map) = self.cascade_data.user.rules.borrow_for_pseudo(pseudo_element) {
map.get_all_matching_rules( map.get_all_matching_rules(
element, element,
&rule_hash_target, &rule_hash_target,
@ -1281,15 +1281,15 @@ impl Stylist {
CascadeLevel::UserNormal, CascadeLevel::UserNormal,
); );
} }
} else {
debug!("skipping user rules");
} }
// Step 3b: XBL rules. // Step 3b: XBL rules.
//
// TODO(emilio): Ensure cascade order is correct for Shadow DOM.
let cut_off_inheritance = element.each_xbl_stylist(|stylist| { let cut_off_inheritance = element.each_xbl_stylist(|stylist| {
// ServoStyleSet::CreateXBLServoStyleSet() loads XBL style sheets // ServoStyleSet::CreateXBLServoStyleSet() loads XBL style sheets
// under eAuthorSheetFeatures level. // under eAuthorSheetFeatures level.
if let Some(map) = stylist.cascade_data.author.borrow_for_pseudo(pseudo_element) { if let Some(map) = stylist.cascade_data.author.rules.borrow_for_pseudo(pseudo_element) {
// NOTE(emilio): This is needed because the XBL stylist may // NOTE(emilio): This is needed because the XBL stylist may
// think it has a different quirks mode than the document. // think it has a different quirks mode than the document.
// //
@ -1320,7 +1320,7 @@ impl Stylist {
!cut_off_inheritance !cut_off_inheritance
{ {
// Step 3c: Author normal rules. // Step 3c: Author normal rules.
if let Some(map) = self.cascade_data.author.borrow_for_pseudo(pseudo_element) { if let Some(map) = self.cascade_data.author.rules.borrow_for_pseudo(pseudo_element) {
map.get_all_matching_rules( map.get_all_matching_rules(
element, element,
&rule_hash_target, &rule_hash_target,
@ -1331,8 +1331,31 @@ impl Stylist {
CascadeLevel::AuthorNormal CascadeLevel::AuthorNormal
); );
} }
} else {
debug!("skipping author normal rules"); let mut current = rule_hash_target.assigned_slot();
while let Some(slot) = current {
// TODO(emilio): Ensure this is the expected cascade order,
// maybe we need to merge the sorting with the above
// get_all_matching_rules.
slot.each_xbl_stylist(|stylist| {
let slotted_rules =
stylist.cascade_data.author.slotted_rules.as_ref();
if let Some(map) = slotted_rules.and_then(|r| r.borrow_for_pseudo(pseudo_element)) {
map.get_all_matching_rules(
element,
&rule_hash_target,
applicable_declarations,
context,
self.quirks_mode,
flags_setter,
CascadeLevel::AuthorNormal
);
}
});
current = slot.assigned_slot();
}
} }
if !only_default_rules { if !only_default_rules {
@ -1368,8 +1391,6 @@ impl Stylist {
) )
); );
} }
} else {
debug!("skipping style attr and SMIL & animation rules");
} }
// //
@ -1388,8 +1409,6 @@ impl Stylist {
) )
); );
} }
} else {
debug!("skipping transition rules");
} }
} }
@ -1841,14 +1860,9 @@ impl<'a> SelectorVisitor for StylistSelectorVisitor<'a> {
} }
} }
/// Data resulting from performing the CSS cascade that is specific to a given /// A set of rules for element and pseudo-elements.
/// origin. #[derive(Debug, Default)]
/// struct ElementAndPseudoRules {
/// FIXME(emilio): Consider renaming and splitting in `CascadeData` and
/// `InvalidationData`? That'd make `clear_cascade_data()` clearer.
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Debug)]
struct CascadeData {
/// Rules from stylesheets at this `CascadeData`'s origin. /// Rules from stylesheets at this `CascadeData`'s origin.
element_map: SelectorMap<Rule>, element_map: SelectorMap<Rule>,
@ -1859,6 +1873,76 @@ struct CascadeData {
/// Figure out a good way to do a `PerNonAnonBox` and `PerAnonBox` (for /// Figure out a good way to do a `PerNonAnonBox` and `PerAnonBox` (for
/// `precomputed_values_for_pseudo`) without duplicating a lot of code. /// `precomputed_values_for_pseudo`) without duplicating a lot of code.
pseudos_map: PerPseudoElementMap<Box<SelectorMap<Rule>>>, pseudos_map: PerPseudoElementMap<Box<SelectorMap<Rule>>>,
}
impl ElementAndPseudoRules {
#[inline(always)]
fn insert(
&mut self,
rule: Rule,
pseudo_element: Option<&PseudoElement>,
quirks_mode: QuirksMode,
) -> Result<(), FailedAllocationError> {
debug_assert!(pseudo_element.map_or(true, |pseudo| !pseudo.is_precomputed()));
let map = match pseudo_element {
None => &mut self.element_map,
Some(pseudo) => {
self.pseudos_map.get_or_insert_with(
&pseudo.canonical(),
|| Box::new(SelectorMap::new())
)
}
};
map.insert(rule, quirks_mode)
}
fn clear(&mut self) {
self.element_map.clear();
self.pseudos_map.clear();
}
#[inline]
fn borrow_for_pseudo(&self, pseudo: Option<&PseudoElement>) -> Option<&SelectorMap<Rule>> {
match pseudo {
Some(pseudo) => self.pseudos_map.get(&pseudo.canonical()).map(|p| &**p),
None => Some(&self.element_map),
}
}
/// Measures heap usage.
#[cfg(feature = "gecko")]
fn add_size_of(&self, ops: &mut MallocSizeOfOps, sizes: &mut ServoStyleSetSizes) {
sizes.mElementAndPseudosMaps += self.element_map.size_of(ops);
for elem in self.pseudos_map.iter() {
if let Some(ref elem) = *elem {
sizes.mElementAndPseudosMaps += <Box<_> as MallocSizeOf>::size_of(elem, ops);
}
}
}
}
/// Data resulting from performing the CSS cascade that is specific to a given
/// origin.
///
/// FIXME(emilio): Consider renaming and splitting in `CascadeData` and
/// `InvalidationData`? That'd make `clear_cascade_data()` clearer.
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
#[derive(Debug)]
struct CascadeData {
/// The rules at this cascade level.
rules: ElementAndPseudoRules,
/// ::slotted() pseudo-element rules.
///
/// We need to store them separately because an element needs to match scoped
/// pseudo-element rules in different style scopes.
///
/// In particular, you need to go through all the style data in all the
/// containing style scopes starting from the closest assigned slot.
slotted_rules: Option<Box<ElementAndPseudoRules>>,
/// A map with all the animations at this `CascadeData`'s origin, indexed /// A map with all the animations at this `CascadeData`'s origin, indexed
/// by name. /// by name.
@ -1925,8 +2009,8 @@ struct CascadeData {
impl CascadeData { impl CascadeData {
fn new() -> Self { fn new() -> Self {
Self { Self {
element_map: SelectorMap::new(), rules: ElementAndPseudoRules::default(),
pseudos_map: PerPseudoElementMap::default(), slotted_rules: None,
animations: Default::default(), animations: Default::default(),
extra_data: ExtraStyleData::default(), extra_data: ExtraStyleData::default(),
invalidation_map: InvalidationMap::new(), invalidation_map: InvalidationMap::new(),
@ -2015,8 +2099,10 @@ impl CascadeData {
for selector in &style_rule.selectors.0 { for selector in &style_rule.selectors.0 {
self.num_selectors += 1; self.num_selectors += 1;
let map = match selector.pseudo_element() { let pseudo_element = selector.pseudo_element();
Some(pseudo) if pseudo.is_precomputed() => {
if let Some(pseudo) = pseudo_element {
if pseudo.is_precomputed() {
debug_assert!(selector.is_universal()); debug_assert!(selector.is_universal());
debug_assert!(matches!(origin, Origin::UserAgent)); debug_assert!(matches!(origin, Origin::UserAgent));
@ -2030,15 +2116,9 @@ impl CascadeData {
CascadeLevel::UANormal, CascadeLevel::UANormal,
selector.specificity() selector.specificity()
)); ));
continue; continue;
} }
None => &mut self.element_map, }
Some(pseudo) => {
self.pseudos_map
.get_or_insert_with(&pseudo.canonical(), || Box::new(SelectorMap::new()))
}
};
let hashes = let hashes =
AncestorHashes::new(&selector, quirks_mode); AncestorHashes::new(&selector, quirks_mode);
@ -2050,7 +2130,15 @@ impl CascadeData {
self.rules_source_order self.rules_source_order
); );
map.insert(rule, quirks_mode)?; if selector.is_slotted() {
if self.slotted_rules.is_none() {
self.slotted_rules = Some(Default::default());
}
let slotted_rules = self.slotted_rules.as_mut().unwrap();
slotted_rules.insert(rule, pseudo_element, quirks_mode)?;
} else {
self.rules.insert(rule, pseudo_element, quirks_mode)?;
}
if rebuild_kind.should_rebuild_invalidation() { if rebuild_kind.should_rebuild_invalidation() {
self.invalidation_map self.invalidation_map
@ -2223,18 +2311,10 @@ impl CascadeData {
true true
} }
#[inline]
fn borrow_for_pseudo(&self, pseudo: Option<&PseudoElement>) -> Option<&SelectorMap<Rule>> {
match pseudo {
Some(pseudo) => self.pseudos_map.get(&pseudo.canonical()).map(|p| &**p),
None => Some(&self.element_map),
}
}
/// Clears the cascade data, but not the invalidation data. /// Clears the cascade data, but not the invalidation data.
fn clear_cascade_data(&mut self) { fn clear_cascade_data(&mut self) {
self.element_map.clear(); self.rules.clear();
self.pseudos_map.clear(); self.slotted_rules = None;
self.animations.clear(); self.animations.clear();
self.extra_data.clear(); self.extra_data.clear();
self.rules_source_order = 0; self.rules_source_order = 0;
@ -2257,12 +2337,9 @@ impl CascadeData {
/// Measures heap usage. /// Measures heap usage.
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
pub fn add_size_of(&self, ops: &mut MallocSizeOfOps, sizes: &mut ServoStyleSetSizes) { pub fn add_size_of(&self, ops: &mut MallocSizeOfOps, sizes: &mut ServoStyleSetSizes) {
sizes.mElementAndPseudosMaps += self.element_map.size_of(ops); self.rules.add_size_of(ops, sizes);
if let Some(ref slotted_rules) = self.slotted_rules {
for elem in self.pseudos_map.iter() { slotted_rules.add_size_of(ops, sizes);
if let Some(ref elem) = *elem {
sizes.mElementAndPseudosMaps += <Box<_> as MallocSizeOf>::size_of(elem, ops);
}
} }
sizes.mOther += self.animations.size_of(ops); sizes.mOther += self.animations.size_of(ops);