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
use fnv::FnvHashMap;
use logical_geometry::WritingMode;
use properties::{ComputedValues, StyleBuilder};
use rule_tree::StrongRuleNode;
use selector_parser::PseudoElement;
use servo_arc::Arc;
use smallvec::SmallVec;
use values::computed::NonNegativeLength;
#[derive(Clone, Debug, Default)]
pub struct RuleCacheConditions {
uncacheable: bool,
font_size: Option<NonNegativeLength>,
writing_mode: Option<WritingMode>,
}
impl RuleCacheConditions {
pub fn set_font_size_dependency(&mut self, font_size: NonNegativeLength) {
debug_assert!(self.font_size.map_or(true, |f| f == font_size));
self.font_size = Some(font_size);
}
pub fn set_uncacheable(&mut self) {
self.uncacheable = true;
}
pub fn set_writing_mode_dependency(&mut self, writing_mode: WritingMode) {
debug_assert!(self.writing_mode.map_or(true, |wm| wm == writing_mode));
self.writing_mode = Some(writing_mode);
}
fn cacheable(&self) -> bool {
!self.uncacheable
}
fn matches(&self, style: &StyleBuilder) -> bool {
if self.uncacheable {
return false;
}
if let Some(fs) = self.font_size {
if style.get_font().clone_font_size().size != fs {
return false;
}
}
if let Some(wm) = self.writing_mode {
if style.writing_mode != wm {
return false;
}
}
true
}
}
pub struct RuleCache {
map: FnvHashMap<StrongRuleNode, SmallVec<[(RuleCacheConditions, Arc<ComputedValues>); 1]>>,
}
impl RuleCache {
pub fn new() -> Self {
Self {
map: FnvHashMap::default(),
}
}
pub fn find(
&self,
builder_with_early_props: &StyleBuilder,
) -> Option<&ComputedValues> {
if builder_with_early_props.is_style_if_visited() {
return None;
}
if builder_with_early_props.pseudo
.and_then(|p| p.property_restriction())
.is_some() {
return None;
}
let rules = builder_with_early_props.rules.as_ref()?;
let cached_values = self.map.get(rules)?;
for &(ref conditions, ref values) in cached_values.iter() {
if conditions.matches(builder_with_early_props) {
debug!("Using cached reset style with conditions {:?}", conditions);
return Some(&**values)
}
}
None
}
pub fn insert_if_possible(
&mut self,
style: &Arc<ComputedValues>,
pseudo: Option<&PseudoElement>,
conditions: &RuleCacheConditions,
) -> bool {
if !conditions.cacheable() {
return false;
}
if style.is_style_if_visited() {
return false;
}
if pseudo.and_then(|p| p.property_restriction()).is_some() {
return false;
}
let rules = match style.rules {
Some(ref r) => r.clone(),
None => return false,
};
debug!("Inserting cached reset style with conditions {:?}", conditions);
self.map
.entry(rules)
.or_insert_with(SmallVec::new)
.push((conditions.clone(), style.clone()));
true
}
}