diff --git a/crates/ui-contract/src/runtime/layout_memory.rs b/crates/ui-contract/src/runtime/layout_memory.rs index a54c1c05a..d692dc045 100644 --- a/crates/ui-contract/src/runtime/layout_memory.rs +++ b/crates/ui-contract/src/runtime/layout_memory.rs @@ -430,7 +430,7 @@ pub(crate) fn read(ctx: &Context, id: Id) -> Option { if crate::runtime::measure::active(ctx) { return peeking(ctx, |read| read(id)); } - journal_keep(ctx, id); + crate::runtime::replay::note_layout_keep(ctx, id); let moment = Moment::of(ctx); with(ctx, &moment, |memory| { let entry = memory.entries.entry(id).or_default(); @@ -459,7 +459,7 @@ pub(crate) fn peeking(ctx: &Context, f: impl FnOnce(&dyn Fn(Id) -> Option(ctx: &Context, body: Id, key: K) { - journal_keep(ctx, body); + crate::runtime::replay::note_layout_keep(ctx, body); let moment = Moment::of(ctx); with(ctx, &moment, |memory| { let Some(at) = memory.measuring.iter().rposition(|open| matches!(open, Open::Body { id, .. } if *id == body)) else { return }; @@ -978,7 +978,7 @@ pub(crate) fn kept_serves(ctx: &Context, b if !reuse_allowed(ctx) || !crate::runtime::measure::outermost(ctx) { return false; } - journal_keep(ctx, body); + crate::runtime::replay::note_layout_keep(ctx, body); let moment = Moment::of(ctx); if with(ctx, &moment, |memory| memory.probing()) { return false; @@ -1135,24 +1135,23 @@ pub(crate) fn ask_discard(ctx: &Context) { with(ctx, &moment, Entries::ask_discard); } -/// Notes in the run records open around the running code that a replay keeps the entry and the kept transcript of -/// `id`: the replayed run does not reach them, and an entry no pass touched in a frame is dropped. -fn journal_keep(ctx: &Context, id: Id) { - crate::runtime::replay::journal( - ctx, - || id, - |ctx, id| { - let moment = Moment::of(ctx); - with(ctx, &moment, |memory| { - if let Some(entry) = memory.entries.get_mut(id) { - entry.touched = moment.frame; - } - if let Some(kept) = memory.kept.get_mut(id) { - kept.touched = moment.frame; - } - }); - }, - ); +/// Keeps the entries and kept transcripts of `ids` through this frame, for a replayed run that kept them +/// (`replay::note_layout_keep`): the replay does not reach them, and an entry no pass touched in a frame is dropped. +pub(crate) fn keep(ctx: &Context, ids: &[Id]) { + if ids.is_empty() { + return; + } + let moment = Moment::of(ctx); + with(ctx, &moment, |memory| { + for id in ids { + if let Some(entry) = memory.entries.get_mut(id) { + entry.touched = moment.frame; + } + if let Some(kept) = memory.kept.get_mut(id) { + kept.touched = moment.frame; + } + } + }); } /// The end hook: asks egui for at most one discard pass per frame, and for a repaint when a change cannot get one, so diff --git a/crates/ui-contract/src/runtime/replay.rs b/crates/ui-contract/src/runtime/replay.rs index 502e1fab0..243ef3337 100644 --- a/crates/ui-contract/src/runtime/replay.rs +++ b/crates/ui-contract/src/runtime/replay.rs @@ -144,6 +144,8 @@ pub(crate) struct Facts { /// The components whose memory owners registered in the run (`memory::register_owner`): a replay needs their /// shown-tier state still kept (`memory::ended_since`). pub(crate) owners: Vec, + /// The layout memory entries the run kept (`note_layout_keep`): a replay keeps them (`layout_memory::keep`). + pub(crate) layout_keeps: Vec, /// The framework inputs the run read outside its props (`runtime::inputs`). pub(crate) inputs: Inputs, /// The command scope instances that registered in the run (`commands::register_instance`): a command queued for one @@ -214,6 +216,7 @@ pub(crate) fn close(ctx: &Context, layer: Layer) -> Record { outer.facts.blocked |= open.facts.blocked; outer.facts.discards |= open.facts.discards; outer.facts.owners.extend(open.facts.owners.iter().copied()); + outer.facts.layout_keeps.extend(open.facts.layout_keeps.iter().copied()); outer.facts.wake = min_wake(outer.facts.wake, open.facts.wake); outer.facts.reads.extend(open.facts.reads.iter().copied()); outer.facts.inputs.extend(open.facts.inputs); @@ -225,6 +228,9 @@ pub(crate) fn close(ctx: &Context, layer: Layer) -> Record { }); let egui = ctx.pass_records_since(&open.egui); let mut facts = open.facts; + // Every measure of an entry keeps it again: the record keeps each one once. + facts.layout_keeps.sort_unstable_by_key(|id| id.value()); + facts.layout_keeps.dedup(); // A paint callback's output depends on what its closure holds, which neither a view's key nor a comparison sees: // its run is never replayed. facts.blocked |= egui.layers().any(|on| on != layer) || egui.paints_callback(); @@ -306,10 +312,12 @@ pub(crate) fn replay(ctx: &Context, record: &Record, shift: &RecordShift) { if noting { pass_state::with::(ctx, |recorder| recorder.replaying = false); } + crate::runtime::layout_memory::keep(ctx, &record.facts.layout_keeps); if let Some(at) = record.facts.wake { crate::runtime::egui::repaint::request_after(ctx, crate::runtime::egui::pass::current_viewport(ctx), at - frame::clock(ctx)); } with_open(ctx, |facts| { + facts.layout_keeps.extend(record.facts.layout_keeps.iter().copied()); facts.reads.extend(record.facts.reads.iter().copied()); facts.inputs.extend(record.facts.inputs); facts.wake = min_wake(facts.wake, record.facts.wake); @@ -386,6 +394,12 @@ pub(crate) fn note_discard(ctx: &Context) { with_open(ctx, |facts| facts.discards = true); } +/// Notes that the running code kept the layout memory entry and kept transcript of `id`: a replay of the records open +/// around it keeps them too, as the replay reaches neither. +pub(crate) fn note_layout_keep(ctx: &Context, id: egui::Id) { + with_open(ctx, |facts| facts.layout_keeps.push(id)); +} + /// Notes that the memory owner `owner` registered in the running code. pub(crate) fn note_owner(ctx: &Context, owner: egui::Id) { with_open(ctx, |facts| facts.owners.push(owner)); diff --git a/crates/ui-widgets/tests/it/body/memo_view.rs b/crates/ui-widgets/tests/it/body/memo_view.rs index 3a8dea0f8..bba3f142c 100644 --- a/crates/ui-widgets/tests/it/body/memo_view.rs +++ b/crates/ui-widgets/tests/it/body/memo_view.rs @@ -15,8 +15,8 @@ use craft_ui::testing::input::key_press; use craft_ui::testing::layout::{in_region, measured_block}; use craft_ui::testing::pointer::{moved, press, release}; use craft_ui::testing::{ - Frames, MemoRun, last_frame_memo_runs, last_pass_memo_key_bytes, last_pass_memo_runs, last_pass_runs, last_pass_text_layouts, measuring_run, - trial, + Frames, MemoRun, last_frame_memo_runs, last_pass_measuring_runs, last_pass_memo_key_bytes, last_pass_memo_runs, last_pass_runs, + last_pass_text_layouts, measuring_run, trial, }; use egui::{Id, Key, Modifiers, Pos2, Rect, Ui, Vec2}; use std::cell::Cell; @@ -988,3 +988,47 @@ fn a_given_value_edit_runs_only_the_views_whose_props_hold_it() { assert!(runs.iter().all(|ran| ran.iter().all(|name| *name == "Level")), "only the level's view runs in a drag frame: {runs:?}"); assert!(runs[1..].iter().all(|ran| !ran.is_empty()), "the level's view runs in each drag frame that edits: {runs:?}"); } + +/// A view whose content is measured in a stack; `extra` is read by nothing inside, so a change of it runs the view and +/// leaves the stack's inputs as they were. +#[derive(MemoProps)] +struct Measured { + extra: u32, +} + +fn measured(props: &mut Measured, ui: &mut Ui) -> u32 { + Stack::new().show(ui, |ui, _| { + Text::new("A measured line").show(ui); + Text::new("Another measured line").show(ui); + }); + props.extra +} + +/// The memoized view `measured` with `extra`, in `STACK_ROOM`: the frame's measuring runs. +fn measured_frame(frames: &mut Frames, extra: u32) -> usize { + frames.frame(vec![], |ui| { + in_region(ui, STACK_ROOM, |ui| { + MemoView::new("measured", Measured { extra }, measured).show(ui); + }); + }); + last_pass_measuring_runs(&frames.ctx).len() +} + +/// A view that replays for frames keeps the layout memory its run kept: when it runs again with its content's inputs +/// unchanged, its content is measured as little as in a run right after a run. +#[trial] +fn a_view_replayed_for_frames_keeps_the_layout_memory_it_kept() { + let mut frames = Frames::new(); + for _ in 0..WARM { + measured_frame(&mut frames, 0); + } + let after_a_run = measured_frame(&mut frames, 1); + assert_eq!(runs_of(&frames, "Measured"), [MemoRun::Ran], "a new prop runs the view"); + for _ in 0..WARM + 3 { + measured_frame(&mut frames, 1); + assert_eq!(runs_of(&frames, "Measured"), [MemoRun::Replayed], "an idle frame replays the view"); + } + let after_replays = measured_frame(&mut frames, 2); + assert_eq!(runs_of(&frames, "Measured"), [MemoRun::Ran], "a new prop runs the view"); + assert_eq!(after_replays, after_a_run, "the replays kept the stack's measurements alive"); +}