Skip to main content

sui_runtime/
layout_work.rs

1//! Opt-in, thread-local work counters for benchmark/diagnostic replays.
2//! Calls compile away unless the layout-diagnostics feature is enabled.
3use std::time::Duration;
4use web_time::Instant;
5
6#[derive(Debug, Clone, Copy, Default, PartialEq)]
7pub struct LayoutWorkSnapshot {
8    pub constructed: u64,
9    pub dropped: u64,
10    pub measure_requests: u64,
11    pub measure_executions: u64,
12    pub size_measure_executions: u64,
13    pub measure_cache_hits: u64,
14    pub forced_measures: u64,
15    pub constraint_changes: u64,
16    pub first_measures: u64,
17    pub probe_requests: u64,
18    pub probe_cache_hits: u64,
19    pub shared_probe_cache_hits: u64,
20    pub intrinsic_executions: u64,
21    pub intrinsic_cache_hits: u64,
22    pub intrinsic_horizontal: u64,
23    pub intrinsic_vertical: u64,
24    pub arrange_requests: u64,
25    pub arrange_executions: u64,
26    pub arrange_cache_hits: u64,
27    pub translations: u64,
28    pub paint_executions: u64,
29    pub paint_cache_hits: u64,
30    pub semantics_executions: u64,
31    pub semantics_cache_hits: u64,
32    pub layout_passes: u64,
33    pub gutter_iterations: u64,
34    pub measure_us: f64,
35    pub arrange_us: f64,
36    pub graph_us: f64,
37}
38
39/// Instrumentation seam used by built-in scrollbar layout.
40#[doc(hidden)]
41#[inline]
42pub fn record_scrollbar_gutter_iteration() {
43    record(|work| work.gutter_iterations += 1);
44}
45
46#[cfg(feature = "layout-diagnostics")]
47thread_local! {
48    static WORK: std::cell::RefCell<Option<LayoutWorkSnapshot>> = const { std::cell::RefCell::new(None) };
49}
50
51/// Start a collector on the current UI thread. No per-widget allocation occurs.
52/// Returns false when this build does not include layout-diagnostics.
53pub fn begin_layout_work_collection() -> bool {
54    #[cfg(feature = "layout-diagnostics")]
55    {
56        WORK.with(|work| *work.borrow_mut() = Some(LayoutWorkSnapshot::default()));
57        true
58    }
59    #[cfg(not(feature = "layout-diagnostics"))]
60    {
61        false
62    }
63}
64
65pub fn take_layout_work_collection() -> Option<LayoutWorkSnapshot> {
66    #[cfg(feature = "layout-diagnostics")]
67    {
68        WORK.with(|work| work.borrow_mut().take())
69    }
70    #[cfg(not(feature = "layout-diagnostics"))]
71    {
72        None
73    }
74}
75
76#[inline]
77pub(crate) fn record(update: impl FnOnce(&mut LayoutWorkSnapshot)) {
78    #[cfg(feature = "layout-diagnostics")]
79    WORK.with(|work| {
80        if let Some(work) = work.borrow_mut().as_mut() {
81            update(work);
82        }
83    });
84    #[cfg(not(feature = "layout-diagnostics"))]
85    let _ = update;
86}
87
88#[inline]
89pub(crate) fn started() -> Option<Instant> {
90    #[cfg(feature = "layout-diagnostics")]
91    {
92        WORK.with(|work| work.borrow().is_some().then(Instant::now))
93    }
94    #[cfg(not(feature = "layout-diagnostics"))]
95    {
96        None
97    }
98}
99
100#[inline]
101pub(crate) fn elapsed(start: Option<Instant>, update: impl FnOnce(&mut LayoutWorkSnapshot, f64)) {
102    if let Some(start) = start {
103        let elapsed: Duration = start.elapsed();
104        record(|work| update(work, elapsed.as_secs_f64() * 1e6));
105    }
106}
107
108#[cfg(all(test, feature = "layout-diagnostics"))]
109mod tests {
110    use super::*;
111    #[test]
112    fn collection_is_scoped_resettable_and_thread_local() {
113        assert!(begin_layout_work_collection());
114        record(|work| work.constructed += 2);
115        assert!(
116            std::thread::spawn(take_layout_work_collection)
117                .join()
118                .unwrap()
119                .is_none()
120        );
121        assert_eq!(take_layout_work_collection().unwrap().constructed, 2);
122        assert!(take_layout_work_collection().is_none());
123        record(|work| work.constructed += 10);
124        begin_layout_work_collection();
125        assert_eq!(
126            take_layout_work_collection().unwrap(),
127            LayoutWorkSnapshot::default()
128        );
129    }
130}