From cc808e7c076386bf27a47e7607299b687970fb4d Mon Sep 17 00:00:00 2001 From: FredApps Date: Thu, 19 Nov 2026 10:34:53 +0100 Subject: [PATCH] Share stamped viewport access and retain pass storage between frames --- crates/ui-contract/src/runtime/frame.rs | 30 ++++++++++++++++++-- crates/ui-contract/src/runtime/pass_state.rs | 8 ++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index e5b077bbf..22e7883a7 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -274,13 +274,37 @@ impl PerViewport> { /// Runs `f` on the record of `viewport` for its pass `pass_nr`; `reset` runs first when the pass changed. #[doc(hidden)] pub fn with_pass(ctx: &Context, viewport: ViewportId, pass_nr: u64, reset: impl FnOnce(&mut T, PassStep), f: impl FnOnce(&mut T) -> R) -> R { - Self::with(ctx, viewport, |record| { + let mut reset = Some(reset); + let mut f = Some(f); + let mut out = None; + Self::visit_pass( + ctx, + viewport, + pass_nr, + &mut |value, step| { + if let Some(reset) = reset.take() { + reset(value, step); + } + }, + &mut |value| { + if let Some(f) = f.take() { + out = Some(f(value)); + } + }, + ); + #[expect(clippy::expect_used, reason = "visit_pass visits the current record exactly once")] + out.expect("the stamped viewport record was visited") + } + + // Callers share stamp validation and reset ordering, including the parked-record lookup. + fn visit_pass(ctx: &Context, viewport: ViewportId, pass_nr: u64, reset: &mut dyn FnMut(&mut T, PassStep), f: &mut dyn FnMut(&mut T)) { + Self::visit(ctx, viewport, &mut |record| { if record.pass != pass_nr { reset(&mut record.value, if record.pass + 1 == pass_nr { PassStep::Next } else { PassStep::Skipped }); record.pass = pass_nr; } - f(&mut record.value) - }) + f(&mut record.value); + }); } } diff --git a/crates/ui-contract/src/runtime/pass_state.rs b/crates/ui-contract/src/runtime/pass_state.rs index 7175420a1..3c80b6d06 100644 --- a/crates/ui-contract/src/runtime/pass_state.rs +++ b/crates/ui-contract/src/runtime/pass_state.rs @@ -225,12 +225,8 @@ fn visit(ctx: &Context, f: &mut dyn FnMut(&PassState)) { /// Moves the context's state into this thread unless it holds it already: the begin hook. States of other contexts /// with no pass running here (a pass a panic left open) go back to their contexts first. pub(crate) fn hold(ctx: &Context) { - let stale: Vec = HELD.with(|held| { - let mut held = held.borrow_mut(); - let (stale, kept) = std::mem::take(&mut held.entries).into_iter().partition(|held| held.ctx != *ctx && frame::running(&held.ctx).is_none()); - held.entries = kept; - stale - }); + let stale: Vec = + HELD.with(|held| held.borrow_mut().entries.extract_if(.., |entry| entry.ctx != *ctx && frame::running(&entry.ctx).is_none()).collect()); for held in stale { park(held); }