Count trial: a settled memory stage records no slot again

This commit is contained in:
aiongg
2026-11-12 14:32:39 +00:00
parent 51c7fc3117
commit 65f67985fc
3 changed files with 43 additions and 1 deletions
+13
View File
@@ -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<Removed>,
/// 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<T: Slot>(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::<Registry>::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<u64> {
Service::<Registry>::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 });
}
+5
View File
@@ -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);
+25 -1
View File
@@ -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::<u32>() += 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::<u32>() += 1;
stage.finish();
});
assert!(records_written(&frames.ctx) >= first + 3, "an owning stage records what it writes in every frame");
}