1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#![deny(missing_docs)]
use atomic_refcell::{AtomicRefMut, AtomicRefCell};
use dom::{SendElement, TElement};
use owning_ref::OwningHandle;
use selectors::bloom::BloomFilter;
use servo_arc::Arc;
use smallvec::SmallVec;
thread_local!(static BLOOM_KEY: Arc<AtomicRefCell<BloomFilter>> =
Arc::new(AtomicRefCell::new(BloomFilter::new())));
pub struct StyleBloom<E: TElement> {
filter: OwningHandle<Arc<AtomicRefCell<BloomFilter>>, AtomicRefMut<'static, BloomFilter>>,
elements: SmallVec<[PushedElement<E>; 16]>,
pushed_hashes: SmallVec<[u32; 64]>,
}
const MEMSET_CLEAR_THRESHOLD: usize = 25;
struct PushedElement<E: TElement> {
element: SendElement<E>,
num_hashes: usize,
}
impl<E: TElement> PushedElement<E> {
fn new(el: E, num_hashes: usize) -> Self {
PushedElement {
element: unsafe { SendElement::new(el) },
num_hashes,
}
}
}
fn each_relevant_element_hash<E, F>(element: E, mut f: F)
where
E: TElement,
F: FnMut(u32),
{
f(element.get_local_name().get_hash());
f(element.get_namespace().get_hash());
if let Some(id) = element.get_id() {
f(id.get_hash());
}
element.each_class(|class| {
f(class.get_hash())
});
}
impl<E: TElement> Drop for StyleBloom<E> {
fn drop(&mut self) {
self.clear();
}
}
impl<E: TElement> StyleBloom<E> {
#[inline(never)]
pub fn new() -> Self {
let bloom_arc = BLOOM_KEY.with(|b| b.clone());
let filter = OwningHandle::new_with_fn(bloom_arc, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
debug_assert!(filter.is_zeroed(), "Forgot to zero the bloom filter last time");
StyleBloom {
filter: filter,
elements: Default::default(),
pushed_hashes: Default::default(),
}
}
pub fn filter(&self) -> &BloomFilter {
&*self.filter
}
pub fn push(&mut self, element: E) {
if cfg!(debug_assertions) {
if self.elements.is_empty() {
assert!(element.traversal_parent().is_none());
}
}
self.push_internal(element);
}
fn push_internal(&mut self, element: E) {
let mut count = 0;
each_relevant_element_hash(element, |hash| {
count += 1;
self.filter.insert_hash(hash);
self.pushed_hashes.push(hash);
});
self.elements.push(PushedElement::new(element, count));
}
#[inline]
fn pop(&mut self) -> Option<E> {
let PushedElement { element, num_hashes } = self.elements.pop()?;
let popped_element = *element;
let mut expected_hashes = vec![];
if cfg!(debug_assertions) {
each_relevant_element_hash(popped_element, |hash| expected_hashes.push(hash));
}
for _ in 0..num_hashes {
let hash = self.pushed_hashes.pop().unwrap();
debug_assert_eq!(expected_hashes.pop().unwrap(), hash);
self.filter.remove_hash(hash);
}
Some(popped_element)
}
pub fn is_empty(&self) -> bool {
self.elements.is_empty()
}
pub fn matching_depth(&self) -> usize {
self.elements.len()
}
pub fn clear(&mut self) {
self.elements.clear();
if self.pushed_hashes.len() > MEMSET_CLEAR_THRESHOLD {
self.filter.clear();
self.pushed_hashes.clear();
} else {
for hash in self.pushed_hashes.drain() {
self.filter.remove_hash(hash);
}
debug_assert!(self.filter.is_zeroed());
}
}
pub fn rebuild(&mut self, mut element: E) {
self.clear();
let mut parents_to_insert = SmallVec::<[E; 16]>::new();
while let Some(parent) = element.traversal_parent() {
parents_to_insert.push(parent);
element = parent;
}
for parent in parents_to_insert.drain().rev() {
self.push(parent);
}
}
pub fn assert_complete(&self, mut element: E) {
if cfg!(debug_assertions) {
let mut checked = 0;
while let Some(parent) = element.traversal_parent() {
assert_eq!(parent, *(self.elements[self.elements.len() - 1 - checked].element));
element = parent;
checked += 1;
}
assert_eq!(checked, self.elements.len());
}
}
#[inline]
pub fn current_parent(&self) -> Option<E> {
self.elements.last().map(|ref el| *el.element)
}
pub fn insert_parents_recovering(&mut self,
element: E,
element_depth: usize)
{
if self.elements.is_empty() {
self.rebuild(element);
return;
}
let traversal_parent = match element.traversal_parent() {
Some(parent) => parent,
None => {
self.clear();
return;
}
};
if self.current_parent() == Some(traversal_parent) {
return;
}
if element_depth == 0 {
self.clear();
return;
}
debug_assert!(element_depth != 0,
"We should have already cleared the bloom filter");
debug_assert!(!self.elements.is_empty(),
"How! We should've just rebuilt!");
let mut current_depth = self.elements.len() - 1;
while current_depth > element_depth - 1 {
self.pop().expect("Emilio is bad at math");
current_depth -= 1;
}
let mut common_parent = traversal_parent;
let mut common_parent_depth = element_depth - 1;
let mut parents_to_insert = SmallVec::<[E; 16]>::new();
while common_parent_depth > current_depth {
parents_to_insert.push(common_parent);
common_parent =
common_parent.traversal_parent().expect("We were lied to");
common_parent_depth -= 1;
}
debug_assert_eq!(common_parent_depth, current_depth);
while *(self.elements.last().unwrap().element) != common_parent {
parents_to_insert.push(common_parent);
self.pop().unwrap();
common_parent = match common_parent.traversal_parent() {
Some(parent) => parent,
None => {
debug_assert!(self.elements.is_empty());
if cfg!(feature = "gecko") {
break;
} else {
panic!("should have found a common ancestor");
}
}
}
}
for parent in parents_to_insert.drain().rev() {
self.push(parent);
}
debug_assert_eq!(self.elements.len(), element_depth);
}
}