Skip to main content

sk_css/
perf.rs

1//! Counters for what the cascade does: selectors the matcher ran, and how `compute_node`'s time
2//! divides between matching, custom properties and folding. Accumulated per `compute_node` call and
3//! drained by the host's phase reporter.
4
5use std::cell::Cell;
6
7thread_local! {
8    static RULES_TESTED: Cell<u64> = const { Cell::new(0) };
9    static MATCH_MS: Cell<f32> = const { Cell::new(0.0) };
10    static VARS_MS: Cell<f32> = const { Cell::new(0.0) };
11    static FOLD_MS: Cell<f32> = const { Cell::new(0.0) };
12    static SPLIT: Cell<Split> = const { Cell::new(Split { by_bucket: [0; 6], matched: 0 }) };
13    static SHARE: Cell<(u64, u64)> = const { Cell::new((0, 0)) };
14}
15
16pub(crate) fn note_share(hit: bool) {
17    SHARE.with(|c| {
18        let (asked, hits) = c.get();
19        c.set((asked + 1, hits + u64::from(hit)));
20    });
21}
22
23pub fn take_share() -> (u64, u64) {
24    SHARE.with(|c| c.replace((0, 0)))
25}
26
27#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
28pub struct Split {
29    pub by_bucket: [u64; 6],
30    pub matched: u64,
31}
32
33pub(crate) fn note_split(by_bucket: [u32; 6], matched: usize) {
34    SPLIT.with(|c| {
35        let mut s = c.get();
36        for (total, n) in s.by_bucket.iter_mut().zip(by_bucket) {
37            *total = total.wrapping_add(n as u64);
38        }
39        s.matched = s.matched.wrapping_add(matched as u64);
40        c.set(s);
41    });
42}
43
44pub fn take_split() -> Split {
45    SPLIT.with(|c| c.replace(Split::default()))
46}
47
48pub(crate) fn note_node(tested: u64, match_ms: f32, vars_ms: f32, fold_ms: f32) {
49    RULES_TESTED.with(|c| c.set(c.get().wrapping_add(tested)));
50    MATCH_MS.with(|c| c.set(c.get() + match_ms));
51    VARS_MS.with(|c| c.set(c.get() + vars_ms));
52    FOLD_MS.with(|c| c.set(c.get() + fold_ms));
53}
54
55pub fn take() -> (u64, f32, f32, f32) {
56    (
57        RULES_TESTED.with(|c| c.replace(0)),
58        MATCH_MS.with(|c| c.replace(0.0)),
59        VARS_MS.with(|c| c.replace(0.0)),
60        FOLD_MS.with(|c| c.replace(0.0)),
61    )
62}