From c07bd78e0acfb7e82c4acf932e710ebb99254404 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 17:30:50 +0000 Subject: [PATCH] fwstub-3: appeared() is true again on the first frame a component is shown after a frame of its viewport without it (a page left and reopened inside the eviction grace), through that frame's passes, as WordCraft told a reopen by a gap in the frames drawn; trial Co-Authored-By: Claude Opus 5.5 --- crates/ui-contract/src/runtime/egui/memory.rs | 13 +++--- crates/ui-contract/src/runtime/memory.rs | 41 +++++++++++++++---- crates/ui-contract/tests/it/runtime/memory.rs | 30 ++++++++++++++ 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/memory.rs b/crates/ui-contract/src/runtime/egui/memory.rs index 33f852545..94656f023 100644 --- a/crates/ui-contract/src/runtime/egui/memory.rs +++ b/crates/ui-contract/src/runtime/egui/memory.rs @@ -46,13 +46,12 @@ pub(crate) fn peek_copy(ctx: &Context, id: Id) -> O ctx.data(|data| data.get_temp::(id)) } -/// Stores `mark` under `id` unless a value of its type is there already; true when it stored it. -pub(crate) fn mark_seen(data: &mut Map, id: Id, mark: T) -> bool { - let absent = data.get_temp::(id).is_none(); - if absent { - data.insert_temp(id, mark); - } - absent +/// Stores `mark(None)` under `id`, or `mark(Some(old))` over the value of its type there already; true when there +/// was none. `T` is a plain mark: `Copy`, so no slot code runs under the data lock. +pub(crate) fn mark_seen(data: &mut Map, id: Id, mark: impl FnOnce(Option) -> T) -> bool { + let old = data.get_temp::(id); + data.insert_temp(id, mark(old)); + old.is_none() } /// Puts a taken-out box back under `id`, in the allocation it left in. The value it replaced, if any, is returned diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index 97f85fb67..c96cd7450 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -98,7 +98,8 @@ pub trait Remember { /// The result of the job `start` returns, one per `key`: it is submitted once, cancelled when the owner is /// evicted, and a landed result asks for a pass. fn pending(&mut self, key: K, start: impl FnOnce() -> Job) -> &Pending; - /// True on the first pass a shown stage exists for this id. + /// True on the first pass a shown stage exists for this id, and on the first pass after the id was left unshown + /// for a frame of its viewport (a page left and opened again), through that frame's passes. fn appeared(&self) -> bool; } @@ -222,10 +223,35 @@ pub(crate) struct Registry { written: usize, } -/// Marks a base whose stage was written back: the frame it was first shown in, for `appeared`. A discard pass -/// repeats the frame, so `appeared` stays true through it. +/// Marks a base whose stage was written back, for `appeared`: the frame it appeared in and the last frame it was +/// shown in. A discard pass repeats the frame, so `appeared` stays true through it. A frame of its viewport that did +/// not show the base (a page left and opened again before its owner was evicted) makes its next showing an +/// appearance again, as WordCraft told a reopened page by a gap in the frames it was drawn in. #[derive(Clone, Copy)] -struct Seen(u64); +struct Seen { + first: u64, + last: u64, +} + +impl Seen { + /// Whether the frames since the base was last shown leave a gap before `frame`. + fn gap_before(self, frame: u64) -> bool { + self.last.saturating_add(1) < frame + } + + /// Whether a stage of this base in `frame` is the base appearing. + fn appears(self, frame: u64) -> bool { + self.first == frame || self.gap_before(frame) + } + + /// The mark after a write-back in `frame`. + fn shown(old: Option, frame: u64) -> Seen { + match old { + Some(seen) if !seen.gap_before(frame) => Seen { first: seen.first, last: frame.max(seen.last) }, + _ => Seen { first: frame, last: frame }, + } + } +} /// Registers `owner`'s pass, at step 3: eviction tests only owners. Shown entries of an owner not registered /// for 3 passes of the viewport that last showed it and 0.5 s are evicted by `evict`. @@ -495,14 +521,15 @@ impl MemoryStage { self.owner } - /// True for the first pass a base id is shown; per base id, so a body and its component differ. + /// True for the first pass a base id is shown, and for the first after a frame that did not show it; per base id, + /// so a body and its component differ. pub(crate) fn appeared_at(&self, base: Id) -> bool { let mut touched = self.touched.borrow_mut(); if !touched.contains(&base) { touched.push(base); } let frame = pass::frame_nr(&self.ctx, pass::current_viewport(&self.ctx)); - store::peek_copy::(&self.ctx, base).is_none_or(|seen| seen.0 == frame) + store::peek_copy::(&self.ctx, base).is_none_or(|seen| seen.appears(frame)) } #[allow(clippy::unreachable, reason = "the entry key holds the lifetime and the type, so the downcast cannot fail")] @@ -650,7 +677,7 @@ impl MemoryStage { replaced.extend((entry.write_back)(entry.value, data, id)); } for base in touched { - if store::mark_seen(data, base, Seen(frame)) || retags { + if store::mark_seen(data, base, |old| Seen::shown(old, frame)) || retags { record.push(((base, SHOWN, TypeId::of::()), take_raw::)); } } diff --git a/crates/ui-contract/tests/it/runtime/memory.rs b/crates/ui-contract/tests/it/runtime/memory.rs index 22b250086..351a3c9f0 100644 --- a/crates/ui-contract/tests/it/runtime/memory.rs +++ b/crates/ui-contract/tests/it/runtime/memory.rs @@ -112,6 +112,36 @@ fn set_saved_value_is_read_back_by_saved_value_and_stored_once() { assert_eq!(store.writes(), 2, "a changed value is stored once"); } +/// A component left unshown for a frame and shown again well inside the eviction grace (a backstage page left and +/// reopened) appears again through every pass of the frame it comes back in, and only then; its shown entries stay. +#[trial] +fn a_component_shown_again_after_a_frame_without_it_appears_again() { + let mut frames = Frames::new(); + let id = global("page", ()); + let passes = RefCell::new(Vec::::new()); + let mut show = |ui: &mut Ui| { + let mut stage = Stage::open(ui.ctx(), id); + passes.borrow_mut().push(stage.appeared()); + *stage.memory::() += 1; + stage.finish(); + }; + frames.frame(vec![], &mut show); + frames.frame(vec![], &mut show); + assert_eq!(passes.borrow().last(), Some(&false), "a component shown every frame appears once"); + frames.frame(vec![], |_| {}); + passes.borrow_mut().clear(); + frames.frame(vec![], &mut show); + let back = passes.take(); + assert!(!back.is_empty() && back.iter().all(|&appeared| appeared), "every pass of the frame it is shown again in: {back:?}"); + frames.frame(vec![], |ui| { + let mut stage = Stage::open(ui.ctx(), id); + passes.borrow_mut().push(stage.appeared()); + assert!(*stage.memory::() >= 3, "the shown entry stayed inside the grace"); + stage.finish(); + }); + assert_eq!(passes.take(), vec![false], "the next frame is no appearance"); +} + /// The lifetimes of 7.2, through `Remember`: shown entries go with their owner; session and saved ones only /// through `forget` and `forget_saved`; a press entry goes when its press ends. #[trial]