Phase 4.3-4.4: Style match cache with epoch invalidation (RefCell), remove redundant re-sort in query(), merge-scan complex rules in order
This commit is contained in:
@@ -529,7 +529,7 @@ impl Interpreter {
|
|||||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
stylesheet.matching_rules(el.type_name, el_id, &classes, active_pseudo, structural, ancestors, preceding_siblings, &el_attributes)
|
stylesheet.matching_rules(Some(el.element_id), el.type_name, el_id, &classes, active_pseudo, structural, ancestors, preceding_siblings, &el_attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_prop(v: &str, variables: &HashMap<String, Value>, rhei: &RheiContext) -> String {
|
fn resolve_prop(v: &str, variables: &HashMap<String, Value>, rhei: &RheiContext) -> String {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use super::reactive::ElementId;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -490,6 +492,7 @@ impl StyleSheet {
|
|||||||
|
|
||||||
pub fn matching_rules<'a>(
|
pub fn matching_rules<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
|
element_id: Option<ElementId>,
|
||||||
type_name: &str,
|
type_name: &str,
|
||||||
el_id: Option<&str>,
|
el_id: Option<&str>,
|
||||||
el_classes: &[&str],
|
el_classes: &[&str],
|
||||||
@@ -499,8 +502,9 @@ impl StyleSheet {
|
|||||||
preceding_siblings: &[AncestorInfo],
|
preceding_siblings: &[AncestorInfo],
|
||||||
el_attributes: &HashMap<String, String>,
|
el_attributes: &HashMap<String, String>,
|
||||||
) -> Vec<&'a HashMap<String, String>> {
|
) -> Vec<&'a HashMap<String, String>> {
|
||||||
self.index.query(
|
self.index.query_cached(
|
||||||
type_name, el_id, el_classes, active_pseudo,
|
element_id, active_pseudo,
|
||||||
|
type_name, el_id, el_classes,
|
||||||
structural, ancestors, preceding_siblings, el_attributes,
|
structural, ancestors, preceding_siblings, el_attributes,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -508,6 +512,7 @@ impl StyleSheet {
|
|||||||
pub fn matching_pseudo_rules(
|
pub fn matching_pseudo_rules(
|
||||||
&self,
|
&self,
|
||||||
pseudo: &str,
|
pseudo: &str,
|
||||||
|
element_id: Option<ElementId>,
|
||||||
type_name: &str,
|
type_name: &str,
|
||||||
el_id: Option<&str>,
|
el_id: Option<&str>,
|
||||||
el_classes: &[&str],
|
el_classes: &[&str],
|
||||||
@@ -518,7 +523,7 @@ impl StyleSheet {
|
|||||||
) -> HashMap<String, String> {
|
) -> HashMap<String, String> {
|
||||||
let mut props = HashMap::new();
|
let mut props = HashMap::new();
|
||||||
let all_sheets = self.matching_rules(
|
let all_sheets = self.matching_rules(
|
||||||
type_name, el_id, el_classes, &[pseudo],
|
element_id, type_name, el_id, el_classes, &[pseudo],
|
||||||
structural, ancestors, preceding_siblings, el_attributes,
|
structural, ancestors, preceding_siblings, el_attributes,
|
||||||
);
|
);
|
||||||
for sheet in &all_sheets {
|
for sheet in &all_sheets {
|
||||||
@@ -555,6 +560,10 @@ pub struct StyleIndex {
|
|||||||
rules: Vec<StyleRule>,
|
rules: Vec<StyleRule>,
|
||||||
/// Epoch counter: incremented on rebuild, used for cache invalidation
|
/// Epoch counter: incremented on rebuild, used for cache invalidation
|
||||||
epoch: u64,
|
epoch: u64,
|
||||||
|
/// Cache: element_id → (matched_rule_ids, epoch_at_insert)
|
||||||
|
match_cache: RefCell<HashMap<ElementId, (Vec<RuleId>, u64)>>,
|
||||||
|
/// Cache for pseudo-class matches: (element_id, pseudo_class) → (matched_rule_ids, epoch)
|
||||||
|
pseudo_cache: RefCell<HashMap<(ElementId, String), (Vec<RuleId>, u64)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StyleIndex {
|
impl StyleIndex {
|
||||||
@@ -568,6 +577,8 @@ impl StyleIndex {
|
|||||||
complex_rules: Vec::new(),
|
complex_rules: Vec::new(),
|
||||||
rules: Vec::new(),
|
rules: Vec::new(),
|
||||||
epoch: 0,
|
epoch: 0,
|
||||||
|
match_cache: RefCell::new(HashMap::new()),
|
||||||
|
pseudo_cache: RefCell::new(HashMap::new()),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sort rules by specificity once
|
// Sort rules by specificity once
|
||||||
@@ -624,6 +635,72 @@ impl StyleIndex {
|
|||||||
self.epoch
|
self.epoch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn query_cached<'a>(
|
||||||
|
&'a self,
|
||||||
|
element_id: Option<ElementId>,
|
||||||
|
active_pseudo: &[&str],
|
||||||
|
type_name: &str,
|
||||||
|
el_id: Option<&str>,
|
||||||
|
el_classes: &[&str],
|
||||||
|
structural: &StructuralContext,
|
||||||
|
ancestors: &[AncestorInfo],
|
||||||
|
preceding_siblings: &[AncestorInfo],
|
||||||
|
el_attributes: &HashMap<String, String>,
|
||||||
|
) -> Vec<&'a HashMap<String, String>> {
|
||||||
|
let eid = match element_id {
|
||||||
|
Some(id) => id,
|
||||||
|
None => return self.query(
|
||||||
|
type_name, el_id, el_classes, active_pseudo,
|
||||||
|
structural, ancestors, preceding_siblings, el_attributes,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check cache
|
||||||
|
if active_pseudo.is_empty() {
|
||||||
|
let cache = self.match_cache.borrow();
|
||||||
|
if let Some((cached_ids, cached_epoch)) = cache.get(&eid) {
|
||||||
|
if *cached_epoch == self.epoch {
|
||||||
|
return cached_ids.iter().map(|&rid| &self.rules[rid].properties).collect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let pseudo_key = active_pseudo.join(",");
|
||||||
|
let cache = self.pseudo_cache.borrow();
|
||||||
|
if let Some((cached_ids, cached_epoch)) = cache.get(&(eid, pseudo_key)) {
|
||||||
|
if *cached_epoch == self.epoch {
|
||||||
|
return cached_ids.iter().map(|&rid| &self.rules[rid].properties).collect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache miss — run full query
|
||||||
|
let result = self.query(
|
||||||
|
type_name, el_id, el_classes, active_pseudo,
|
||||||
|
structural, ancestors, preceding_siblings, el_attributes,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Extract rule IDs from matched results (track which rules matched)
|
||||||
|
let matched_ids: Vec<RuleId> = {
|
||||||
|
result.iter().filter_map(|props| {
|
||||||
|
self.rules.iter().position(|r| &r.properties == *props)
|
||||||
|
}).collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Populate cache (short-lived mutable borrow)
|
||||||
|
if active_pseudo.is_empty() {
|
||||||
|
if !matched_ids.is_empty() || self.match_cache.borrow().len() < 2048 {
|
||||||
|
self.match_cache.borrow_mut().insert(eid, (matched_ids, self.epoch));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let pseudo_key = active_pseudo.join(",");
|
||||||
|
if !matched_ids.is_empty() || self.pseudo_cache.borrow().len() < 1024 {
|
||||||
|
self.pseudo_cache.borrow_mut().insert((eid, pseudo_key), (matched_ids, self.epoch));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
pub fn query<'a>(
|
pub fn query<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
type_name: &str,
|
type_name: &str,
|
||||||
@@ -705,9 +782,25 @@ impl StyleIndex {
|
|||||||
let mut seen = std::collections::HashSet::new();
|
let mut seen = std::collections::HashSet::new();
|
||||||
candidates.retain(|r| seen.insert(*r));
|
candidates.retain(|r| seen.insert(*r));
|
||||||
|
|
||||||
// Test candidates against full selector
|
// Merge candidate and complex matches in a single pass.
|
||||||
|
// Both lists reference self.rules, which is already sorted by specificity,
|
||||||
|
// so we collect all matches and they stay in insertion order.
|
||||||
let mut matched: Vec<&StyleRule> = Vec::new();
|
let mut matched: Vec<&StyleRule> = Vec::new();
|
||||||
|
let mut ci = 0usize;
|
||||||
|
|
||||||
for &rid in &candidates {
|
for &rid in &candidates {
|
||||||
|
// Insert any complex rules that precede this candidate in the sorted rules
|
||||||
|
while ci < self.complex_rules.len() && self.complex_rules[ci].1 < rid {
|
||||||
|
let (sel, crid) = &self.complex_rules[ci];
|
||||||
|
if sel.matches(
|
||||||
|
type_name, el_id, el_classes, active_pseudo,
|
||||||
|
structural, ancestors, preceding_siblings, el_attributes,
|
||||||
|
) {
|
||||||
|
matched.push(&self.rules[*crid]);
|
||||||
|
}
|
||||||
|
ci += 1;
|
||||||
|
}
|
||||||
|
|
||||||
let rule = &self.rules[rid];
|
let rule = &self.rules[rid];
|
||||||
if rule.selector.matches(
|
if rule.selector.matches(
|
||||||
type_name, el_id, el_classes, active_pseudo,
|
type_name, el_id, el_classes, active_pseudo,
|
||||||
@@ -717,24 +810,16 @@ impl StyleIndex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test complex rules
|
// Remaining complex rules
|
||||||
for (sel, rid) in &self.complex_rules {
|
for (sel, crid) in &self.complex_rules[ci..] {
|
||||||
if sel.matches(
|
if sel.matches(
|
||||||
type_name, el_id, el_classes, active_pseudo,
|
type_name, el_id, el_classes, active_pseudo,
|
||||||
structural, ancestors, preceding_siblings, el_attributes,
|
structural, ancestors, preceding_siblings, el_attributes,
|
||||||
) {
|
) {
|
||||||
matched.push(&self.rules[*rid]);
|
matched.push(&self.rules[*crid]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-sort matched by specificity (candidates are already sorted)
|
|
||||||
// but we only need to sort the matched subset
|
|
||||||
matched.sort_by(|a, b| {
|
|
||||||
a.selector
|
|
||||||
.specificity()
|
|
||||||
.cmp(&b.selector.specificity())
|
|
||||||
});
|
|
||||||
|
|
||||||
matched.into_iter().map(|r| &r.properties).collect()
|
matched.into_iter().map(|r| &r.properties).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,10 +59,10 @@ pub fn collect_hover_active<'a>(
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let hover_props = stylesheet.matching_pseudo_rules(
|
let hover_props = stylesheet.matching_pseudo_rules(
|
||||||
"hover", el.type_name, el_id, &classes, &default_struct, &[], &[], &el_attributes,
|
"hover", Some(el.element_id), el.type_name, el_id, &classes, &default_struct, &[], &[], &el_attributes,
|
||||||
);
|
);
|
||||||
let active_props = stylesheet.matching_pseudo_rules(
|
let active_props = stylesheet.matching_pseudo_rules(
|
||||||
"active", el.type_name, el_id, &classes, &default_struct, &[], &[], &el_attributes,
|
"active", Some(el.element_id), el.type_name, el_id, &classes, &default_struct, &[], &[], &el_attributes,
|
||||||
);
|
);
|
||||||
|
|
||||||
(hover_props, active_props)
|
(hover_props, active_props)
|
||||||
@@ -1032,9 +1032,9 @@ fn render_button<'a>(
|
|||||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
||||||
.collect();
|
.collect();
|
||||||
let hover_sheets: Vec<&HashMap<String, String>> =
|
let hover_sheets: Vec<&HashMap<String, String>> =
|
||||||
stylesheet.matching_rules(el.type_name, el_id, &classes, &["hover"], &default_struct, &[], &[], &el_attributes);
|
stylesheet.matching_rules(Some(el.element_id), el.type_name, el_id, &classes, &["hover"], &default_struct, &[], &[], &el_attributes);
|
||||||
let active_sheets: Vec<&HashMap<String, String>> =
|
let active_sheets: Vec<&HashMap<String, String>> =
|
||||||
stylesheet.matching_rules(el.type_name, el_id, &classes, &["active"], &default_struct, &[], &[], &el_attributes);
|
stylesheet.matching_rules(Some(el.element_id), el.type_name, el_id, &classes, &["active"], &default_struct, &[], &[], &el_attributes);
|
||||||
|
|
||||||
let mut hover_props = std::collections::HashMap::new();
|
let mut hover_props = std::collections::HashMap::new();
|
||||||
for sheet in &hover_sheets {
|
for sheet in &hover_sheets {
|
||||||
|
|||||||
Reference in New Issue
Block a user