From 9a3e009b045db695ad002f09485105ab7e5b88b4 Mon Sep 17 00:00:00 2001 From: Frank McSherry Date: Sun, 22 Nov 2026 17:49:20 -0500 Subject: [PATCH] reduce: stop keeping visited synthetic times as witnesses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cursor reduce kept every synthetic time it visited in `times_current`, as a bare time. A bare time never cancels, so it stayed after the updates that produced it cancelled, and kept making later times interesting. With many input times retired in one round, this can cost about n²/2 evaluations where single steps cost about 3n. A visited synthetic time is no longer kept. What made it interesting stays in the batch buffer, and what it emits goes to `output_produced`. `output_produced` now also marks later times interesting and joins with uninteresting times, as `times_current` did. Both are advanced by the meet and consolidated, so they vanish once they cancel. `reduce_scaling_cancelled_history` runs such a round with n = 100,000. It takes about 2 s in debug with this change; on master-next the same workload takes 46 s at n = 1,000 and 361 s at n = 2,000. Co-Authored-By: Claude Opus 5.5 --- .../src/operators/cursor/reduce.rs | 37 +++++++++++-------- differential-dataflow/tests/reduce.rs | 26 +++++++++++++ 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/differential-dataflow/src/operators/cursor/reduce.rs b/differential-dataflow/src/operators/cursor/reduce.rs index 11a83853d..84974afd6 100644 --- a/differential-dataflow/src/operators/cursor/reduce.rs +++ b/differential-dataflow/src/operators/cursor/reduce.rs @@ -386,9 +386,11 @@ mod history_replay { // advance both `synth_times` and `times_slice`, marking this time interesting if in either. while self.synth_times.last() == Some(&next_time) { - // We don't know enough about `next_time` to avoid putting it in to `times_current`. - // TODO: If we knew that the time derived from a canceled batch update, we could remove the time. - self.times_current.push(self.synth_times.pop().expect("failed to pop from synth_times")); // <-- TODO: this could be a min-heap. + // A visited synthetic time is not kept in `times_current`. What made it interesting stays + // in the batch buffer, and what it emits goes to `output_produced`. Both are advanced by + // `meet` and consolidated, so they vanish once they cancel, where a bare time would stay + // forever and make every later time it joins interesting. + self.synth_times.pop().expect("failed to pop from synth_times"); // <-- TODO: this could be a min-heap. interesting = true; } while times_slice.first() == Some(&next_time) { @@ -406,6 +408,8 @@ mod history_replay { // yield false positives if not sufficiently compact. Maybe we should look into this and see. interesting = interesting || batch_replay.buffer().iter().any(|&((_, ref t),_)| t.less_equal(&next_time)); interesting = interesting || self.times_current.iter().any(|t| t.less_equal(&next_time)); + // A correction this round changes the output at every time above it. + interesting = interesting || self.output_produced.iter().any(|((_, t), _)| t.less_equal(&next_time)); // We should only process times that are not in advance of `upper_limit`. // @@ -415,35 +419,36 @@ mod history_replay { // // An uninteresting time in advance of `upper_limit` still needs its synthetic times. With // partially ordered times, advancing history by `meet` can move a time from before the - // interval to beyond it. Its joins with batch times and `times_current` are interesting, - // and no later interval finds them, as it seeds only from its own batch and pending times. - // The joins are also in advance of `upper_limit`, so the determination below defers them. + // interval to beyond it. Its joins with batch times, `times_current`, and this round's output + // are interesting, and no later interval finds them, as it seeds only from its own batch and + // pending times. The joins are also in advance of `upper_limit`, so the determination below + // defers them. if !interesting || !upper_limit.less_equal(&next_time) { // DETERMINATION (times only). Determine synthetic interesting times. // // Synthetic interesting times are produced differently for interesting and uninteresting // times. An uninteresting time must join with an interesting time to become interesting, - // which means joins with `self.batch_history` and `self.times_current`. I think we can - // skip `self.synth_times` as we haven't gotten to them yet, but we will and they will be - // joined against everything. + // which means joins with `self.batch_history`, `self.times_current`, and `self.output_produced`. + // I think we can skip `self.synth_times` as we haven't gotten to them yet, but we will and they + // will be joined against everything. // Any time, even uninteresting times, must be joined with the current accumulation of - // batch times as well as the current accumulation of `times_current`. + // batch times, the current accumulation of `times_current`, and this round's produced output. self.temporary.extend(batch_replay.buffer().iter().map(|((_,time),_)| time).filter(|time| !time.less_equal(&next_time)).map(|time| time.join(&next_time))); self.temporary.extend(self.times_current.iter().filter(|time| !time.less_equal(&next_time)).map(|time| time.join(&next_time))); + self.temporary.extend(self.output_produced.iter().map(|((_,time),_)| time).filter(|time| !time.less_equal(&next_time)).map(|time| time.join(&next_time))); - // An interesting time additionally joins with `input` and `output` history and this round's - // produced output: it carries the seed, so those joins stay interesting (an uninteresting - // time does not, as `input`/`output` times are not themselves seeds). We advance the buffers - // by `meet` first, exactly as evaluation reads them below; by join preservation the advanced - // and unadvanced times spawn the same synthetics, so this matches the pre-split behavior. + // An interesting time additionally joins with `input` and `output` history: it carries the seed, + // so those joins stay interesting (an uninteresting time does not, as `input`/`output` times are + // not themselves seeds). We advance the buffers by `meet` first, exactly as evaluation reads them + // below; by join preservation the advanced and unadvanced times spawn the same synthetics, so this + // matches the pre-split behavior. if interesting { if let Some(meet) = meet.as_ref() { input_replay.advance_buffer_by(meet) }; if let Some(meet) = meet.as_ref() { output_replay.advance_buffer_by(meet) }; self.temporary.extend(input_replay.buffer().iter().map(|((_,time),_)| time).filter(|time| !time.less_equal(&next_time)).map(|time| time.join(&next_time))); self.temporary.extend(output_replay.buffer().iter().map(|((_,time),_)| time).filter(|time| !time.less_equal(&next_time)).map(|time| time.join(&next_time))); - self.temporary.extend(self.output_produced.iter().map(|((_,time),_)| time).filter(|time| !time.less_equal(&next_time)).map(|time| time.join(&next_time))); } sort_dedup(&mut self.temporary); diff --git a/differential-dataflow/tests/reduce.rs b/differential-dataflow/tests/reduce.rs index 114cd3bad..8161ab388 100644 --- a/differential-dataflow/tests/reduce.rs +++ b/differential-dataflow/tests/reduce.rs @@ -193,3 +193,29 @@ fn iterate_distinct_join_with_history_past_upper() { assert_eq!(accumulate(&output, &time), expected, "at {time:?}"); } } + +/// Records that each live for one step of the first coordinate, all retired in one round. At step +/// `i`, record `2i` is present at `(i, 0)` and record `2i+1` at `(i, n+1-i)`, and both are retracted +/// at step `i+1`. A later step `(s, 0)` joins `(i, n+1-i)` at `(s, n+1-i)`, where nothing changes, +/// since record `2i+1` has come and gone there. If the reduce remembers those times after the +/// record cancels, step `s` evaluates about `s` of them, and the round takes about `n²/2` +/// evaluations rather than about `3n`. The third coordinate is unused. +#[test] +fn reduce_scaling_cancelled_history() { + let n: u64 = 100_000; + let mut updates = Vec::new(); + for i in 0 ..= n { + updates.push(((0u64, 2 * i), time3(i, 0, 0), 1)); + updates.push(((0u64, 2 * i), time3(i + 1, 0, 0), -1)); + updates.push(((0u64, 2 * i + 1), time3(i, n + 1 - i, 0), 1)); + updates.push(((0u64, 2 * i + 1), time3(i + 1, n + 1 - i, 0), -1)); + } + let output = run_rounds(vec![time3(0, 0, 0)], vec![(updates, vec![])], |c| { + c.reduce(|_key, input, output| output.push((input.len() as u64, 1))) + }); + // At step `i`, one record is present below `(i, n+1-i)`, and two from there on. + for i in [0, 1, n / 2, n] { + assert_eq!(accumulate(&output, &time3(i, n - i, 0)), [((0, 1), 1)].into(), "at step {i}"); + assert_eq!(accumulate(&output, &time3(i, n + 1 - i, 0)), [((0, 2), 1)].into(), "at step {i}"); + } +}