Skip to content

Commit

Permalink
Store SelectorInner and only revalidate up to the rightmost ancestor …
Browse files Browse the repository at this point in the history
…combinator.

MozReview-Commit-ID: HiTGVhwuvCE
  • Loading branch information
bholley committed Apr 27, 2017
1 parent 32c624e commit 7e69cf8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
37 changes: 37 additions & 0 deletions components/selectors/parser.rs
Expand Up @@ -169,6 +169,27 @@ impl<Impl: SelectorImpl> SelectorInner<Impl> {
}
}

/// Creates a clone of this selector with everything to the left of
/// (and including) the rightmost ancestor combinator removed. So
/// the selector |span foo > bar + baz| will become |bar + baz|.
/// This is used for revalidation selectors in servo.
///
/// The bloom filter hashes are copied, even though they correspond to
/// parts of the selector that have been stripped out, because they are
/// still useful for fast-rejecting the reduced selectors.
pub fn slice_to_first_ancestor_combinator(&self) -> Self {
let maybe_pos = self.complex.iter_raw()
.position(|s| s.as_combinator()
.map_or(false, |c| c.is_ancestor()));
match maybe_pos {
None => self.clone(),
Some(index) => SelectorInner {
complex: self.complex.slice_to(index),
ancestor_hashes: self.ancestor_hashes.clone(),
},
}
}

/// Creates a SelectorInner from a Vec of Components. Used in tests.
pub fn from_vec(vec: Vec<Component<Impl>>) -> Self {
let complex = ComplexSelector::from_vec(vec);
Expand Down Expand Up @@ -319,6 +340,14 @@ impl<Impl: SelectorImpl> ComplexSelector<Impl> {
ComplexSelector(self.0.clone().slice_to(self.0.len() - index))
}

/// Returns a ComplexSelector identical to |self| but with the leftmost
/// |len() - index| entries removed.
pub fn slice_to(&self, index: usize) -> Self {
// Note that we convert the slice_to to slice_from because selectors are
// stored left-to-right but logical order is right-to-left.
ComplexSelector(self.0.clone().slice_from(self.0.len() - index))
}

/// Creates a ComplexSelector from a vec of Components. Used in tests.
pub fn from_vec(vec: Vec<Component<Impl>>) -> Self {
ComplexSelector(ArcSlice::new(vec.into_boxed_slice()))
Expand Down Expand Up @@ -499,6 +528,14 @@ impl<Impl: SelectorImpl> Component<Impl> {
pub fn is_combinator(&self) -> bool {
matches!(*self, Component::Combinator(_))
}

/// Returns the value as a combinator if applicable, None otherwise.
pub fn as_combinator(&self) -> Option<Combinator> {
match *self {
Component::Combinator(c) => Some(c),
_ => None,
}
}
}

#[derive(Eq, PartialEq, Clone, Hash, Copy, Debug)]
Expand Down
6 changes: 3 additions & 3 deletions components/style/stylist.rs
Expand Up @@ -115,7 +115,7 @@ pub struct Stylist {
/// on state that is not otherwise visible to the cache, like attributes or
/// tree-structural state like child index and pseudos).
#[cfg_attr(feature = "servo", ignore_heap_size_of = "Arc")]
selectors_for_cache_revalidation: Vec<Selector<SelectorImpl>>,
selectors_for_cache_revalidation: Vec<SelectorInner<SelectorImpl>>,

/// The total number of selectors.
num_selectors: usize,
Expand Down Expand Up @@ -336,7 +336,7 @@ impl Stylist {
self.dependencies.note_selector(selector);

if needs_revalidation(selector) {
self.selectors_for_cache_revalidation.push(selector.clone());
self.selectors_for_cache_revalidation.push(selector.inner.slice_to_first_ancestor_combinator());
}
}
}
Expand Down Expand Up @@ -837,7 +837,7 @@ impl Stylist {

for (i, ref selector) in self.selectors_for_cache_revalidation
.iter().enumerate() {
results.set(i, matches_selector(&selector.inner,
results.set(i, matches_selector(selector,
element,
Some(bloom),
&mut StyleRelations::empty(),
Expand Down

0 comments on commit 7e69cf8

Please sign in to comment.