Skip to content

Commit

Permalink
Fix the handling of the Bloom filter in the style sharing cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky committed Jun 5, 2017
1 parent a36edb9 commit 98f95a3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
10 changes: 9 additions & 1 deletion components/style/bloom.rs
Expand Up @@ -158,6 +158,14 @@ impl<E: TElement> StyleBloom<E> {
}
}

/// Get the element that represents the chain of things inserted
/// into the filter right now. That chain is the given element
/// (if any) and its ancestors.
#[inline]
pub fn current_parent(&self) -> Option<E> {
self.elements.last().map(|el| **el)
}

/// Insert the parents of an element in the bloom filter, trying to recover
/// the filter if the last element inserted doesn't match.
///
Expand Down Expand Up @@ -185,7 +193,7 @@ impl<E: TElement> StyleBloom<E> {
}
};

if self.elements.last().map(|el| **el) == Some(parent_element) {
if self.current_parent() == Some(parent_element) {
// Ta da, cache hit, we're all done.
return;
}
Expand Down
4 changes: 2 additions & 2 deletions components/style/sharing/checks.rs
Expand Up @@ -6,10 +6,10 @@
//! quickly whether it's worth to share style, and whether two different
//! elements can indeed share the same style.

use bloom::StyleBloom;
use context::{SelectorFlagsMap, SharedStyleContext};
use dom::TElement;
use element_state::*;
use selectors::bloom::BloomFilter;
use selectors::matching::StyleRelations;
use sharing::{StyleSharingCandidate, StyleSharingTarget};
use stylearc::Arc;
Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn have_same_state_ignoring_visitedness<E>(element: E,
pub fn revalidate<E>(target: &mut StyleSharingTarget<E>,
candidate: &mut StyleSharingCandidate<E>,
shared_context: &SharedStyleContext,
bloom: &BloomFilter,
bloom: &StyleBloom<E>,
selector_flags_map: &mut SelectorFlagsMap<E>)
-> bool
where E: TElement,
Expand Down
45 changes: 36 additions & 9 deletions components/style/sharing/mod.rs
Expand Up @@ -7,13 +7,13 @@

use Atom;
use bit_vec::BitVec;
use bloom::StyleBloom;
use cache::{LRUCache, LRUCacheMutIterator};
use context::{SelectorFlagsMap, SharedStyleContext, StyleContext};
use data::{ComputedStyle, ElementData, ElementStyles};
use dom::{TElement, SendElement};
use matching::{ChildCascadeRequirement, MatchMethods};
use properties::ComputedValues;
use selectors::bloom::BloomFilter;
use selectors::matching::{ElementSelectorFlags, VisitedHandlingMode, StyleRelations};
use smallvec::SmallVec;
use std::mem;
Expand Down Expand Up @@ -95,19 +95,40 @@ impl ValidationData {
}

/// Computes the revalidation results if needed, and returns it.
/// Inline so we know at compile time what bloom_known_valid is.
#[inline]
fn revalidation_match_results<E, F>(
&mut self,
element: E,
stylist: &Stylist,
bloom: &BloomFilter,
bloom: &StyleBloom<E>,
bloom_known_valid: bool,
flags_setter: &mut F
) -> &BitVec
where E: TElement,
F: FnMut(&E, ElementSelectorFlags),
{
if self.revalidation_match_results.is_none() {
// The bloom filter may already be set up for our element.
// If it is, use it. If not, we must be in a candidate
// (i.e. something in the cache), and the element is one
// of our cousins, not a sibling. In that case, we'll
// just do revalidation selector matching without a bloom
// filter, to avoid thrashing the filter.
let bloom_to_use = if bloom_known_valid {
debug_assert_eq!(bloom.current_parent(),
element.parent_element());
Some(bloom.filter())
} else {
if bloom.current_parent() == element.parent_element() {
Some(bloom.filter())
} else {
None
}
};
self.revalidation_match_results =
Some(stylist.match_revalidation_selectors(&element, bloom,
Some(stylist.match_revalidation_selectors(&element,
bloom_to_use,
flags_setter));
}

Expand Down Expand Up @@ -149,16 +170,18 @@ impl<E: TElement> StyleSharingCandidate<E> {
self.validation_data.pres_hints(*self.element)
}

/// Get the classlist of this candidate.
/// Compute the bit vector of revalidation selector match results
/// for this candidate.
fn revalidation_match_results(
&mut self,
stylist: &Stylist,
bloom: &BloomFilter,
bloom: &StyleBloom<E>,
) -> &BitVec {
self.validation_data.revalidation_match_results(
*self.element,
stylist,
bloom,
/* bloom_known_valid = */ false,
&mut |_, _| {})
}
}
Expand Down Expand Up @@ -204,7 +227,7 @@ impl<E: TElement> StyleSharingTarget<E> {
fn revalidation_match_results(
&mut self,
stylist: &Stylist,
bloom: &BloomFilter,
bloom: &StyleBloom<E>,
selector_flags_map: &mut SelectorFlagsMap<E>
) -> &BitVec {
// It's important to set the selector flags. Otherwise, if we succeed in
Expand All @@ -231,6 +254,7 @@ impl<E: TElement> StyleSharingTarget<E> {
self.element,
stylist,
bloom,
/* bloom_known_valid = */ true,
&mut set_selector_flags)
}

Expand All @@ -243,7 +267,10 @@ impl<E: TElement> StyleSharingTarget<E> {
{
let shared_context = &context.shared;
let selector_flags_map = &mut context.thread_local.selector_flags;
let bloom_filter = context.thread_local.bloom_filter.filter();
let bloom_filter = &context.thread_local.bloom_filter;

debug_assert_eq!(bloom_filter.current_parent(),
self.element.parent_element());

let result = context.thread_local
.style_sharing_candidate_cache
Expand Down Expand Up @@ -400,7 +427,7 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
&mut self,
shared_context: &SharedStyleContext,
selector_flags_map: &mut SelectorFlagsMap<E>,
bloom_filter: &BloomFilter,
bloom_filter: &StyleBloom<E>,
target: &mut StyleSharingTarget<E>,
data: &mut ElementData
) -> StyleSharingResult {
Expand Down Expand Up @@ -498,7 +525,7 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
fn test_candidate(target: &mut StyleSharingTarget<E>,
candidate: &mut StyleSharingCandidate<E>,
shared: &SharedStyleContext,
bloom: &BloomFilter,
bloom: &StyleBloom<E>,
selector_flags_map: &mut SelectorFlagsMap<E>)
-> Result<ComputedStyle, CacheMiss> {
macro_rules! miss {
Expand Down
4 changes: 2 additions & 2 deletions components/style/stylist.rs
Expand Up @@ -1092,7 +1092,7 @@ impl Stylist {
/// revalidation selectors.
pub fn match_revalidation_selectors<E, F>(&self,
element: &E,
bloom: &BloomFilter,
bloom: Option<&BloomFilter>,
flags_setter: &mut F)
-> BitVec
where E: TElement,
Expand All @@ -1101,7 +1101,7 @@ impl Stylist {
// NB: `MatchingMode` doesn't really matter, given we don't share style
// between pseudos.
let mut matching_context =
MatchingContext::new(MatchingMode::Normal, Some(bloom));
MatchingContext::new(MatchingMode::Normal, bloom);

// Note that, by the time we're revalidating, we're guaranteed that the
// candidate and the entry have the same id, classes, and local name.
Expand Down
25 changes: 12 additions & 13 deletions components/style/traversal.rs
Expand Up @@ -799,19 +799,6 @@ fn compute_style<E, D>(_traversal: &D,
context.thread_local.statistics.elements_styled += 1;
let kind = data.restyle_kind();

// First, try the style sharing cache. If we get a match we can skip the rest
// of the work.
if let MatchAndCascade = kind {
let target = StyleSharingTarget::new(element);
let sharing_result = target.share_style_if_possible(context, data);

if let StyleWasShared(index, had_damage) = sharing_result {
context.thread_local.statistics.styles_shared += 1;
context.thread_local.style_sharing_candidate_cache.touch(index);
return had_damage;
}
}

match kind {
MatchAndCascade => {
// Ensure the bloom filter is up to date.
Expand All @@ -820,6 +807,18 @@ fn compute_style<E, D>(_traversal: &D,
traversal_data.current_dom_depth);

context.thread_local.bloom_filter.assert_complete(element);

// Now that our bloom filter is set up, try the style sharing
// cache. If we get a match we can skip the rest of the work.
let target = StyleSharingTarget::new(element);
let sharing_result = target.share_style_if_possible(context, data);

if let StyleWasShared(index, had_damage) = sharing_result {
context.thread_local.statistics.styles_shared += 1;
context.thread_local.style_sharing_candidate_cache.touch(index);
return had_damage;
}

context.thread_local.statistics.elements_matched += 1;

// Perform the matching and cascading.
Expand Down

0 comments on commit 98f95a3

Please sign in to comment.