From 03710d438d6341b032d1b7df74af5c4dc17c63fc Mon Sep 17 00:00:00 2001 From: Frank McSherry Date: Sat, 15 Aug 2026 15:34:15 -0400 Subject: [PATCH 1/8] Reduce proxy pending-state rebuild overhead --- .../src/operators/int_proxy/reduce.rs | 142 +++++++++++++++--- 1 file changed, 118 insertions(+), 24 deletions(-) diff --git a/differential-dataflow/src/operators/int_proxy/reduce.rs b/differential-dataflow/src/operators/int_proxy/reduce.rs index 1e46d2c4f..52108087d 100644 --- a/differential-dataflow/src/operators/int_proxy/reduce.rs +++ b/differential-dataflow/src/operators/int_proxy/reduce.rs @@ -3,7 +3,10 @@ //! A conventional differential reduce against `(u64, u64)`, where the backend supplies the //! implementation of the interpretation of the integers. +use std::cell::RefCell; use std::collections::BTreeMap; +use std::sync::OnceLock; +use std::time::{Duration, Instant}; use timely::PartialOrder; use timely::progress::{Antichain, Timestamp}; @@ -16,6 +19,72 @@ use super::ProxyBridge; use crate::operators::reduce::{sort_dedup, ReduceTactic}; use crate::operators::ValueHistory; +/// One opt-in phase measurement from the generic proxy-reduce driver. +#[derive(Clone, Debug)] +pub struct ProxyReducePhaseStat { + /// Stable phase label. + pub phase: &'static str, + /// Number of timed invocations. + pub calls: u64, + /// Phase-specific row or slot count, useful only within the named phase. + pub work: u64, + /// Total wall-clock time spent in the phase. + pub elapsed: Duration, +} + +#[derive(Default)] +struct ProxyReducePhaseAccum { + calls: u64, + work: u64, + elapsed: Duration, +} + +thread_local! { + static PROXY_REDUCE_PROFILE: RefCell> = RefCell::new(BTreeMap::new()); +} + +fn proxy_profile_enabled() -> bool { + static ENABLED: OnceLock = OnceLock::new(); + *ENABLED.get_or_init(|| std::env::var("PROXY_REDUCE_PROFILE").is_ok_and(|x| x != "0")) +} + +#[inline] +fn phase_start() -> Option { + proxy_profile_enabled().then(Instant::now) +} + +#[inline] +fn phase_finish(phase: &'static str, start: Option, work: usize) { + if let Some(start) = start { + PROXY_REDUCE_PROFILE.with(|profile| { + let mut profile = profile.borrow_mut(); + let stat = profile.entry(phase).or_default(); + stat.calls += 1; + stat.work += work as u64; + stat.elapsed += start.elapsed(); + }); + } +} + +/// Clear proxy-reduce phase counters for the current worker thread. +pub fn reset_phase_profile() { + PROXY_REDUCE_PROFILE.with(|profile| profile.borrow_mut().clear()); +} + +/// Snapshot current-thread proxy-reduce phase counters, slowest phase first. +pub fn phase_profile() -> Vec { + PROXY_REDUCE_PROFILE.with(|profile| { + let mut stats: Vec<_> = profile.borrow().iter().map(|(&phase, stat)| ProxyReducePhaseStat { + phase, + calls: stat.calls, + work: stat.work, + elapsed: stat.elapsed, + }).collect(); + stats.sort_unstable_by_key(|stat| std::cmp::Reverse(stat.elapsed)); + stats + }) +} + /// A unit of proxied reduce work, presented to the backend. pub struct ReduceInstance<'a, B1: BatchReader, B2: BatchReader