From 21b790af4a60f81331054ef82effd64f2523c0ac Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 06:41:32 +0000 Subject: [PATCH] Gate the counters on the testing feature --- .../ui-contract/src/contract/steps/measure.rs | 1 + .../ui-contract/src/runtime/egui/text_edit.rs | 1 + .../src/runtime/egui/text_layout.rs | 1 + crates/ui-contract/src/runtime/lazy.rs | 23 ++++++------ crates/ui-contract/src/runtime/measure.rs | 35 +++++++------------ 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/crates/ui-contract/src/contract/steps/measure.rs b/crates/ui-contract/src/contract/steps/measure.rs index 856259c7a..6ccec0dbf 100644 --- a/crates/ui-contract/src/contract/steps/measure.rs +++ b/crates/ui-contract/src/contract/steps/measure.rs @@ -27,6 +27,7 @@ pub(crate) fn measure_content( value: Option<&ValueOf>, ) -> (Vec2, Inputs) { let cx = measure_cx::(input, style, resolved, value); + #[cfg(feature = "testing")] crate::runtime::measure::count_measure_ask(input.ui.ctx(), std::any::type_name::()); layout_memory::measure_call(input.ui.ctx(), || comp.measure(&cx)) } diff --git a/crates/ui-contract/src/runtime/egui/text_edit.rs b/crates/ui-contract/src/runtime/egui/text_edit.rs index e75201bd6..237e116be 100644 --- a/crates/ui-contract/src/runtime/egui/text_edit.rs +++ b/crates/ui-contract/src/runtime/egui/text_edit.rs @@ -127,6 +127,7 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown { } None => job.append(text, 0.0, plain), } + #[cfg(feature = "testing")] crate::runtime::measure::count_text_layout(ui.ctx()); ui.fonts_mut(|fonts| crate::runtime::egui::text_layout::layout_with(fonts, job)) }; diff --git a/crates/ui-contract/src/runtime/egui/text_layout.rs b/crates/ui-contract/src/runtime/egui/text_layout.rs index 1d64bf0df..d07364659 100644 --- a/crates/ui-contract/src/runtime/egui/text_layout.rs +++ b/crates/ui-contract/src/runtime/egui/text_layout.rs @@ -10,6 +10,7 @@ use std::sync::Arc; /// different logical heights at different interface scales (14.5 at scale 2, 15 at scale 1), and a wrapped text would /// change height with the scale. pub(crate) fn layout(ctx: &Context, job: LayoutJob) -> Arc { + #[cfg(feature = "testing")] crate::runtime::measure::count_text_layout(ctx); ctx.fonts_mut(|fonts| layout_with(fonts, job)) } diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 7850311c1..b6fd3756c 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -256,6 +256,7 @@ pub(crate) fn decide(ctx: &Context, id: Id, view: &View, at: impl FnOnce(Vec2) - }; if skip { Decision::Skip(size) } else { Decision::Run } }); + #[cfg(feature = "testing")] if matches!(decision, Decision::Skip(_)) { crate::runtime::measure::count_settle(ctx, id, crate::runtime::measure::Settle::OutOfView); } @@ -302,17 +303,11 @@ pub(crate) fn replay(ctx: &Context, id: Id) { /// - for a child with no content version: no input other than pointer motion this frame or the one before; /// - the run did nothing a replay cannot repeat (`runtime::replay`). pub(crate) fn settled(ctx: &Context, owner: Option, id: Id, sizing: bool, place: Option, content: Option) -> Option { - use crate::runtime::measure::{Settle, count_memo_key_bytes, count_settle}; - // The 8 bytes of a keyed call's content version are the key fed in. + // Keyed calls feed their 8-byte content version into the key. + #[cfg(feature = "testing")] if content.is_some() { - count_memo_key_bytes(ctx, 8); + crate::runtime::measure::count_memo_key_bytes(ctx, 8); } - let size = decide_settled(ctx, owner, id, sizing, place, content); - count_settle(ctx, id, if size.is_some() { Settle::Replayed } else { Settle::Ran }); - size -} - -fn decide_settled(ctx: &Context, owner: Option, id: Id, sizing: bool, place: Option, content: Option) -> Option { let input = ctx.input(|input| { input.events.iter().any(|event| !matches!(event, egui::Event::PointerMoved(_) | egui::Event::MouseMoved(_) | egui::Event::PointerGone)) }); @@ -355,15 +350,23 @@ fn decide_settled(ctx: &Context, owner: Option, id: Id, sizing: bool, place: settled.map(|settled| (entry.sizes[kind].or(entry.sizes[1 - kind]).unwrap_or(settled.rect.size()), settled)) }); // The pointer over it, or a press that started on it, is read outside the store. - let (size, settled) = settled?; + let Some((size, settled)) = settled else { + #[cfg(feature = "testing")] + crate::runtime::measure::count_settle(ctx, id, crate::runtime::measure::Settle::Ran); + return None; + }; if engaged(ctx, &settled) { with(ctx, |store, pass_nr, _| { if let Some(entry) = store.entries.get_mut(&id) { entry.replays = Some((pass_nr, false)); } }); + #[cfg(feature = "testing")] + crate::runtime::measure::count_settle(ctx, id, crate::runtime::measure::Settle::Ran); return None; } + #[cfg(feature = "testing")] + crate::runtime::measure::count_settle(ctx, id, crate::runtime::measure::Settle::Replayed); Some(size) } diff --git a/crates/ui-contract/src/runtime/measure.rs b/crates/ui-contract/src/runtime/measure.rs index 5bed8a5e6..85afc682f 100644 --- a/crates/ui-contract/src/runtime/measure.rs +++ b/crates/ui-contract/src/runtime/measure.rs @@ -328,19 +328,15 @@ struct SettleLog { /// Counts the decision about the settling child `id` in this pass. Fed by `lazy::settled` (ran, replayed) and /// `lazy::decide` (out of view); a rename of that decision point moves both calls. +#[cfg(feature = "testing")] pub(crate) fn count_settle(ctx: &Context, id: Id, what: Settle) { - #[cfg(feature = "testing")] - { - let pass = ctx.cumulative_pass_nr(); - store::with::(ctx, |log| { - if log.pass != pass { - *log = SettleLog { pass, decisions: Vec::new() }; - } - log.decisions.push((id, what)); - }); - } - #[cfg(not(feature = "testing"))] - let _ = (ctx, id, what); + let pass = ctx.cumulative_pass_nr(); + store::with::(ctx, |log| { + if log.pass != pass { + *log = SettleLog { pass, decisions: Vec::new() }; + } + log.decisions.push((id, what)); + }); } /// The settled-test decisions of the last pass, by child id, in the order they were made; empty when that pass made none. @@ -377,12 +373,11 @@ fn last_totals(ctx: &Context, f: impl FnOnce(&PassTotals) -> R, none: R) -> R store::with::(ctx, |log| if log.pass == last { f(log) } else { none }) } -/// Counts `bytes` fed into a memo key in this pass. Fed by `MemoKey`. +/// Counts `bytes` fed into a memo key in this pass. Fed by `MemoKey`; until it exists, by `lazy::settled` (8 bytes +/// per call with a content version). +#[cfg(feature = "testing")] pub(crate) fn count_memo_key_bytes(ctx: &Context, bytes: usize) { - #[cfg(feature = "testing")] totals(ctx, |log| log.key_bytes += bytes); - #[cfg(not(feature = "testing"))] - let _ = (ctx, bytes); } /// The bytes fed into memo keys in the last pass. @@ -392,11 +387,9 @@ pub fn last_pass_memo_key_bytes(ctx: &Context) -> usize { } /// Counts a text layout handed to egui (`text_layout::layout`, `layout_with`) in this pass. +#[cfg(feature = "testing")] pub(crate) fn count_text_layout(ctx: &Context) { - #[cfg(feature = "testing")] totals(ctx, |log| log.text_layouts += 1); - #[cfg(not(feature = "testing"))] - let _ = ctx; } /// The text layouts handed to egui in the last pass. @@ -406,14 +399,12 @@ pub fn last_pass_text_layouts(ctx: &Context) -> u32 { } /// Counts a measure call of a leaf component of the type `name` in this pass. +#[cfg(feature = "testing")] pub(crate) fn count_measure_ask(ctx: &Context, name: &'static str) { - #[cfg(feature = "testing")] totals(ctx, |log| match log.asks.iter_mut().find(|(ty, _)| *ty == name) { Some((_, count)) => *count += 1, None => log.asks.push((name, 1)), }); - #[cfg(not(feature = "testing"))] - let _ = (ctx, name); } /// The leaf types that were asked to measure in the last pass, and how many times each.