Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions differential-dataflow/src/operators/cursor/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
/// It builds a [`CursorList`](crate::trace::cursor::CursorList) over the input, output, and new-batch
/// updates and replays them together per key, applying `logic` and shaping output with `push`. It holds
/// the outstanding synthetic interesting `(key, time)` moments across activations, and reasons only
/// about times: capabilities are the driver's concern.

Check warning on line 49 in differential-dataflow/src/operators/cursor/reduce.rs

View workflow job for this annotation

GitHub Actions / Cargo clippy

empty line after doc comment

/// The conventional cursor-based [`ReduceTactic`].
pub struct CursorTactic<B1, B2, Bu, L, P>
Expand Down Expand Up @@ -386,9 +386,11 @@

// 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) {
Expand All @@ -406,6 +408,8 @@
// 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`.
//
Expand All @@ -415,35 +419,36 @@
//
// 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);

Expand Down
26 changes: 26 additions & 0 deletions differential-dataflow/tests/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
Loading