Check a kept transcript's leaf calls against the leaf's own answers

A real run under a kept transcript compared each kept measure call by asking
the component again, though step 5 had just asked it the same call. The check
now takes the calls the leaf's run made: a kept call at the same proposal and
room that read no inputs compares with that answer, and only other calls are
asked again. The check reads the transcript's calls in place instead of
cloning them and their inputs per leaf, and a leaf hands its ideal and minimum
calls over in one leaf_call.
This commit is contained in:
aiongg
2026-11-12 14:32:39 +00:00
parent cb33c14ff2
commit d4da861dad
4 changed files with 121 additions and 60 deletions
+29 -19
View File
@@ -14,6 +14,7 @@ use crate::primitives::sensing::{self, MemberSpec, PartDecl, RootSpec};
use crate::primitives::{FaceRecord, PaintRecord, PartResult, face_id};
use crate::runtime::access::{self, Politeness, Role, SetPosition, scope};
use crate::runtime::env::{AppValues, Env, EnvView};
use crate::runtime::layout_memory::Call;
use crate::runtime::memory::{MemoryStage, sub};
use crate::runtime::overlay::CloseReason;
use crate::runtime::reads::{self, Sample};
@@ -972,28 +973,36 @@ fn run<C: Component>(mut comp: C, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>
steps::measure(comp, &input, &style, &resolved, shown.as_deref(), extent)
};
let (size, inputs) = ask(&mut comp, Proposal::Ideal, room, extent);
// A leaf's measure calls are what its body's layout takes from it: a measuring run keeps them in the body's
// transcript, and a real run its kept transcript serves asks them again. A leaf shown at a rect takes no room in
// the flow: its body's layout takes the rect, and any measure its caller sized the rect by records itself.
let leaf = shape == ShapeKind::Leaf && matches!(at, ShowAt::Flow) && (!sizing || crate::runtime::measure::active(&ctx));
if leaf {
crate::runtime::layout_memory::leaf_call(&ctx, id, Proposal::Ideal, room, inputs, size, |proposal, room| {
ask(&mut comp, proposal, room, extent).0
});
}
trace.mark(Step::Measure);
let ideal = Call { proposal: Proposal::Ideal, room, inputs, took: size };
// A control in a label scope reports its narrowest width, in a measuring run too: the real run's label step reads
// it to decide whether the row fits the label beside it.
let min = (shape == ShapeKind::Leaf && scope::in_label(&ctx)).then(|| {
let (min, inputs) = ask(&mut comp, Proposal::Min, None, Vec2::ZERO);
if leaf {
crate::runtime::layout_memory::leaf_call(&ctx, id, Proposal::Min, None, inputs, min, |proposal, room| {
ask(&mut comp, proposal, room, Vec2::ZERO).0
});
}
scope::record_control_min(&ctx, min.x);
min
let (took, inputs) = ask(&mut comp, Proposal::Min, None, Vec2::ZERO);
Call { proposal: Proposal::Min, room: None, inputs, took }
});
let min_took = min.as_ref().map(|min| min.took);
// A leaf's measure calls are what its body's layout takes from it: a measuring run keeps them in the body's
// transcript, and a real run its kept transcript serves checks them against this run's. A leaf shown at a rect
// takes no room in the flow: its body's layout takes the rect, and any measure its caller sized the rect by
// records itself.
let leaf = shape == ShapeKind::Leaf && matches!(at, ShowAt::Flow) && (!sizing || crate::runtime::measure::active(&ctx));
if leaf {
let mut both;
let mut one;
let calls: &mut [Call] = match min {
Some(min) => {
both = [ideal, min];
&mut both
}
None => {
one = [ideal];
&mut one
}
};
crate::runtime::layout_memory::leaf_call(&ctx, id, calls, |proposal, room| ask(&mut comp, proposal, room, extent).0);
}
trace.mark(Step::Measure);
let min = min_took.inspect(|min| scope::record_control_min(&ctx, min.x));
// The read record: the binding step 4 read, and the sizes step 5 made from it.
if let Some(key) = comp.bind().and_then(|bind| bind.key(id)) {
let ideal = Sample { proposal: Proposal::Ideal, room, took: size };
@@ -1218,7 +1227,8 @@ fn measure_shape<C: Component>(
};
let (took, inputs) = ask(comp, proposal);
if hooks.shape() == ShapeKind::Leaf && (!ui.is_sizing_pass() || crate::runtime::measure::active(ui.ctx())) {
crate::runtime::layout_memory::leaf_call(ui.ctx(), id, proposal, None, inputs, took, |proposal, _| ask(comp, proposal).0);
let calls = &mut [Call { proposal, room: None, inputs, took }];
crate::runtime::layout_memory::leaf_call(ui.ctx(), id, calls, |proposal, _| ask(comp, proposal).0);
}
if let Some(key) = comp.bind().and_then(|bind| bind.key(id)) {
reads::note(ui.ctx(), key, id, true, &[Sample { proposal, room: None, took }]);
+40 -36
View File
@@ -102,13 +102,13 @@ enum Item {
/// The inputs outside a measure that it read (`read_input`), by key: the widest value of each.
pub(crate) type Inputs = HashMap<Id, f32>;
/// One measure call a component got in a measuring run, the inputs besides its room it read, and its answer.
/// One measure call a component got, the inputs besides its room it read, and its answer.
#[derive(Clone)]
struct Call {
proposal: Proposal,
room: Option<f32>,
inputs: Inputs,
took: Vec2,
pub(crate) struct Call {
pub proposal: Proposal,
pub room: Option<f32>,
pub inputs: Inputs,
pub took: Vec2,
}
/// What the components of a body's content got from its measuring run: each measure call, and whether it filled the
@@ -200,8 +200,9 @@ struct Entries {
served: HashMap<Id, Arc<Transcript>>,
measuring: Vec<Open>,
scopes: Vec<Scope>,
/// The inputs of the kept calls a real run asks again, innermost last: `read_input` gives what they read.
replaying: Vec<Inputs>,
/// The kept calls a real run asks again, as the transcript, the component and the call's index, innermost last:
/// `read_input` gives what they read.
replaying: Vec<(Arc<Transcript>, Id, usize)>,
ask: bool,
asked: Option<u64>,
}
@@ -418,7 +419,8 @@ pub(crate) fn calls_from(ctx: &Context) -> usize {
pub(crate) fn read_input(ctx: &Context, from: usize, key: Id, value: f32) -> f32 {
let moment = Moment::of(ctx);
with(ctx, &moment, |memory| {
if let Some(read) = memory.replaying.iter().rev().find_map(|inputs| inputs.get(&key)) {
let replayed = memory.replaying.iter().rev().find_map(|(transcript, id, at)| transcript.leaves.get(id)?.calls.get(*at)?.inputs.get(&key));
if let Some(read) = replayed {
return *read;
}
for open in memory.measuring.iter_mut().skip(from) {
@@ -504,53 +506,55 @@ pub(crate) fn boundary<R>(ctx: &Context, stops_changes: bool, f: impl FnOnce() -
out
}
/// A measure call component `id` got, with the `inputs` it read (`measure_call`) and its answer `took`. A measuring
/// run adds it to the innermost body's transcript. A real run inside a body whose records a kept transcript gave asks
/// the component again (`ask`) at each call the transcript holds for it, under the inputs that call read, once per
/// pass, and compares; `ask` runs outside the memory's lock.
pub(crate) fn leaf_call(
ctx: &Context,
id: Id,
proposal: Proposal,
room: Option<f32>,
inputs: Inputs,
took: Vec2,
ask: impl FnMut(Proposal, Option<f32>) -> Vec2,
) {
/// The measure calls component `id` got in this run (`calls`: each with the inputs it read, `measure_call`, and its
/// answer). A measuring run adds them to the innermost body's transcript. A real run inside a body whose records a
/// kept transcript gave checks each call the transcript holds for the component, once per pass: a kept call this run
/// made too (same proposal and room, no inputs read besides the room) compares with this run's answer, and any other
/// is asked again (`ask`) under the inputs it read. `ask` runs outside the memory's lock.
pub(crate) fn leaf_call(ctx: &Context, id: Id, calls: &mut [Call], ask: impl FnMut(Proposal, Option<f32>) -> Vec2) {
let moment = Moment::of(ctx);
if !real(ctx) {
with(ctx, &moment, |memory| {
if let Some(leaves) = memory.leaf() {
let calls = &mut leaves.entry(id).or_default().calls;
if !calls.iter().any(|call| call.proposal == proposal && call.room == room) {
calls.push(Call { proposal, room, inputs, took });
let kept = &mut leaves.entry(id).or_default().calls;
for call in calls {
if !kept.iter().any(|kept| kept.proposal == call.proposal && kept.room == call.room) {
let inputs = std::mem::take(&mut call.inputs);
kept.push(Call { proposal: call.proposal, room: call.room, inputs, took: call.took });
}
}
}
});
return;
}
check(ctx, &moment, id, ask);
check(ctx, &moment, id, calls, ask);
}
fn check(ctx: &Context, moment: &Moment, id: Id, mut ask: impl FnMut(Proposal, Option<f32>) -> Vec2) {
let calls = with(ctx, moment, |memory| {
fn check(ctx: &Context, moment: &Moment, id: Id, made: &[Call], mut ask: impl FnMut(Proposal, Option<f32>) -> Vec2) {
let transcript = with(ctx, moment, |memory| {
let scope = memory.checking()?;
if !scope.visited.insert(id) {
return None;
}
let transcript = scope.served.clone()?;
Some(transcript.leaves.get(&id).map(|leaf| leaf.calls.clone()))
scope.served.clone()
});
let Some(calls) = calls else { return };
let Some(transcript) = transcript else { return };
let tolerance = measure_tolerance(crate::runtime::egui::scale::pixels_per_point(ctx));
// A component the transcript does not hold was added; one that answers differently under the inputs it read then
// changed. An input that changed since (a shared column a later row widened in the measuring run's sweep) is not a
// change of this component: the input's own records and reports are checked where they are made.
let same = calls.is_some_and(|calls| {
calls.iter().all(|call| {
with(ctx, moment, |memory| memory.replaying.push(call.inputs.clone()));
let now = ask(call.proposal, call.room);
with(ctx, moment, |memory| memory.replaying.pop());
let same = transcript.leaves.get(&id).is_some_and(|leaf| {
leaf.calls.iter().enumerate().all(|(at, call)| {
let made = made.iter().find(|made| call.inputs.is_empty() && made.proposal == call.proposal && made.room == call.room);
let now = match made {
Some(made) => made.took,
None => {
with(ctx, moment, |memory| memory.replaying.push((transcript.clone(), id, at)));
let now = ask(call.proposal, call.room);
with(ctx, moment, |memory| memory.replaying.pop());
now
}
};
(now - call.took).abs().max_elem() <= tolerance
})
});
+13 -1
View File
@@ -9,6 +9,8 @@ use crate::runtime::memory::sub;
use crate::style::{ComponentStyle, StyleCx};
use craft_behavior::{Metrics, Sense};
use egui::{Id, Rect, Response, Ui, Vec2};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
/// Runs a body laid out by `layout` under `key` and places one child per entry of `sizes` in flow, as step 6
/// does. Returns each child's rect.
@@ -70,12 +72,19 @@ pub struct Block {
#[common]
common: Common,
size: Vec2,
measures: Option<Arc<AtomicUsize>>,
}
impl Block {
/// A child that takes `size`.
pub const fn new(size: Vec2) -> Self {
Block { common: Common::new(), size }
Block { common: Common::new(), size, measures: None }
}
/// Counts each call of its measure in `measures`.
pub fn counted(mut self, measures: &Arc<AtomicUsize>) -> Self {
self.measures = Some(measures.clone());
self
}
}
@@ -98,6 +107,9 @@ impl Component for Block {
}
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
if let Some(measures) = &self.measures {
measures.fetch_add(1, Ordering::Relaxed);
}
self.size
}
+39 -4
View File
@@ -1,10 +1,15 @@
//! `LayoutMemory`: the tolerance rule and size tweens.
//! `LayoutMemory`: the tolerance rule, size tweens and the check of a kept transcript.
use craft_behavior::ColumnWidth;
use craft_ui::contract::{ContainerProps, Props as _, Show as _, ShowContainer as _, Sizing};
use craft_ui::layout::BodyLayout;
use craft_ui::testing::layout::{body, measured_block};
use craft_ui::testing::{Frames, trial};
use egui::Vec2;
use craft_ui::testing::layout::{Block, body, measured_block};
use craft_ui::testing::{Frames, last_pass_measuring_runs, trial};
use craft_ui_test_kit::body::Slab;
use egui::{Rect, Ui, Vec2};
use std::cell::Cell;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
static COLUMNS: [ColumnWidth; 2] = [ColumnWidth::Content, ColumnWidth::Content];
@@ -38,3 +43,33 @@ fn continuous_change_runs_no_discard_and_a_settled_change_runs_one() {
assert_eq!(run(&mut frames, LONGER), twice, "a change after a settled frame runs a discard pass, for both blocks together");
assert_eq!(run(&mut frames, WITHIN_TOLERANCE), once, "a change under half a point is the same size");
}
/// A real run under a kept transcript checks each leaf against the answer the leaf's own run just gave, when the
/// transcript's call is at the same proposal and room: a settled frame measures each leaf once, and a leaf that
/// changes under the same body inputs shows its new size in that frame.
#[trial]
fn a_settled_body_measures_each_leaf_once_and_a_changed_leaf_shows_in_its_frame() {
const LEAVES: usize = 3;
let measures = Arc::new(AtomicUsize::new(0));
let size = Cell::new(Vec2::new(80.0, 20.0));
let last = Cell::new(Rect::NOTHING);
let show = |ui: &mut Ui| {
let at = Rect::from_min_size(ui.cursor().min, Vec2::new(300.0, 200.0));
let _ = Slab::new(ContainerProps::new().sizing(Sizing::Fill)).id_salt("slab").show_at(ui, at, |ui, _| {
for _ in 0..LEAVES {
Block::new(Vec2::new(60.0, 20.0)).counted(&measures).show(ui);
}
last.set(Block::new(size.get()).show(ui).rect);
});
};
let mut frames = Frames::new();
frames.idle(4, show);
measures.store(0, Ordering::Relaxed);
assert_eq!(frames.frame(vec![], show), 1, "a settled frame runs one pass");
let runs = last_pass_measuring_runs(&frames.ctx);
assert!(runs.is_empty(), "the body takes its kept transcript: {runs:?}");
assert_eq!(measures.load(Ordering::Relaxed), LEAVES, "each leaf is measured once, in its own run");
size.set(Vec2::new(140.0, 20.0));
frames.frame(vec![], show);
assert!((last.get().width() - 140.0).abs() < 0.5, "the changed leaf shows its new size in its frame: {:?}", last.get());
}