From 8a7e1e6bea7ec2fbb37c42879063bf69c6e8e358 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 06:39:51 +0000 Subject: [PATCH] Add count-trial counters for settling, text layout and measure asks, and an idle-frame trial --- .../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 | 19 ++- crates/ui-contract/src/runtime/measure.rs | 116 ++++++++++++++++++ crates/ui-contract/src/testing/mod.rs | 5 +- crates/ui-widgets/tests/it/body/settle.rs | 72 +++++++---- 7 files changed, 190 insertions(+), 25 deletions(-) diff --git a/crates/ui-contract/src/contract/steps/measure.rs b/crates/ui-contract/src/contract/steps/measure.rs index 6293bebde..856259c7a 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); + 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 0170bb71d..e75201bd6 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), } + crate::runtime::measure::count_text_layout(ui.ctx()); ui.fonts_mut(|fonts| crate::runtime::egui::text_layout::layout_with(fonts, job)) }; let hint_text = hint.map(|hint| egui::RichText::new(hint).font(FontId::proportional(font_id.size))); diff --git a/crates/ui-contract/src/runtime/egui/text_layout.rs b/crates/ui-contract/src/runtime/egui/text_layout.rs index 113581b1b..1d64bf0df 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 { + 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 8cefd9fb5..7850311c1 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -240,7 +240,7 @@ fn with(ctx: &Context, f: impl FnOnce(&mut Store, u64, u64) -> R) -> R { /// pass gets the same answer, so a measuring run and the real run lay out the same children. pub(crate) fn decide(ctx: &Context, id: Id, view: &View, at: impl FnOnce(Vec2) -> Rect, sizing: bool, estimate: Vec2) -> Decision { let targets = crate::runtime::focus::targets(ctx); - with(ctx, |store, pass_nr, frame| { + let decision = with(ctx, |store, pass_nr, frame| { let entry = store.touch(Some(view.owner), id, frame); let kind = usize::from(!sizing); // A child the other kind of run showed last takes that run's extent. @@ -255,7 +255,11 @@ pub(crate) fn decide(ctx: &Context, id: Id, view: &View, at: impl FnOnce(Vec2) - } }; if skip { Decision::Skip(size) } else { Decision::Run } - }) + }); + if matches!(decision, Decision::Skip(_)) { + crate::runtime::measure::count_settle(ctx, id, crate::runtime::measure::Settle::OutOfView); + } + decision } /// Records what a run that showed the child `id` found: its extent in a run of this kind and, from a real run, what @@ -298,6 +302,17 @@ 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. + if content.is_some() { + 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)) }); diff --git a/crates/ui-contract/src/runtime/measure.rs b/crates/ui-contract/src/runtime/measure.rs index c281c04a5..5bed8a5e6 100644 --- a/crates/ui-contract/src/runtime/measure.rs +++ b/crates/ui-contract/src/runtime/measure.rs @@ -305,3 +305,119 @@ pub(crate) fn count_least(ctx: &Context, id: Id) { pub fn least_runs(ctx: &Context) -> Vec<(Id, u32)> { store::with::(ctx, |log| log.runs.clone()) } + +/// What a settling child's decision was. +#[cfg(feature = "testing")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Settle { + /// The child ran. + Ran, + /// The child's last run was replayed in place of running. + Replayed, + /// The child is out of view and took its last extent without running. + OutOfView, +} + +/// The settled-test decisions of one pass. +#[cfg(feature = "testing")] +#[derive(Default)] +struct SettleLog { + pass: u64, + decisions: Vec<(Id, Settle)>, +} + +/// 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. +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); +} + +/// The settled-test decisions of the last pass, by child id, in the order they were made; empty when that pass made none. +#[cfg(feature = "testing")] +pub fn last_pass_settles(ctx: &Context) -> Vec<(Id, Settle)> { + let last = ctx.cumulative_pass_nr().saturating_sub(1); + store::with::(ctx, |log| if log.pass == last { log.decisions.clone() } else { Vec::new() }) +} + +/// Bytes fed into memo keys in one pass, and the layouts and measure asks of that pass. +#[cfg(feature = "testing")] +#[derive(Default)] +struct PassTotals { + pass: u64, + key_bytes: usize, + text_layouts: u32, + asks: Vec<(&'static str, u32)>, +} + +#[cfg(feature = "testing")] +fn totals(ctx: &Context, f: impl FnOnce(&mut PassTotals) -> R) -> R { + let pass = ctx.cumulative_pass_nr(); + store::with::(ctx, |log| { + if log.pass != pass { + *log = PassTotals { pass, ..PassTotals::default() }; + } + f(log) + }) +} + +#[cfg(feature = "testing")] +fn last_totals(ctx: &Context, f: impl FnOnce(&PassTotals) -> R, none: R) -> R { + let last = ctx.cumulative_pass_nr().saturating_sub(1); + 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`. +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. +#[cfg(feature = "testing")] +pub fn last_pass_memo_key_bytes(ctx: &Context) -> usize { + last_totals(ctx, |log| log.key_bytes, 0) +} + +/// Counts a text layout handed to egui (`text_layout::layout`, `layout_with`) in this pass. +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. +#[cfg(feature = "testing")] +pub fn last_pass_text_layouts(ctx: &Context) -> u32 { + last_totals(ctx, |log| log.text_layouts, 0) +} + +/// Counts a measure call of a leaf component of the type `name` in this pass. +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. +#[cfg(feature = "testing")] +pub fn last_pass_measure_asks(ctx: &Context) -> Vec<(&'static str, u32)> { + last_totals(ctx, |log| log.asks.clone(), Vec::new()) +} diff --git a/crates/ui-contract/src/testing/mod.rs b/crates/ui-contract/src/testing/mod.rs index 90ed467df..9dcb90e6a 100644 --- a/crates/ui-contract/src/testing/mod.rs +++ b/crates/ui-contract/src/testing/mod.rs @@ -41,8 +41,9 @@ pub use crate::contract::{Exemption, PatternKind, PatternProbe, Probe, Registrat #[cfg(feature = "testing")] pub use crate::runtime::layout_memory::forget_kept as forget_measurements; pub use crate::runtime::measure::{ - active as is_measuring, body_runs as measuring_runs, contributions as measured_contributions, last_pass_body_runs as last_pass_measuring_runs, - last_pass_runs, least_runs as shrink_least_runs, row_measures, run as measuring_run, take_effects, widest_runs, + Settle, active as is_measuring, body_runs as measuring_runs, contributions as measured_contributions, + last_pass_body_runs as last_pass_measuring_runs, last_pass_measure_asks, last_pass_memo_key_bytes, last_pass_runs, last_pass_settles, + last_pass_text_layouts, least_runs as shrink_least_runs, row_measures, run as measuring_run, take_effects, widest_runs, }; pub use craft_ui_macros::{story, trial}; pub use fixture::{Fixture, Impure, Room, Target}; diff --git a/crates/ui-widgets/tests/it/body/settle.rs b/crates/ui-widgets/tests/it/body/settle.rs index f7dae2c36..317b8d902 100644 --- a/crates/ui-widgets/tests/it/body/settle.rs +++ b/crates/ui-widgets/tests/it/body/settle.rs @@ -13,7 +13,7 @@ use craft_ui::runtime::commands::{CommandInput, CommandScope, CommandState, Comm use craft_ui::runtime::{frame, inspect}; use craft_ui::testing::layout::in_region; use craft_ui::testing::pointer::{moved, press, release}; -use craft_ui::testing::{Fixture, Frames, last_pass_measuring_runs, trial}; +use craft_ui::testing::{Fixture, Frames, Settle, last_pass_measuring_runs, last_pass_runs, last_pass_settles, last_pass_text_layouts, trial}; use egui::{Id, Pos2, Rect, Ui, Vec2}; use std::cell::{Cell, RefCell}; @@ -36,6 +36,8 @@ struct Scene { versions: [Cell>; 2], repaints: Cell, app_repaints: Cell, + /// Whether the scene shows only what is outside the settling children. + bare: Cell, runs: [Cell; 2], buttons: [Cell<(Id, Rect)>; 2], seen: Seen, @@ -51,6 +53,7 @@ impl Scene { versions: Default::default(), repaints: Cell::new(false), app_repaints: Cell::new(false), + bare: Cell::new(false), runs: Default::default(), buttons: [Cell::new((Id::NULL, Rect::NOTHING)), Cell::new((Id::NULL, Rect::NOTHING))], seen: Seen::default(), @@ -73,28 +76,30 @@ impl Scene { if self.editor_first { self.editor(ui); } - in_region(ui, Rect::from_min_size(ORIGIN, self.room.get()), |ui| { - self.child("reader", 0).show(ui, |ui| { - self.runs[0].set(self.runs[0].get() + 1); - let response = Button::new("Alpha").show(ui).response; - self.buttons[0].set((response.id, response.rect)); - Scrub::new(Bind::bound(&mut *self.value.borrow_mut())).id_salt("reader").seen(&self.seen).show_at(ui, READER); - }); - self.child("other", 1).show(ui, |ui| { - self.runs[1].set(self.runs[1].get() + 1); - if self.repaints.get() { - frame::request_repaint(ui.ctx()); - } - // In a body, whose measure a run that is not settled takes from what its last run kept. - Stack::new().show(ui, |ui, _| { - let response = Button::new("Beta").show(ui).response; - self.buttons[1].set((response.id, response.rect)); + if !self.bare.get() { + in_region(ui, Rect::from_min_size(ORIGIN, self.room.get()), |ui| { + self.child("reader", 0).show(ui, |ui| { + self.runs[0].set(self.runs[0].get() + 1); + let response = Button::new("Alpha").show(ui).response; + self.buttons[0].set((response.id, response.rect)); + Scrub::new(Bind::bound(&mut *self.value.borrow_mut())).id_salt("reader").seen(&self.seen).show_at(ui, READER); + }); + self.child("other", 1).show(ui, |ui| { + self.runs[1].set(self.runs[1].get() + 1); + if self.repaints.get() { + frame::request_repaint(ui.ctx()); + } + // In a body, whose measure a run that is not settled takes from what its last run kept. + Stack::new().show(ui, |ui, _| { + let response = Button::new("Beta").show(ui).response; + self.buttons[1].set((response.id, response.rect)); + }); + if self.spinner { + Spinner::new().show(ui); + } }); - if self.spinner { - Spinner::new().show(ui); - } }); - }); + } if !self.editor_first { self.editor(ui); } @@ -146,6 +151,31 @@ fn a_settled_child_does_not_run_and_still_paints() { } } +/// After two warm-up frames, an idle frame with no input runs nothing that settles: every keyed child replays, the +/// components that run are those outside every settling child, the frame takes one pass and lays out no text. +#[trial] +fn an_idle_frame_runs_nothing_that_settles() { + let scene = Scene::new(false, false); + scene.versions.iter().for_each(|version| version.set(Some(1))); + let mut frames = Frames::new(); + scene.settle(&mut frames); + let reference = Scene::new(false, false); + reference.bare.set(true); + let mut bare = Frames::new(); + reference.settle(&mut bare); + let outside = { + reference.frame(&mut bare, vec![]); + last_pass_runs(&bare.ctx).iter().map(|(_, runs)| runs).sum::() + }; + let passes = frames.frame(vec![], |ui| scene.show(ui)); + let ran = last_pass_settles(&frames.ctx).into_iter().filter(|(_, what)| *what == Settle::Ran).count(); + let runs = last_pass_runs(&frames.ctx); + assert_eq!(ran, 0, "an idle frame ran keyed children: {:?}", last_pass_settles(&frames.ctx)); + assert_eq!(runs.iter().map(|(_, runs)| runs).sum::(), outside, "components ran inside settling children: {runs:?}"); + assert_eq!(passes, 1, "an idle frame takes one pass"); + assert_eq!(last_pass_text_layouts(&frames.ctx), 0, "an idle frame laid out text"); +} + /// The pointer over a settled child runs it, and only it, in the frame it arrives, from the measures its last run /// kept; leaving, the child runs until its hover has faded out, then settles again. #[trial]