Add count-trial counters for settling, text layout and measure asks, and an idle-frame trial

This commit is contained in:
aiongg committed 2026-11-19 08:05:00 +00:00
1 parent 3a815c9657
commit d06fd33dfb
7 files changed
+190 -25

No files matched your search

@@ -27,6 +27,7 @@ pub(crate) fn measure_content<C: Component>(
value: Option<&ValueOf<C>>,
) -> (Vec2, Inputs) {
let cx = measure_cx::<C>(input, style, resolved, value);
crate::runtime::measure::count_measure_ask(input.ui.ctx(), std::any::type_name::<C>());
layout_memory::measure_call(input.ui.ctx(), || comp.measure(&cx))
}
@@ -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)));
@@ -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<Galley> {
crate::runtime::measure::count_text_layout(ctx);
ctx.fonts_mut(|fonts| layout_with(fonts, job))
}
+17 -2
View File
@@ -240,7 +240,7 @@ fn with<R>(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: Id, sizing: bool, place: Option<Place>, content: Option<u64>) -> Option<Vec2> {
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: Id, sizing: bool, place: Option<Place>, content: Option<u64>) -> Option<Vec2> {
let input = ctx.input(|input| {
input.events.iter().any(|event| !matches!(event, egui::Event::PointerMoved(_) | egui::Event::MouseMoved(_) | egui::Event::PointerGone))
});
+116
View File
@@ -305,3 +305,119 @@ pub(crate) fn count_least(ctx: &Context, id: Id) {
pub fn least_runs(ctx: &Context) -> Vec<(Id, u32)> {
store::with::<LeastRuns, _>(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::<SettleLog, _>(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::<SettleLog, _>(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<R>(ctx: &Context, f: impl FnOnce(&mut PassTotals) -> R) -> R {
let pass = ctx.cumulative_pass_nr();
store::with::<PassTotals, _>(ctx, |log| {
if log.pass != pass {
*log = PassTotals { pass, ..PassTotals::default() };
}
f(log)
})
}
#[cfg(feature = "testing")]
fn last_totals<R>(ctx: &Context, f: impl FnOnce(&PassTotals) -> R, none: R) -> R {
let last = ctx.cumulative_pass_nr().saturating_sub(1);
store::with::<PassTotals, _>(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())
}
+3 -2
View File
@@ -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};
+51 -21
View File
@@ -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<Option<u64>>; 2],
repaints: Cell<bool>,
app_repaints: Cell<bool>,
/// Whether the scene shows only what is outside the settling children.
bare: Cell<bool>,
runs: [Cell<usize>; 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::<u32>()
};
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::<u32>(), 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]