1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5 //! The style bloom filter is used as an optimization when matching deep
6 //! descendant selectors.
10 use crate::dom::{SendElement, TElement};
11 use atomic_refcell::{AtomicRefCell, AtomicRefMut};
12 use owning_ref::OwningHandle;
13 use selectors::bloom::BloomFilter;
15 use smallvec::SmallVec;
16 use std::mem::ManuallyDrop;
19 /// Bloom filters are large allocations, so we store them in thread-local storage
20 /// such that they can be reused across style traversals. StyleBloom is responsible
21 /// for ensuring that the bloom filter is zeroed when it is dropped.
23 /// We intentionally leak this from TLS because we don't have the guarantee
24 /// of TLS destructors to run in worker threads.
26 /// We could change this once https://github.com/rayon-rs/rayon/issues/688
27 /// is fixed, hopefully.
28 static BLOOM_KEY: ManuallyDrop<Arc<AtomicRefCell<BloomFilter>>> =
29 ManuallyDrop::new(Arc::new_leaked(Default::default()));
32 /// A struct that allows us to fast-reject deep descendant selectors avoiding
33 /// selector-matching.
35 /// This is implemented using a counting bloom filter, and it's a standard
36 /// optimization. See Gecko's `AncestorFilter`, and Blink's and WebKit's
39 /// The constraints for Servo's style system are a bit different compared to
40 /// traditional style systems given Servo does a parallel breadth-first
41 /// traversal instead of a sequential depth-first traversal.
43 /// This implies that we need to track a bit more state than other browsers to
44 /// ensure we're doing the correct thing during the traversal, and being able to
45 /// apply this optimization effectively.
47 /// Concretely, we have a bloom filter instance per worker thread, and we track
48 /// the current DOM depth in order to find a common ancestor when it doesn't
49 /// match the previous element we've styled.
51 /// This is usually a pretty fast operation (we use to be one level deeper than
52 /// the previous one), but in the case of work-stealing, we may needed to push
53 /// and pop multiple elements.
55 /// See the `insert_parents_recovering`, where most of the magic happens.
57 /// Regarding thread-safety, this struct is safe because:
59 /// * We clear this after a restyle.
60 /// * The DOM shape and attributes (and every other thing we access here) are
61 /// immutable during a restyle.
63 pub struct StyleBloom<E: TElement> {
64 /// A handle to the bloom filter from the thread upon which this StyleBloom
65 /// was created. We use AtomicRefCell so that this is all |Send|, which allows
66 /// StyleBloom to live in ThreadLocalStyleContext, which is dropped from the
68 filter: OwningHandle<Arc<AtomicRefCell<BloomFilter>>, AtomicRefMut<'static, BloomFilter>>,
70 /// The stack of elements that this bloom filter contains, along with the
71 /// number of hashes pushed for each element.
72 elements: SmallVec<[PushedElement<E>; 16]>,
74 /// Stack of hashes that have been pushed onto this filter.
75 pushed_hashes: SmallVec<[u32; 64]>,
78 /// The very rough benchmarks in the selectors crate show clear()
79 /// costing about 25 times more than remove_hash(). We use this to implement
80 /// clear() more efficiently when only a small number of hashes have been
83 /// One subtly to note is that remove_hash() will not touch the value
84 /// if the filter overflowed. However, overflow can only occur if we
85 /// get 255 collisions on the same hash value, and 25 < 255.
86 const MEMSET_CLEAR_THRESHOLD: usize = 25;
88 struct PushedElement<E: TElement> {
89 /// The element that was pushed.
90 element: SendElement<E>,
92 /// The number of hashes pushed for the element.
96 impl<E: TElement> PushedElement<E> {
97 fn new(el: E, num_hashes: usize) -> Self {
99 element: unsafe { SendElement::new(el) },
105 /// Returns whether the attribute name is excluded from the bloom filter.
107 /// We do this for attributes that are very common but not commonly used in
110 pub fn is_attr_name_excluded_from_filter(atom: &crate::Atom) -> bool {
111 *atom == atom!("class") || *atom == atom!("id") || *atom == atom!("style")
114 fn each_relevant_element_hash<E, F>(element: E, mut f: F)
119 f(element.local_name().get_hash());
120 f(element.namespace().get_hash());
122 if let Some(id) = element.id() {
126 element.each_class(|class| f(class.get_hash()));
128 element.each_attr_name(|name| {
129 if !is_attr_name_excluded_from_filter(name) {
135 impl<E: TElement> Drop for StyleBloom<E> {
137 // Leave the reusable bloom filter in a zeroed state.
142 impl<E: TElement> StyleBloom<E> {
143 /// Create an empty `StyleBloom`. Because StyleBloom acquires the thread-
144 /// local filter buffer, creating multiple live StyleBloom instances at
145 /// the same time on the same thread will panic.
147 // Forced out of line to limit stack frame sizes after extra inlining from
148 // https://github.com/rust-lang/rust/pull/43931
150 // See https://github.com/servo/servo/pull/18420#issuecomment-328769322
152 pub fn new() -> Self {
153 let bloom_arc = BLOOM_KEY.with(|b| Arc::clone(&*b));
155 OwningHandle::new_with_fn(bloom_arc, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
158 "Forgot to zero the bloom filter last time"
162 elements: Default::default(),
163 pushed_hashes: Default::default(),
167 /// Return the bloom filter used properly by the `selectors` crate.
168 pub fn filter(&self) -> &BloomFilter {
172 /// Push an element to the bloom filter, knowing that it's a child of the
173 /// last element parent.
174 pub fn push(&mut self, element: E) {
175 if cfg!(debug_assertions) {
176 if self.elements.is_empty() {
177 assert!(element.traversal_parent().is_none());
180 self.push_internal(element);
183 /// Same as `push`, but without asserting, in order to use it from
185 fn push_internal(&mut self, element: E) {
187 each_relevant_element_hash(element, |hash| {
189 self.filter.insert_hash(hash);
190 self.pushed_hashes.push(hash);
192 self.elements.push(PushedElement::new(element, count));
195 /// Pop the last element in the bloom filter and return it.
197 fn pop(&mut self) -> Option<E> {
201 } = self.elements.pop()?;
202 let popped_element = *element;
204 // Verify that the pushed hashes match the ones we'd get from the element.
205 let mut expected_hashes = vec![];
206 if cfg!(debug_assertions) {
207 each_relevant_element_hash(popped_element, |hash| expected_hashes.push(hash));
210 for _ in 0..num_hashes {
211 let hash = self.pushed_hashes.pop().unwrap();
212 debug_assert_eq!(expected_hashes.pop().unwrap(), hash);
213 self.filter.remove_hash(hash);
219 /// Returns the DOM depth of elements that can be correctly
220 /// matched against the bloom filter (that is, the number of
221 /// elements in our list).
222 pub fn matching_depth(&self) -> usize {
226 /// Clears the bloom filter.
227 pub fn clear(&mut self) {
228 self.elements.clear();
230 if self.pushed_hashes.len() > MEMSET_CLEAR_THRESHOLD {
232 self.pushed_hashes.clear();
234 for hash in self.pushed_hashes.drain(..) {
235 self.filter.remove_hash(hash);
237 debug_assert!(self.filter.is_zeroed());
241 /// Rebuilds the bloom filter up to the parent of the given element.
242 pub fn rebuild(&mut self, mut element: E) {
245 let mut parents_to_insert = SmallVec::<[E; 16]>::new();
246 while let Some(parent) = element.traversal_parent() {
247 parents_to_insert.push(parent);
251 for parent in parents_to_insert.drain(..).rev() {
256 /// In debug builds, asserts that all the parents of `element` are in the
259 /// Goes away in release builds.
260 pub fn assert_complete(&self, mut element: E) {
261 if cfg!(debug_assertions) {
263 while let Some(parent) = element.traversal_parent() {
266 *(self.elements[self.elements.len() - 1 - checked].element)
271 assert_eq!(checked, self.elements.len());
275 /// Get the element that represents the chain of things inserted
276 /// into the filter right now. That chain is the given element
277 /// (if any) and its ancestors.
279 pub fn current_parent(&self) -> Option<E> {
280 self.elements.last().map(|ref el| *el.element)
283 /// Insert the parents of an element in the bloom filter, trying to recover
284 /// the filter if the last element inserted doesn't match.
286 /// Gets the element depth in the dom, to make it efficient, or if not
287 /// provided always rebuilds the filter from scratch.
289 /// Returns the new bloom filter depth, that the traversal code is
290 /// responsible to keep around if it wants to get an effective filter.
291 pub fn insert_parents_recovering(&mut self, element: E, element_depth: usize) {
292 // Easy case, we're in a different restyle, or we're empty.
293 if self.elements.is_empty() {
294 self.rebuild(element);
298 let traversal_parent = match element.traversal_parent() {
299 Some(parent) => parent,
301 // Yay, another easy case.
307 if self.current_parent() == Some(traversal_parent) {
308 // Ta da, cache hit, we're all done.
312 if element_depth == 0 {
317 // We should've early exited above.
320 "We should have already cleared the bloom filter"
322 debug_assert!(!self.elements.is_empty(), "How! We should've just rebuilt!");
324 // Now the fun begins: We have the depth of the dom and the depth of the
325 // last element inserted in the filter, let's try to find a common
328 // The current depth, that is, the depth of the last element inserted in
329 // the bloom filter, is the number of elements _minus one_, that is: if
330 // there's one element, it must be the root -> depth zero.
331 let mut current_depth = self.elements.len() - 1;
333 // If the filter represents an element too deep in the dom, we need to
335 while current_depth > element_depth - 1 {
336 self.pop().expect("Emilio is bad at math");
340 // Now let's try to find a common parent in the bloom filter chain,
341 // starting with traversal_parent.
342 let mut common_parent = traversal_parent;
343 let mut common_parent_depth = element_depth - 1;
345 // Let's collect the parents we are going to need to insert once we've
346 // found the common one.
347 let mut parents_to_insert = SmallVec::<[E; 16]>::new();
349 // If the bloom filter still doesn't have enough elements, the common
350 // parent is up in the dom.
351 while common_parent_depth > current_depth {
352 // TODO(emilio): Seems like we could insert parents here, then
353 // reverse the slice.
354 parents_to_insert.push(common_parent);
355 common_parent = common_parent.traversal_parent().expect("We were lied to");
356 common_parent_depth -= 1;
359 // Now the two depths are the same.
360 debug_assert_eq!(common_parent_depth, current_depth);
362 // Happy case: The parents match, we only need to push the ancestors
363 // we've collected and we'll never enter in this loop.
365 // Not-so-happy case: Parent's don't match, so we need to keep going up
366 // until we find a common ancestor.
368 // Gecko currently models native anonymous content that conceptually
369 // hangs off the document (such as scrollbars) as a separate subtree
370 // from the document root.
372 // Thus it's possible with Gecko that we do not find any common
374 while *(self.elements.last().unwrap().element) != common_parent {
375 parents_to_insert.push(common_parent);
377 common_parent = match common_parent.traversal_parent() {
378 Some(parent) => parent,
380 debug_assert!(self.elements.is_empty());
381 if cfg!(feature = "gecko") {
384 panic!("should have found a common ancestor");
390 // Now the parents match, so insert the stack of elements we have been
391 // collecting so far.
392 for parent in parents_to_insert.drain(..).rev() {
396 debug_assert_eq!(self.elements.len(), element_depth);