diff --git a/crates/ui-contract/src/runtime/reads.rs b/crates/ui-contract/src/runtime/reads.rs index 39a115c25..78c71cced 100644 --- a/crates/ui-contract/src/runtime/reads.rs +++ b/crates/ui-contract/src/runtime/reads.rs @@ -96,7 +96,9 @@ pub(crate) fn note(ctx: &Context, key: BindKey, reader: Id, measuring: bool, sam } else { let read = Read { key, reader, measuring, runs, samples }; crate::runtime::replay::note_read(ctx, key); - crate::runtime::replay::journal(ctx, || read.clone(), |ctx, read| with(ctx, |reads| reads.reads.push(read.clone()))); + // The reads are a set to what reads them: a run that measures a component again (a kept transcript's check) + // reads nothing new. + crate::runtime::replay::journal_distinct(ctx, || read.clone(), |ctx, read| with(ctx, |reads| reads.reads.push(read.clone()))); with(ctx, |reads| reads.reads.push(read)); } } diff --git a/crates/ui-contract/src/runtime/replay.rs b/crates/ui-contract/src/runtime/replay.rs index 243ef3337..8ff45c698 100644 --- a/crates/ui-contract/src/runtime/replay.rs +++ b/crates/ui-contract/src/runtime/replay.rs @@ -356,6 +356,25 @@ where } } +/// `journal` for a registration that a second equal one adds nothing to: the innermost open record keeps it once, so +/// its journal does not depend on how many times its run made it. +pub(crate) fn journal_distinct(ctx: &Context, args: impl FnOnce() -> A, again: F) +where + A: PartialEq + fmt::Debug + Shift + Send + Sync + 'static, + F: Fn(&Context, &A) + Copy + Send + Sync + 'static, +{ + const { assert!(std::mem::size_of::() == 0, "`journal_distinct`'s `again` captures nothing: pass its state in `args`") }; + if recording(ctx) && !pass_state::with::(ctx, |recorder| recorder.replaying) { + let entry = Entry(Arc::new(Note { args: args(), again })); + pass_state::with::(ctx, |recorder| { + let from = recorder.open.last().map_or(0, |open| open.from); + if !recorder.journal[from..].contains(&entry) { + recorder.journal.push(entry); + } + }); + } +} + /// Notes that the running code read the value `key`. pub(crate) fn note_read(ctx: &Context, key: BindKey) { with_open(ctx, |facts| {