diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index b74db5d05..875edfcf6 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -149,6 +149,9 @@ struct Registry { slots: FastMap<(Id, u8, TypeId), SlotRecord>, /// Evicted slots, moved out inside the end hook; the first pipeline run of the next pass drops them. graveyard: Vec, + /// Slot records write-back has written, for the count trials. + #[cfg(feature = "testing")] + written: usize, } /// Marks a base whose stage was written back: the frame it was first shown in, for `appeared`. A discard pass @@ -244,6 +247,12 @@ pub(crate) fn dispose_shown(ctx: &Context, id: Id) { } } +/// How many slot records write-back has written in `ctx` so far. +#[cfg(feature = "testing")] +pub(crate) fn records_written(ctx: &Context) -> usize { + Service::::run(ctx, |reg| reg.written) +} + /// The pass of the viewport that last registered `owner` at step 3, `None` once the owner is evicted. pub(crate) fn owner_pass(ctx: &Context, owner: Id) -> Option { Service::::run(ctx, |reg| reg.owners.get(&owner).map(|record| record.pass)) @@ -504,6 +513,10 @@ impl MemoryStage { for key in &ended { reg.slots.remove(key); } + #[cfg(feature = "testing")] + { + reg.written += record.len(); + } for (key, remove) in record { reg.slots.insert(key, SlotRecord { owner, remove }); } diff --git a/crates/ui-contract/src/testing/memory.rs b/crates/ui-contract/src/testing/memory.rs index 2a2de9061..f61acd471 100644 --- a/crates/ui-contract/src/testing/memory.rs +++ b/crates/ui-contract/src/testing/memory.rs @@ -63,6 +63,11 @@ pub fn forget_saved(ctx: &Context, key: &SaveKey) { memory::forget_saved(ctx, key); } +/// How many slot records the stages' write-backs have written in `ctx` so far. +pub fn records_written(ctx: &Context) -> usize { + memory::records_written(ctx) +} + /// Drops the slots the end hook evicted. pub fn dispose_evicted(ctx: &Context) { memory::dispose_evicted(ctx); diff --git a/crates/ui-contract/tests/it/runtime/memory.rs b/crates/ui-contract/tests/it/runtime/memory.rs index 21832faf6..6d69bc4c3 100644 --- a/crates/ui-contract/tests/it/runtime/memory.rs +++ b/crates/ui-contract/tests/it/runtime/memory.rs @@ -1,7 +1,7 @@ //! Memory lifetimes, eviction by owner and the runtime sub-id derivation. use craft_ui::runtime::memory::{Remember, SaveKey}; -use craft_ui::testing::memory::{Stage, dispose_evicted, forget, forget_saved, global}; +use craft_ui::testing::memory::{Stage, dispose_evicted, forget, forget_saved, global, records_written}; use craft_ui::testing::{Frames, trial}; const PLACE: SaveKey = SaveKey::new("craft_tests.place"); @@ -86,3 +86,27 @@ fn a_slot_written_by_an_owning_stage_goes_with_that_owner() { dispose_evicted(&frames.ctx); assert_eq!(last % 10, 1, "the slot its maker wrote stayed with the container that keeps showing it"); } + +/// Write-back records a slot once: a stage that keeps writing the same slots records nothing after its first +/// frame, and a stage owned by another component than its base records what it writes every frame. +#[trial] +fn a_settled_stage_records_no_slot_again() { + let mut frames = Frames::new(); + let (base, container) = (global("settled", ()), global("settled_owner", ())); + let show = |ui: &mut egui::Ui| { + let mut stage = Stage::open(ui.ctx(), base); + *stage.memory::() += 1; + stage.finish(); + }; + frames.frame(vec![], show); + let first = records_written(&frames.ctx); + assert!(first > 0, "the first frame records the slot and its seen mark"); + frames.idle(3, show); + assert_eq!(records_written(&frames.ctx), first, "settled frames record nothing"); + frames.idle(3, |ui| { + let mut stage = Stage::open_owned(ui.ctx(), base, container); + *stage.memory::() += 1; + stage.finish(); + }); + assert!(records_written(&frames.ctx) >= first + 3, "an owning stage records what it writes in every frame"); +}